python - 鼠标单击python中的随机图像
问题描述
我是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()
解决方案
您必须评估鼠标是否在图像上。注意,apygame.Surface
没有位置。它blit
处于一个位置。因此pygame.Rect
,返回的对象的位置get_rect()
是 (0, 0)。
您必须通过关键字参数(例如image.get_rect(topleft = (posx, posy))
)来设置位置。最后,您可以使用collidepoint()
来评估鼠标光标 ( Mousex
, Mousey
) 是否位于当前放置图像的显示区域上:
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()
推荐阅读
- r - 如何在R中绘制由两部分组成的数据?
- javascript - CSV 解析多个文件
- java - 如何反序列化通用列表
与杰克逊? - python - 使用条件将数据框映射到另一个数据框
- ms-access - MS Access 的多对多关系问题
- ubuntu-18.04 - 从 ubuntu 18.04 过渡到 centos 8 后的 Iconv
- meshlab - 是否可以在 Meshlab 中加载多个网格并一次显示一个?
- javascript - 如何告诉 javascript 验证我的信息并通过提交按钮同时将我引导到另一个页面
- c++ - c++中如何在字符串中查找关键字
- javascript - Gatsby/GraphQL 可以利用 javascript 文件吗?