gpt4 book ai didi

python - 生命计数器不断重置

转载 作者:太空宇宙 更新时间:2023-11-03 19:56:45 25 4
gpt4 key购买 nike

我最近开始使用 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()

我有一个声明,当玩家死亡时,生命计数器会减少一。然而,生命计数器似乎重置为 3(原始数量)。我需要改变什么来修复生命计数器?谢谢!

最佳答案

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

关于python - 生命计数器不断重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59494003/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com