我已经编写了一个游戏(“躲避球”),该游戏具有开始菜单,但是如果我按了播放按钮,则屏幕无任何作用,尽管每回合都有“显示功能”,但是该程序仍然可以运行,并在控制台中显示输出。
该程序已经可以工作了,但是现在我想为其添加菜单。 请原谅,如果我有话没说-这是我在论坛上的第一个问题,对不起我的英语不好。
class Button(object):
def __init__(self,x,y,label):
self.surround = 10
self.width = 400
self.height = 60
self.x = x - self.width/2
self.y = y - self.height/2
self.label = label
self.borderColor = (100,100,255)
self.pressedColor = (0,0,120)
self.mouseOverButtonColor = (0,0,150)
self.basicColor = (0,0,200)
self.borderThickness = 2
self.rect = pg.Rect(self.x, self.y,self.width, self.height)
self.rectOut = pg.Rect(self.x-self.borderThickness, self.y-self.borderThickness,self.width+2*self.borderThickness, self.height+2*self.borderThickness)
self.font = pg.font.Font('freesansbold.ttf', 80)
self.label = font.render(self.label, True, fontColor)
self.labelwidth = self.label.get_width()
self.labelheight = self.label.get_height()
def checkMouseOverButton(self):
x, y = pg.mouse.get_pos()
if self.x < x < self.x+self.width and self.y < y < self.y+self.height:
return True
else:
return False
def show(self):
x, y = pg.mouse.get_pos()
leftPressed,rightPressed,middelPressed = pg.mouse.get_pressed()
pg.draw.rect(screen, self.lineColor, self.rectOut)
if self.x < x < self.x+self.width and self.y < y < self.y+self.height:
if leftPressed:
pg.draw.rect(screen, self.pressedColor, self.rect)
else:
pg.draw.rect(screen, self.mouseOverButtonColor, self.rect)
else:
pg.draw.rect(screen, self.basicColor, self.rect)
screen.blit(self.label,(self.x+self.width/2-self.labelwidth/2,self.y+self.height/2-self.labelheight/2))
def game():
global heartnumber,probability,stripespeed,score
clock.tick(30)
player.gotomouse() #own Function
if heartnumber == 0: #check for life-condition
run = False
if r.randrange(0,1000) < probability: #new stripe falling down
newstripex = r.randrange(0,screenWidth)
newstripe = projectile(round(stripespeed+r.randrange(-2,12)), newstripex, -64, newstripex+8)
stripes.append(newstripe)
print("New Stripe:{}".format(newstripe))
for stripe in stripes:
stripe.position['y1'] += stripe.speed
stripe.position['y2'] += stripe.speed
if collision(stripe.position['x1'], stripe.position['y1'],stripe.position['x2']-stripe.position['x1'], stripe.position['y2']-stripe.position['y1'],player.position['x1']+player.width/2, player.position['y1']+player.height/2, player.width/2):
print("Heartnumber -= 1")
heartnumber -= 1
del stripes[stripes.index(stripe)]
'''if stripe.position['y2'] > player.position['y1'] + 60:
if not player.position['x2'] < stripe.position['x1'] and not player.position['x1'] > stripe.position['x2']:
heartnumber -= 1
del stripes[stripes.index(stripe)]'''
if stripe.position['y1'] >= screenHeight:
print("Stripe '{}' deleted and Score += 1'".format(stripe))
stripes.pop(stripes.index(stripe))
score += 1
probability += probability_addition
stripespeed += stripespeedaddition
show()
def show():
global fpsLabel
for g in range(heartnumber):
screen.blit(heart, (screenWidth * (0.95 - g * 0.05) ,15))
screen.blit(scorelabel, (20,20))
scorel = font.render((str(score)), 10, fontColor)
screen.blit(scorel, (200,20))
for stripe in stripes:
screen.blit(stripe.pic, (stripe.position['x1'], stripe.position['y1']))
player.show()
fpsLabel.show()
pg.display.flip()
print("New Pic")
while work:
print("New Work Run")
clock = pg.time.Clock()
stripes = []
run = True
quit = False
mLeftClick = False
player = Player(0, screenHeight*0.92)
start()
print("Startmethod ready")
pg.mouse.set_visible(mousevisibility)
while run != 0:
game()
print("Next run")
这是输出的重要部分:
the startLoop L-I-V-E-S
the startLoop L-I-V-E-S
Playbutton pressed
Startmethod ready
New Pic
Next run
New Pic
Next run
New Pic
Next run
New Pic
Next run
New Pic
Next run
New Stripe:<__main__.projectile object at 0x102b3bf60>
New Pic
Next run