我对pygame非常陌生,正在尝试制作一些非常基础的东西。这段代码应该根据鼠标的X位置改变矩形的颜色。
问题在于矩形要么不显示,要么控制台会给出错误提示:
AttributeError: 'pygame.Rect' object has no attribute 'color'
这里是相关代码:
mouseButtons = pygame.mouse.get_pos()
test = pygame.draw.rect(screen, (255,0,0), rect1)
if (mouseButtons[0] <= 100):
color = (0, 255, 0)
else:
color = (255, 0, 0)
test = pygame.draw.rect(screen, color, rect1)
这里是完整的代码:
import pygame
from pygame.locals import *
SIZE = 400, 400
pygame.init()
screen = pygame.display.set_mode(SIZE)
rect1 = Rect(100, 100, 200, 200)
def loop():
mouseButtons = pygame.mouse.get_pos()
test = pygame.draw.rect(screen, (255,0,0), rect1)
if (mouseButtons[0] <= 100):
color = (0, 255, 0)
else:
color = (255, 0, 0)
test = pygame.draw.rect(screen, color, rect1)
running = True
while running:
for event in pygame.event.get():
if event.type == QUIT:
running = False
loop()
screen.fill((255, 255, 255))
pygame.display.flip()
pygame.quit()