【问题标题】:PyGame - Plotting a Pixel and Printing out it's coordinatesPyGame - 绘制一个像素并打印出它的坐标
【发布时间】:2012-11-28 22:24:28
【问题描述】:

这似乎是一个很容易回答的问题,但我只是一个需要快速帮助的初学者。

我正在尝试创建一个程序,当您单击 pyGame 窗口上的某个位置时,它会打印出您用鼠标左键点击它,并打印出按下位置的坐标.我已经有了这个。我在让它在 pyGame 窗口上绘制像素时遇到问题。基本上,我希望它在我按下 pyGame 窗口的位置绘制一个像素。

#!/usr/bin/env python

#import the module for use 
import pygame

#setting up some variables
running = 1
LEFT = 1 
#Set up the graphics area/screen
screen=pygame.display.set_mode((640,400))

#continuous loop to keep the graphics running
while running==1:
    event=pygame.event.poll()
    if event.type==pygame.QUIT:
        running=0
        pygame.quit()
    elif event.type==pygame.MOUSEBUTTONDOWN and event.button==LEFT:
        print "You pressed the left mouse button at (%d,%d)" %event.pos
    elif event.type==pygame.MOUSEBUTTONUP and event.button==LEFT:
        print "You released the left mouse button at (%d,%d)" %event.pos

【问题讨论】:

    标签: python graphics pygame python-idle


    【解决方案1】:

    在收到鼠标按下事件时尝试设置每个像素的颜色。

    elif event.type==pygame.MOUSEBUTTONDOWN and event.button==LEFT:
      print "You pressed the left mouse button at (%d,%d)" %event.pos
      screen.set_at((event.pos.x, event.pos.y), pygame.Color(255,0,0,255))
    

    请注意,这将根据需要临时锁定和解锁 Surface。

    【讨论】:

    • 如果你能帮上忙,我该怎么办?
    • @user1861480 - 进行了更改,它应该将您单击并拖动到的每个像素的颜色更改为红色。
    • 我很抱歉。我是这方面的初学者。不过我想知道,我把这个放在代码的什么地方?因为我希望它就像我单击 pyGame 窗口上的任意位置时一样,会出现一个新像素。当我输入这段代码时,它告诉我 x 没有属性
    【解决方案2】:

    我将 Aesthete 的代码编辑为一个独立的工作示例:

    根据您的操作,获取/设置单个像素可能会很慢。 (如果需要,还有 Surfarray 和 Pixelarray。)

    import pygame
    from pygame.locals import *
    
    class Game(object):
        done = False
        def __init__(self, width=640, height=480):
            pygame.init()
            self.width, self.height = width, height
            self.screen = pygame.display.set_mode((width, height))
    
            # start with empty screen, since we modify it every mouseclick
            self.screen.fill(Color("gray50"))
    
        def main_loop(self):
            while not self.done:
                # events
                events = pygame.event.get()
                for event in events:
                    if event.type == pygame.QUIT: self.done = True
                    elif event.type == KEYDOWN:
                        if event.key == K_ESCAPE: self.done = True
                    elif event.type == MOUSEMOTION:
                        pass
                    elif event.type == MOUSEBUTTONDOWN and event.button == 1:
                        print "Click: ({})".format(event.pos)
                        self.screen.set_at(event.pos, Color("white"))
    
                # draw
                pygame.display.flip()
    
    
    if __name__ == "__main__":
        g = Game()
        g.main_loop()
    

    【讨论】:

    • 对我不起作用。给了我一些错误。不过还是谢谢。
    • 你确定吗?因为我什至没有使用'x'。它在我的身上运行良好。
    • 抱歉,这是错误 Traceback(最近一次调用最后一次):文件“C:/Python26/1234.py”,第 34 行,在 g.main_loop() 文件“C: /Python26/1234.py”,第 25 行,在 main_loop 中打印“单击:({})”.format(event.pos) ValueError:格式中的零长度字段名称
    猜你喜欢
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-01
    相关资源
    最近更新 更多