我是python初学者。最近我在做一个pygame项目。我想做一个游戏,屏幕将显示一个随机位置的图像,0.3秒后,图像将再次移动到另一个随机位置。玩家会用鼠标反复点击改变位置的图像,分数会增加。即使我做了所有事情,用鼠标点击也不会增加分数
这是我的密码:
pygame.init()
width = 500
height = 500
score = 0
display = pygame.display.set_mode((width, height))
pygame.display.set_caption("Tapping")
image = pygame.image.load('spaceship.png').convert()
sides = ['Top', 'Botom', 'left', 'Right']
weights = [width, width, height, height]
posx = random.randint(50, 450)
posy = random.randint(20, 460)
tsp = 1.2
Mousex = 0
Mousey = 0
def image_view(x, y):
display.blit(image, (x, y))
run = True
while run:
display.fill((153, 255, 187))
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.MOUSEBUTTONDOWN:
Mousex, Mousey = event.pos
if image.get_rect().collidepoint(posx, posy):
score += 1
side = random.choices(sides, weights)[0]
if side == 'Top':
posx = random.randrange(100, 300)
posy = random.randrange(20, 100)
time.sleep(tsp)
elif side == 'Botom':
posx = random.randrange(350, 430)
posy = random.randrange(250, 450)
time.sleep(tsp)
elif side == 'left':
posx = random.randrange(20, 250)
posy = random.randrange(20, 250)
time.sleep(tsp)
elif side == 'Right':
posx = random.randrange(280, 450)
posy = random.randrange(280, 450)
time.sleep(tsp)
print(score)
image_view(posx, posy)
pygame.display.update()
您必须评估鼠标是否在图像上。注意^{} 没有位置。它位于某个位置。因此^{} 返回的^{} 对象的位置是(0,0)。} 评估鼠标光标(
您必须通过关键字参数设置位置(例如
image.get_rect(topleft = (posx, posy))
)。最后,您可以使用^{Mousex
,Mousey
)是否位于当前放置图像的显示区域上:此外,} 以毫秒为单位获取时间。添加变量
time.sleep(tsp)
阻止系统响应。切勿延迟主应用程序循环。使用^{
next_choice_time
。时间指示必须更改图像位置的时间。设置更改图像位置时的新时间:请参见示例:
相关问题 更多 >
编程相关推荐