gpt4 book ai didi

python - 鼠标单击python中的随机图像

转载 作者:行者123 更新时间:2023-12-03 23:06:28 26 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Why is my collision test always returning 'true' and why is the position of the rectangle of the image always wrong (0, 0)?

(1 个回答)


去年关闭。




我是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()

最佳答案

您必须评估鼠标是否在图像上。注意,一个 pygame.Surface 没有位置。是blit在一个位置。因此 pygame.Rect 的位置对象,由 get_rect() 返回是 (0, 0)。
您必须通过关键字参数(例如 image.get_rect(topleft = (posx, posy)) )设置位置。最后你可以使用 collidepoint() 评估鼠标光标( MousexMousey )是否位于当前放置图像的显示区域上:

if event.type == pygame.MOUSEBUTTONDOWN:
Mousex, Mousey = event.pos
image_rect = image.get_rect(topleft = (posx, posy))
if image_rect.collidepoint(Mousex, Mousey):
score += 1

更进一步, time.sleep(tsp)阻止系统响应。永远不要延迟主应用程序循环。
使用 pygame.time.get_ticks() 以毫秒为单位获取时间。添加变量 next_choice_time .时间指示何时必须更改图像的位置。设置更改图像位置的新时间:
next_choice_time = 0
while run:
current_time = pygame.time.get_ticks()

for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.MOUSEBUTTONDOWN:
Mousex, Mousey = event.pos
image_rect = image.get_rect(topleft = (posx, posy))
if image_rect.collidepoint(Mousex, Mousey):
score += 1
next_choice_time = current_time

if current_time >= next_choice_time:
next_choice_time = current_time + 300 # 300 milliseconds == 0.3 seconds
side = random.choices(sides, weights)[0]
# [...]


看例子:
import pygame
import random

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

next_choice_time = 0
while run:
current_time = pygame.time.get_ticks()

for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.MOUSEBUTTONDOWN:
Mousex, Mousey = event.pos
image_rect = image.get_rect(topleft = (posx, posy))
if image_rect.collidepoint(Mousex, Mousey):
score += 1
next_choice_time = current_time
print(score)

if current_time >= next_choice_time:
next_choice_time = current_time + 300 # 300 milliseconds == 0.3 seconds
side = random.choices(sides, weights)[0]
if side == 'Top':
posx = random.randrange(100, 300)
posy = random.randrange(20, 100)
elif side == 'Botom':
posx = random.randrange(350, 430)
posy = random.randrange(250, 450)
elif side == 'left':
posx = random.randrange(20, 250)
posy = random.randrange(20, 250)
elif side == 'Right':
posx = random.randrange(280, 450)
posy = random.randrange(280, 450)

display.fill((153, 255, 187))
image_view(posx, posy)
pygame.display.update()

关于python - 鼠标单击python中的随机图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62374999/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com