【发布时间】:2017-08-11 09:42:18
【问题描述】:
我正在使用 pygame 库,我需要创建一些按钮。这是我的代码的一部分:
import pygame
pygame.init()
screenw=1000 ;screenh=550
screen = pygame.display.set_mode(( screenw , screenh ))
interface=1
class Button(object):
def __init__(self, aroundimage, onimage,position):
self.imageAround = pygame.image.load(aroundimage).convert_alpha()
self.imageOn = pygame.image.load(onimage).convert_alpha()
self.position = position
def isOn(self):
point_x,point_y = pygame.mouse.get_pos()
x, y = self. position
w, h = self.imageAround.get_size()
in_x = x - w/2 < point_x < x + w/2
in_y = y - h/2 < point_y < y + h/2
return in_x and in_y
def render(self):
w, h = self.imageAround.get_size()
x, y = self.position
if self.isOn():
screen.blit(self.imageOn, (x-w/2, y-h/2))
else:
screen.blit(self.imageAround, (x-w/2, y-h/2))
bStart = Button(r'pic\button\startAround.png',r'pic\button\startOn.png',(500,450))
bPlay = Button(r'pic\button\playAround.png',r'pic\button\playOn.png',(500,450))
button_choiceR=Button(r'pic\button\rightAround.png',r'pic\button\rightOn.png',(650,225))
KeepGoing=True
while KeepGoing:
screen.blit(background,(0,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
KeepGoing = False
if interface==1:
bStart.render()
if bStart.isOn() and pygame.mouse.get_pressed()[0]:
face=2
if interface==2:
bPlay.render()
if bPlay.isOn() and pygame.mouse.get_pressed()[0]:
face=3
if button_choiceR.isOn() and pygame.mouse.get_pressed()[0]:
playing=True
if interface==3:
if playing:
pass #other code
当它运行时,我点击“bStart”按钮,游戏直接切换到界面3,因为“bStart”和“bPlay”在同一个位置。
前提是不移动两个按钮或删除一个界面。出于某种原因,我必须保留它们。
【问题讨论】:
-
所以
bStart和bPlay按钮应该在同一个位置,但只有bStart应该是活动的?
标签: python python-3.x class pygame imagebutton