Python 生命计数器不断重置

Python 生命计数器不断重置,python,python-3.x,pygame,Python,Python 3.x,Pygame,我最近进入了pygame,我遇到了麻烦。我的生命计数器在我的程序中不工作 import pygame, time, random pygame.init() # Initializes Pygame display_width = 1280 display_height = 720 gameDisplay = pygame.display.set_mode((display_width,display_height)) pygame.display.set_caption('Cube Ga

我最近进入了pygame,我遇到了麻烦。我的生命计数器在我的程序中不工作

import pygame, time, random

pygame.init() # Initializes Pygame

display_width = 1280
display_height = 720

gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Cube Game')

black = (0,0,0)
white = (255,255,255)

clock = pygame.time.Clock()

def life(lives):
    font = pygame.font.SysFont(None, 25);
    text = font.render("Lives: " + str(lives), True, white)
    gameDisplay.blit(text,(1200,5))

def score(count):
    font = pygame.font.SysFont(None, 25);
    text = font.render("Score: " + str(count), True, white)
    gameDisplay.blit(text,(5,5))

def enemyBall(ballx, bally, ballr, color):
    pygame.draw.circle(gameDisplay, color, [ballx, bally], ballr)

# Creates Location for the Cube
def cube(x,y,side):
    pygame.draw.rect(gameDisplay, white, [x, y, side, side])

def text_objects(text, font):
    textSurface = font.render(text, True, white)
    return textSurface, textSurface.get_rect()

def message_display(text):
    largeText = pygame.font.Font('freesansbold.ttf', 50)
    TextSurf, TextRect = text_objects(text, largeText)
    TextRect.center = ((display_width/2),(display_height/2))
    gameDisplay.blit(TextSurf, TextRect)

    pygame.display.update()

    time.sleep(2)

    __init__()

def dead():
    gameDisplay.fill(black)
    message_display('You have fallen out of the castle and died!')


# Game Loop
def __init__():
    x = ((display_width/2) - 30)
    y = (display_height - 70)
    side = 60

    x_change = 0

    ballx_change = 25
    bally_change = 25
    ball_radius = 50

    ball_startx = random.randrange(26, (display_width-ball_radius-1))
    ball_starty = random.randrange((ball_radius+1), 100)

    death = False
    goodbye = False
    myLives = 3
    myScore = 0


    # If Player has not Died
    while not death:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                death = True
                pygame.display.quit()
                pygame.quit()
                quit()


            # Key is still Pressed
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x_change = -10
                if event.key == pygame.K_RIGHT:
                    x_change = 10

            # Key is no longer Pressed
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    x_change = 0

        x += x_change


        gameDisplay.fill(black)

        # Defines the enemy ball
        enemyBall(ball_startx, ball_starty, ball_radius, white)
        ball_startx += ballx_change
        ball_starty += bally_change

        # Wall Collision
        if ball_startx - ball_radius <= 0 or ball_startx + ball_radius >= display_width:
            ballx_change = -ballx_change
            myScore += 1
        if ball_starty - ball_radius <= 0 or ball_starty + ball_radius >= display_height:
            bally_change = -bally_change
            myScore += 1

        # Determines Player Position
        cube(x,y,side)

        # Score and Life Counter
        score(myScore)
        life(myLives)

        # Death by Abyss
        if x > display_width or x < -50:
            myLives -= 1
            dead()

        # Death by Ball
        if ball_startx + ball_radius >= x and ball_startx - ball_radius <= x + side:
            if ball_starty + ball_radius >= y and ball_starty - ball_radius <= y + side:
                myLives -= 1
                dead()

        pygame.display.update()
        clock.tick(60)

time.sleep(2)
__init__()
pygame.display.quit()
pygame.quit()
quit()

