python - 在python中创建一个按钮
问题描述
我正在尝试为有特殊需要的孩子制作一个互动游戏,你点击一个按钮,它就会移动照片。但是,我需要一些帮助,因为当我运行我的代码时,控制台会打印“...”,就像您单击按钮时一样,而当您单击远离按钮时则不会。一切都在那里工作。无论您单击按钮上方或下方的哪个位置,它都会将其识别为按钮。因此它无法将 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
解决方案
您必须单独检查坐标。并且不要忘记按钮大小:
mx, my = pygame.mouse.get_pos()
if mx >= 200 and my >= 400 and mx < 200 + imageOption3.get_width() and my < 400 + imageOption3.get_height():
print("horse")
您还可以使用列表缩短整个内容(如果您不熟悉对象):
gameData = [
# (buttonX, buttonY, name) - will be extended by button image
[400, 400, 'Cow'],
[500, 400, 'Duck'],
#...
]
# load buttons:
for item in gameData:
item.append(pygame.image.load("photos/button%s.png"%item[2]))
def displayOptions():
for button in gameData:
screen.blit(button[3], (button[0], button[1]))
def buttons():
mx, my = pygame.mouse.get_pos()
for button in gameData:
bx, by = button[0], item[1]
bw, bh = button[3].get_size()
if mx >= bx and my >= by and mx < bx+bw and my < by+bh:
print(button[2])
# etc.
推荐阅读
- jquery - 可以通过 CSS 中的值来定位元素吗?
- javascript - ExecJS::RuntimeError 使用等待意外令牌 (48:10)
- amazon-web-services - AWS Cognito - 使用 JWT 与 cognito.getUser 开发工具包验证令牌
- python - 为什么 Pandas 的函数 to_excel 将公式字符串小写?
- java - 如果设置为 null,Lombok @Builder.Default 不适用于 @Value 对象
- wpf - 调整 WPF 中的投影大小。如图所示
- ios - 使用 formData 在 iOS 应用中上传两种文件类型?
- ios - UIScrollview 底部的额外空白区域
- java - 如何使用 Junit 4.11 有条件地执行测试类
- xml - 用于将文件从一个位置复制到另一个位置并重命名的 Shell 脚本将当前日期和时间戳添加到 YYYYMMDDHHmmSS 格式的每个文件