pygame 的新功能.我的按钮不起作用.为什么? [英] new with pygame. My button doesnt work. why?
本文介绍了pygame 的新功能.我的按钮不起作用.为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
忽略 AmountOfPlayers()
函数.我是 pygame 的新手,我的按钮什么也没做.为什么?如何使用 pygame 改进我的代码?
Ignore AmountOfPlayers()
function.
Im new with pygame and my button doesn't do anything. why? How can I improve my code with pygame?
while True:
pygame.init()
size=(1440,810)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("POCKER SFAHOT")
welcomeimage = 'welcome.png'
imgwelcome = pygame.image.load(welcomeimage)
screen.blit(imgwelcome, (0,0))
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
mouse = pygame.mouse.get_pos()
if (300 <= mouse[0] <= 1150) and (600 <= mouse[1] <= 470):
AmountOfPlayers()
pygame.event.get()
推荐答案
条件 600 <= mouse[1] <= 470
总是 False
,因为600 永远不会小于 470.它必须是 470 <= mouse[1] <= 600
:
The condition 600 <= mouse[1] <= 470
is always False
, because 600 is never less than 470. It has to be 470 <= mouse[1] <= 600
:
>if (300 <= mouse[0] <= 1150) and (600 <= mouse[1] <= 470):
if (300 <= mouse[0] <= 1150) and (470 <= mouse[1] <= 600):
不过,我建议使用 pygame.Rect
和 collidepoint()
.看例子:
However, I recommend using pygame.Rect
and collidepoint()
. See the example:
import pygame
pygame.init()
size=(1440,810)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("POCKER SFAHOT")
welcomeimage = 'welcome.png'
imgwelcome = pygame.image.load(welcomeimage).convert_alpha()
welcome = True
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.MOUSEBUTTONDOWN:
rect = pygame.Rect(300, 370, 850, 130)
if welcome and rect.collidepoint(event.pos):
welcome = False
if welcome:
screen.blit(imgwelcome, (0,0))
else:
screen.fill(0)
AmountOfPlayers()
pygame.display.flip()
pygame.quit()
exit()
这篇关于pygame 的新功能.我的按钮不起作用.为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文