在python中创建按钮

问题描述 投票:0回答:1

我正在尝试为有特殊需要的孩子们制作一个互动游戏,在这里您单击一个按钮,然后它将照片移开。但是,我需要一些帮助,因为当我运行代码时,控制台会按您单击按钮时的样子打印“ ...”,而当您单击按钮之外时,它不会打印。所有人都在那里工作。无论您在按钮的上方还是下方单击任何位置,它都将其识别为按钮。因此,它无法将y坐标列区分为不是按钮坐标的一部分。帮助将不胜感激!参见照片:[游戏窗口] [1] ...代码:

import pygame
import random

pygame.init()
#define variables
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
background_colour = (255, 255, 255)
(width, height) = (700, 500)
x = 250
y = 100
mpos = pygame.mouse.get_pos()
mpress = pygame.mouse.get_pressed()
Button1 = 'photos/buttonCow.png'
Button2 = 'photos/buttonDuck.png'
Button3 = 'photos/buttonHorse.png'
Button4 = 'photos/buttonSheep.png'

imageOption1 = pygame.image.load(Button1)
imageOption2 = pygame.image.load(Button2)
imageOption3 = pygame.image.load(Button3)
imageOption4 = pygame.image.load(Button4)


screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Game!')
screen.fill(background_colour)
running = True


def buttons():
    if pygame.mouse.get_pos() >= (200, 400):
        if pygame.mouse.get_pos() <= (260, 430):
            print("horse")
    if pygame.mouse.get_pos() >= (300, 400):
        if pygame.mouse.get_pos() <= (360, 430):
            print("duck")
    if pygame.mouse.get_pos() >= (400, 400):
        if pygame.mouse.get_pos() <= (460, 430):
            print("cow")
    if pygame.mouse.get_pos() >= (500, 400):
        if pygame.mouse.get_pos() <= (560, 430):
            print("sheep")


def displayOptions():
    screen.blit(imageOption1, (400, 400))
    screen.blit(imageOption2, (300, 400))
    screen.blit(imageOption3, (200, 400))
    screen.blit(imageOption4, (500, 400))


def whichAnimalFunc():
    whichAnimal = random.randint(0, 4)
    if whichAnimal == 1:
        image = pygame.image.load('photos/cow.png')
        screen.fill(WHITE)
        screen.blit(image, (x, y))
        displayOptions()
        pygame.display.update()
    if whichAnimal == 2:
        image = pygame.image.load('photos/duck.png')
        screen.fill(WHITE)
        screen.blit(image, (x, y))
        displayOptions()
        pygame.display.update()
    if whichAnimal == 3:
        image = pygame.image.load('photos/horse.png')
        screen.fill(WHITE)
        screen.blit(image, (x, y))
        displayOptions()
        pygame.display.update()
    elif whichAnimal == 4:
        image = pygame.image.load('photos/sheep.jpg')
        screen.fill(WHITE)
        screen.blit(image, (x, y))
        displayOptions()
        pygame.display.update()
    else:
        pygame.QUIT


while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False  # allows quit button to work
        if event.type == pygame.MOUSEBUTTONDOWN:
            pos = pygame.mouse.get_pos()
            print(pos)
            buttons()
            whichAnimalFunc()
            pygame.display.update()```



  [1]: https://i.stack.imgur.com/xDCa0.png
python pygame python-3.7
1个回答
0
投票

您不能只说pygame.mouse.get_pos() >= (200, 400),您必须说:

mouse_pos = pygame.mouse.get_pos()
if mouse[0] >= 200 and mouse[0] >= 200:
   # hovering
© www.soinside.com 2019 - 2024. All rights reserved.