第34行显然是发生错误的地方,我是pygame的新手,所以我实际上不知道发生了什么。该代码用于在单击图像后执行功能。我也不知道这是否可行,因为此错误使我无法知道。
import pygame
black = (0, 0, 0)
white = (255, 255, 255)
green = (0, 255, 0)
red = (255, 0, 0)
pygame.init()
size = (500, 400)
screen = pygame.display.set_mode(size)
pygame.draw.rect(screen, red,(150,450,100,50))
button1 = pygame.Rect(100,100,50,50)
button2 = pygame.Rect(200,200,50,50)
button3 = pygame.Rect(130,250,50,50)
pygame.display.set_caption("Yami no Game")
txt = pygame.image.load('txt.png')
Stxt = pygame.transform.scale(txt,(48,48))
exe = pygame.image.load('exe.jpg')
Sexe = pygame.transform.scale(exe,(48,48))
done = False
clock = pygame.time.Clock()
background_image=pygame.image.load('windows_background.jpg').convert()
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
mouse = pygame.mouse.get_pos() # position
click = pygame.mouse.get_pressed() # left/right click
if Sexe[100] + Sexe[48] > mouse[0] > Sexe[100] and Sexe[100] + Sexe(48) > mouse[1] > Sexe[100]: # Mouse coordinates checking.
if click[0] == 1: # Left click
import The_Start #ignore this
screen.blit(background_image, [0,0])
screen.blit(Stxt,[100,100])
screen.blit(Sexe,[250,250])
pygame.display.update()
clock.tick(60)
pygame.quit()
答案 0 :(得分:1)
似乎您想评估鼠标是否位于pygame.Surface
对象Sexe
所覆盖的矩形区域中。
我建议通过pygame.Rect
形式Sexe
获取一个get_rect()
对象。
使用collidepoint()
检查鼠标是否在矩形区域中。
请注意,Surface没有位置,当blit
时,它获取一个位置。因此,矩形的位置必须由关键字参数(Sexe.get_rect(topleft = (250,250))
)设置:
while not done:
# [...]
sexe_rect = Sexe.get_rect(topleft = (250, 250)) # position of Sexe as in blit(Sexe, [250,250])
mouse_pos = pygame.mouse.get_pos()
if sexe_rect.collidepoint(mouse_pos):
click = pygame.mouse.get_pressed()
if click[0] == 1:
print("Clicked on Sexe")
# [...]
screen.blit(Sexe, [250,250])
# [...]