pygame在display.upd之后停止

2024-05-15 01:32:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经编写了一个游戏(“躲避球”),它有一个开始菜单,但如果我按下播放按钮,屏幕不会做任何事情,虽然每个回合都有“显示功能”,但程序仍然工作,并在控制台中显示输出

这个程序已经运行了,但是现在我想给它添加一个菜单。 请原谅,如果我错过了什么要说的-这是我在论坛上的第一个问题,很抱歉我的英语不好

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

Tags: runrectselfnewifpositionwidthscreen

热门问题