我正在写一个基本的画图程序练习pygame的,但我有我使用的按键事件来改变绘制颜色有问题,但是当你放开键,又回到默认的黑色。 我敢肯定有一个简单的方法来解决这个问题我不知道怎么办!
这里是我的代码:
import pygame
from pygame.locals import *
import sys
RED = (255,0,0)
GREEN = (0,255,0)
BLUE = (0,0,255)
WHITE = (255,255,255)
class Draw(object):
def update(self, screen):
color = (0,0,0)
key = pygame.key.get_pressed()
if key[pygame.K_r]:
color = RED
if key[pygame.K_g]:
color = GREEN
if key[pygame.K_b]:
color = BLUE
if key[pygame.K_w]:
color = WHITE
mouse_pos = pygame.mouse.get_pos()
pygame.draw.circle(screen, (color), (mouse_pos),30)
def main(self):
pygame.init()
screen = pygame.display.set_mode((640, 480))
pygame.display.set_caption('Basic Pygame program')
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((0, 0, 0))
screen.blit(background, (0, 0))
while 1:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
self.update(screen)
pygame.display.flip()
if __name__ == '__main__':
draw = Draw()
draw.main()
同时,任何指针或更简单的方式来写我的代码是大加赞赏
谢谢!