首页 > 解决方案 > 鼠标单击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()

标签: pythonpygame

解决方案


您必须评估鼠标是否在图像上。注意,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()

推荐阅读