导入pygame、时间、随机
pygame.init()初始化pygame
显示宽度=1280
显示高度=720
gameDisplay=pygame.display.set_模式((显示宽度、显示高度))
pygame.display.set_标题('立方体游戏')
黑色=(0,0,0)
白色=(255255)
clock=pygame.time.clock()
def生命(生命):
font=pygame.font.SysFont(无,25);
text=font.render(“生命:”+str(生命),真,白色)
游戏显示.blit(文本,(1200,5))
def分数(计数):
font=pygame.font.SysFont(无,25);
text=font.render(“分数:+str(计数),真,白色)
blit(文本,(5,5))
def enemyBall(圆球、圆球、圆球、彩色):
pygame.draw.circle(游戏显示,颜色,[ballx,bally],ballr)
#创建多维数据集的位置
def立方体(x、y、侧面):
pygame.draw.rect(游戏显示,白色,[x,y,side,side])
def text_对象(文本、字体):
textSurface=font.render(文本、真、白)
返回textSurface,textSurface.get_rect()
def信息_显示(文本):
largeText=pygame.font.font('freesansbold.ttf',50)
TextSurf,TextRect=text\u对象(text,largeText)
text rect.center=((显示宽度/2),(显示高度/2))
blit(TextSurf,TextRect)
pygame.display.update()
时间。睡眠(2)
__初始化
def dead():
游戏显示。填充(黑色)
信息显示(“你从城堡里掉出来死了!”)
#游戏循环
def u u init__;()
x=((显示宽度/2)-30)
y=(显示高度-70)
侧面=60
x_变化=0
ballx_变化=25
巴利变化=25
球头半径=50
ball_startx=random.randrange(26,(显示宽度-ball_半径-1))
ball_starty=random.randrange((ball_半径+1),100)
死亡=虚假
再见=错
迈利夫=3
myScore=0
#如果玩家没有死
虽然不是死亡:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
死亡=真实
pygame.display.quit()
pygame.quit()
退出
#键仍然按下
如果event.type==pygame.KEYDOWN:
如果event.key==pygame.K_左:
x_变化=-10
如果event.key==pygame.K_RIGHT:
x_变化=10
#按键不再按下
如果event.type==pygame.KEYUP:
如果event.key==pygame.K_左或event.key==pygame.K_右:
x_变化=0
x+=x_变化
游戏显示。填充(黑色)
#定义敌人的球
enemyBall(ball_startx、ball_starty、ball_半径、白色)
ball_startx+=ballx_变化
ball_starty+=bally_change
#撞墙
如果球头半径=显示宽度:
ballx_change=-ballx_change
myScore+=1
如果球开始-球半径=显示高度:
bally_change=-bally_change
myScore+=1
#决定球员的位置
立方体(x,y,侧面)
#分数和生命计数器
分数(myScore)
生活(myLives)
#深渊之死
如果x>显示宽度或x<-50:
myLives-=1
死亡()
#死神

如果球员死亡时ball\u startx+ball\u radius>=x,ball\u startx-ball\u radius=y,ball\u starty-ball\u radius,则称之为死亡()

dead()
调用
消息显示()

message\u display()
再次调用
\u init\u()
(即使它已经在运行)

\uuuu init\uuuu()
的开头表示
myLives=3


如果您不想在玩家死亡时将变量重置为3,请不要再次调用
\uuu init\uuu
,因为该函数的开始总是将
myLives
重置为3。

当玩家死亡时,您调用
dead()

dead()
调用
消息显示()

message\u display()
再次调用
\u init\u()
(即使它已经在运行)

\uuuu init\uuuu()
的开头表示
myLives=3


如果您不想在玩家死亡时将变量重置为3,请不要再次继续调用
\uu init\uuuu
,因为该函数的开头总是将
myLives
重置为3。

dead()调用message\u display(),再次调用您的init()。在每次init()调用时,您都会再次设置myLifes=3

dead()调用message_display(),它再次调用您的init()。在每次init()调用时,您都会再次设置myLifes=3

堆栈溢出不是免费的调试服务。请将代码简化为最小的可重复示例,也许您会发现您的错误:)堆栈溢出不是免费的调试服务。请将代码简化为最小的可重复示例,也许您会发现您的错误:)