用鼠标点击python中的随机图像

2024-06-07 06:05:29 发布

您现在位置: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()

Tags: imageeventiftimedisplaysleeprandomwidth
1条回答
网友
1楼 · 发布于 2024-06-07 06:05:29

您必须评估鼠标是否在图像上。注意^{}没有位置。它位于某个位置。因此^{}返回的^{}对象的位置是(0,0)。
您必须通过关键字参数设置位置(例如image.get_rect(topleft = (posx, posy)))。最后,您可以使用^{}评估鼠标光标(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)阻止系统响应。切勿延迟主应用程序循环。
使用^{}以毫秒为单位获取时间。添加变量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()

相关问题 更多 >