我的python代码一直存在问题,我正在使用pygame,random和math模块。我似乎无法在玩家健康栏上减少我的玩家的生命值之一。玩家健康栏固定在指定玩家的x和y位置,并且在其他玩家射击时不会降低玩家健康。
我已经尝试过在互联网上寻找答案,但没有找到答案。
import OpenGL
import panda3d
import pygame
import pyglet
import tkinter
import time
import random
import math
pygame.init()
display_width = 1250
display_height = 650
map_width = display_width*3
map_height = display_height*3
green = (0, 255, 0)
yellow = (255, 255, 0)
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
blue = (0, 0, 255)
person_height = 113
person_width = 171
person2_width = 171
person2_height = 113
gameDisplay = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('Wert')
clock = pygame.time.Clock()
personImg = pygame.image.load('Untitled7.png').convert_alpha()
personImg2 = pygame.image.load('Untitled8.png').convert_alpha()
bulletImg = pygame.image.load('Untitled9.png').convert_alpha()
background = pygame.image.load('Untitled4.png')
player_health = 100
player2_health = 100
x = map_width/2
y = map_height/2 - 56.5
ny = map_height/2 + 56.5
nx = map_width/2
#x = (0)
#y = (display_height * 0.37)
#ny = (display_height * 0.37)
#nx = (display_width * 0.87)
#thing_width = 100
#thing_height = 100
bullets1 = pygame.sprite.Group()
class Player(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.original_image = personImg
self.image = self.original_image.copy()
self.rect = self.image.get_rect(center=(person_width/2, person_height/2))
self.angle = 0
self.rect.x = x
self.rect.y = y
self.player_health = 100
self.x_change = 0
self.y_change = 0
def update(self):
self.x_change = 0
self.y_change = 0
keystate = pygame.key.get_pressed()
if keystate[pygame.K_w] and pygame.key.get_mods() and pygame.KMOD_LSHIFT and not pygame.sprite.collide_rect(self, player2): #and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width / 2, player2.rect.y + person2_height) and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width / 4, player2.rect.y + person2_height) and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width - 10, player2.rect.y + person2_height) and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width/2, player2.rect.y) and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width/4, player2.rect.y) and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width - 10, player2.rect.y) and not pygame.Rect.collidepoint(self.rect, player2.rect.x, player2.rect.y + person2_height/2) and not pygame.Rect.collidepoint(self.rect, player2.rect.x, player2.rect.y + person2_height/4) and not pygame.Rect.collidepoint(self.rect, player2.rect.x, player2.rect.y + person2_height - 10) and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width, player2.rect.y + person2_height/2) and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width, player2.rect.y + person2_height/4) and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width, player2.rect.y + person2_height - 10):
self.rect.x += math.cos(math.radians(self.angle)) * 10
self.rect.y += math.sin(math.radians(self.angle)) * -10
elif keystate[pygame.K_w] and not pygame.sprite.collide_rect(self, player2): #and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width/2, player2.rect.y + person2_height) and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width/4, player2.rect.y + person2_height) and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width - 10, player2.rect.y + person2_height) and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width/2, player2.rect.y) and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width/4, player2.rect.y) and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width - 10, player2.rect.y) and not pygame.Rect.collidepoint(self.rect, player2.rect.x, player2.rect.y + person2_height/2) and not pygame.Rect.collidepoint(self.rect, player2.rect.x, player2.rect.y + person2_height/4) and not pygame.Rect.collidepoint(self.rect, player2.rect.x, player2.rect.y + person2_height - 10) and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width, player2.rect.y + person2_height/2) and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width, player2.rect.y + person2_height/4) and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width, player2.rect.y + person2_height - 10):
self.rect.x += math.cos(math.radians(self.angle)) * 5
self.rect.y += math.sin(math.radians(self.angle)) * -5
if keystate[pygame.K_s] and not pygame.sprite.collide_rect(self, player2): #and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width/2, player2.rect.y) and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width/4, player2.rect.y) and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width - 10, player2.rect.y) and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width/2, player2.rect.y + person2_height) and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width/4, player2.rect.y + person2_height) and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width - 10, player2.rect.y + person2_height) and not pygame.Rect.collidepoint(self.rect, player2.rect.x, player2.rect.y + person2_height/2) and not pygame.Rect.collidepoint(self.rect, player2.rect.x, player2.rect.y + person2_height/4) and not pygame.Rect.collidepoint(self.rect, player2.rect.x, player2.rect.y + person2_height - 10) and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width, player2.rect.y + person2_height/2) and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width, player2.rect.y + person2_height/4) and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width, player2.rect.y + person2_height - 10):
self.rect.x += math.cos(math.radians(self.angle)) * -5
self.rect.y += math.sin(math.radians(self.angle)) * 5
if pygame.sprite.collide_rect(self, player2):
self.x_change += 7
self.y_change += 7
#if keystate[pygame.K_d] and pygame.key.get_mods() and pygame.KMOD_LSHIFT and not pygame.Rect.collidepoint(self.rect, player2.rect.x, player2.rect.y + person2_height/2) and not pygame.Rect.collidepoint(self.rect, player2.rect.x, player2.rect.y + person2_height/4) and not pygame.Rect.collidepoint(self.rect, player2.rect.x, player2.rect.y + person2_height - 10):
#self.x_change = 10
#elif keystate[pygame.K_d] and not pygame.Rect.collidepoint(self.rect, player2.rect.x, player2.rect.y + person2_height/2) and not pygame.Rect.collidepoint(self.rect, player2.rect.x, player2.rect.y + person2_height/4) and not pygame.Rect.collidepoint(self.rect, player2.rect.x, player2.rect.y + person2_height - 10):
#self.x_change = 5
#if keystate[pygame.K_a] and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width, player2.rect.y + person2_height/2) and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width, player2.rect.y + person2_height/4) and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width, player2.rect.y + person2_height - 10):
#self.x_change = -5
self.rect.x += self.x_change
self.rect.y += self.y_change
if self.rect.right > map_width:
self.rect.right = map_width
if self.rect.left < 0:
self.rect.left = 0
if self.rect.top < 0:
self.rect.top = 0
if self.rect.bottom > map_height:
self.rect.bottom = map_height
self.rect.contains(healthbar1.rect)
self.image.blit(healthbar1.image, (self.rect.x, self.rect.y))
healthbar1.rect.clamp_ip(self.rect)
def shoot(self):
bullet = Bullet1(self.rect.x, self.rect.y)
all_sprites.add(bullet)
bullets.add(bullet)
def modifyRotation(self):
self.angle += 5
def modifyRotation1(self):
self.angle += -5
def getRotation(self):
return self.angle
def getRotation1(self):
return self.angle
class Healthbar1(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
player_health = 100
for bullet1 in bullets1:
if pygame.sprite.collide_rect(bullet1, player):
bullet1.kill()
player_health -= 5
break
if player_health == 0 or player_health <= 0:
died()
if player_health > 75:
player_health_color = green
elif player_health > 50:
player_health_color = yellow
else:
player_health_color = red
self.image = pygame.Surface((player_health, 25))
self.rect = self.image.get_rect()
self.image.fill(player_health_color)
class Combined(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.rect = player.rect
self.rect.contains(healthbar1.rect)
self.image = player.image
self.image.blit(healthbar1.image, (player.rect.x, player.rect.y))
class Player2(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.original_image = personImg2
self.image = self.original_image.copy()
self.rect = self.image.get_rect(center=(person2_width / 2, person2_height / 2))
self.angle = 0
self.rect.x = nx
self.rect.y = ny
self.player2_health = 100
self.x_change1 = 0
self.y_change1 = 0
def update(self):
self.x_change1 = 0
self.y_change1 = 0
keystate = pygame.key.get_pressed()
if keystate[pygame.K_UP] and pygame.key.get_mods() and pygame.KMOD_RSHIFT and not pygame.sprite.collide_rect(self, player): #and not pygame.Rect.collidepoint(self.rect, player.rect.x + person_width/2, player.rect.y + person_height) and not pygame.Rect.collidepoint(self.rect, player.rect.x + person_width/4, player.rect.y + person_height) and not pygame.Rect.collidepoint(self.rect, player.rect.x + person_width - 10, player.rect.y + person_height):
self.rect.x += math.cos(math.radians(self.angle)) * -10
self.rect.y += math.sin(math.radians(self.angle)) * 10
elif keystate[pygame.K_UP] and not pygame.sprite.collide_rect(self, player): #and not pygame.Rect.collidepoint(self.rect, player.rect.x + person_width/2, player.rect.y + person_height) and not pygame.Rect.collidepoint(self.rect, player.rect.x + person_width/4, player.rect.y + person_height) and not pygame.Rect.collidepoint(self.rect, player.rect.x + person_width - 10, player.rect.y + person_height):
self.rect.x += math.cos(math.radians(self.angle)) * -5
self.rect.y += math.sin(math.radians(self.angle)) * 5
if keystate[pygame.K_DOWN] and not pygame.sprite.collide_rect(self, player): #and not pygame.Rect.collidepoint(self.rect, player.rect.x + person_width/2, player.rect.y) and not pygame.Rect.collidepoint(self.rect, player.rect.x + person_width/4, player.rect.y) and not pygame.Rect.collidepoint(self.rect, player.rect.x + person_width - 10, player.rect.y):
self.rect.x += math.cos(math.radians(self.angle)) * 5
self.rect.y += math.sin(math.radians(self.angle)) * -5
if pygame.sprite.collide_rect(self, player):
self.x_change1 += -7
self.y_change1 += -7
#if keystate[pygame.K_LEFT] and pygame.key.get_mods() and pygame.KMOD_RSHIFT and not pygame.Rect.collidepoint(self.rect, player.rect.x + person_width, player.rect.y + person_height/2) and not pygame.Rect.collidepoint(self.rect, player.rect.x + person_width, player.rect.y + person_height/4) and not pygame.Rect.collidepoint(self.rect, player.rect.x + person_width, player.rect.y + person_height - 10):
#self.x_change1 = -10
#elif keystate[pygame.K_LEFT] and not pygame.Rect.collidepoint(self.rect, player.rect.x + person_width, player.rect.y + person_height/2) and not pygame.Rect.collidepoint(self.rect, player.rect.x + person_width, player.rect.y + person_height/4) and not pygame.Rect.collidepoint(self.rect, player.rect.x + person_width, player.rect.y + person_height - 10):
#self.x_change1 = -5
#if keystate[pygame.K_RIGHT] and not pygame.Rect.collidepoint(self.rect, player.rect.x, player.rect.y + person_height/2) and not pygame.Rect.collidepoint(self.rect, player.rect.x, player.rect.y + person_height/4) and not pygame.Rect.collidepoint(self.rect, player.rect.x, player.rect.y + person_height - 10):
#self.x_change1 = 5
self.rect.x += self.x_change1
self.rect.y += self.y_change1
if self.rect.right > map_width:
self.rect.right = map_width
if self.rect.left < 0:
self.rect.left = 0
if self.rect.top < 0:
self.rect.top = 0
if self.rect.bottom > map_height:
self.rect.bottom = map_height
def shoot(self):
bullet = Bullet(self.rect.x, self.rect.y)
all_sprites.add(bullet)
bullets1.add(bullet)
def modifyRotation(self):
self.angle += 5
def modifyRotation1(self):
self.angle += -5
def getRotation(self):
return self.angle
def getRotation1(self):
return self.angle
all_sprites = pygame.sprite.Group()
player = Player()
player2 = Player2()
healthbar1 = Healthbar1()
combined = Combined()
#all_sprites.add(combined)
all_sprites.add(healthbar1)
bullets = pygame.sprite.Group()
bullets1 = pygame.sprite.Group()
class Bullet(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((20, 10))
self.mask = pygame.mask.from_surface(gameDisplay)
self.image.fill(black)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
def update(self):
#point = pygame.math.Vector2D(player.rect.x, player.rect.y + person_height/2)
#point1 = pygame.math.Vector2D(player.rect.x + person_width, player.rect.y + person_height/2)
#vector = point1 - point
#self.rect.x += vector
#self.rect.y += vector
#self.rect.x += self.x_change
#self.rect.y += self.y_change
if self.rect.left > map_width:
self.kill()
if self.rect.right < 0:
self.kill()
for angle in [0, 30, 45, 60, 0 + 90, 30 + 90, 45 + 90, 60 + 90, 0 + 180, 30 + 180, 45 + 180, 60 + 180, 0 + 270,
30 + 270, 45 + 270, 60 + 270]:
bullet_speed = 5
self.rect.x += -bullet_speed * math.cos(math.radians(player2.angle))
self.rect.y += bullet_speed * math.sin(math.radians(player2.angle))
class Bullet1(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((20, 10))
self.mask = pygame.mask.from_surface(gameDisplay)
self.image.fill(black)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
def update(self):
if self.rect.left > map_width:
self.kill()
if self.rect.right < 0:
self.kill()
for angle in [0, 30, 45, 60, 0 + 90, 30 + 90, 45 + 90, 60 + 90, 0 + 180, 30 + 180, 45 + 180, 60 + 180, 0 + 270,
30 + 270, 45 + 270, 60 + 270]:
bullet_speed1 = 5
self.rect.x += bullet_speed1 * math.cos(math.radians(player.angle))
self.rect.y += -bullet_speed1 * math.sin(math.radians(player.angle))
class Map(pygame.sprite.Sprite):
def __init__(self, map_width, map_height):
pygame.sprite.Sprite.__init__(self)
self.image = background
self.rect = pygame.Rect(0, 0, map_width, map_height)
self.width = map_width
self.height = map_height
class Camera(pygame.sprite.Sprite):
def __init__(self, display_height, display_width):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((display_width, display_height))
self.camera = pygame.Rect(0, 0, display_width, display_height)
self.rect = pygame.Rect(0, 0, display_width, display_height)
self.rect.x = x
self.rect.y = y
self.width = display_width
self.height = display_height
def apply(self, entity):
return entity.rect.move(self.camera.topleft)
def update(self):
x = (-player.rect.x + -player2.rect.x)/2 + int(display_width/2)
y = (-player.rect.y + -player2.rect.y)/2 + int(display_height/2)
self.camera = pygame.Rect(x, y, self.width, self.height)
bullets = pygame.sprite.Group()
players1 = pygame.sprite.Group()
players2 = pygame.sprite.Group()
bullet1 = Bullet1(x, y)
players1.add(player)
players2.add(player2)
map = Map(map_width, map_height)
camera = Camera(display_height, display_width)
bullet = Bullet(x, y)
all_sprites.add(player)
all_sprites.add(player2)
def button(msg,x,y,w,h,ic,ac,action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(gameDisplay, ac,(x,y,w,h))
if click[0] == 1 and action != None:
action()
else:
pygame.draw.rect(gameDisplay, ic,(x,y,w,h))
smallText = pygame.font.SysFont("timesnewromanms",20)
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ( (x+(w/2)), (y+(h/2)) )
gameDisplay.blit(textSurf, textRect)
def died():
GameOver = True
while GameOver:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameDisplay.fill(white)
redrawgame()
largeText = pygame.font.SysFont("timesnewromanms", 115)
TextSurf, TextRect = text_objects("Player 2 wins", largeText)
TextRect.center = ((display_width / 2), (display_height / 2))
gameDisplay.blit(TextSurf, TextRect)
button("Play Again", 500, 400, 100, 50, blue, blue, game_loop)
button("Quit", 700, 400, 100, 50, red, red, quitgame)
pygame.display.update()
clock.tick(15)
def died1():
GameOver = True
while GameOver:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameDisplay.fill(white)
redrawgame()
largeText = pygame.font.SysFont("timesnewromanms", 115)
TextSurf, TextRect = text_objects("Player 1 wins", largeText)
TextRect.center = ((display_width / 2), (display_height / 2))
gameDisplay.blit(TextSurf, TextRect)
button("Play Again", 500, 400, 100, 50, blue, blue, game_loop)
button("Quit", 700, 400, 100, 50, red, red, quitgame)
pygame.display.update()
clock.tick(15)
def newRotate():
keystate = pygame.key.get_pressed()
if keystate[pygame.K_a]:
player.modifyRotation()
player.image = player.original_image.copy()
player.image = pygame.transform.rotate(player.image, player.getRotation())
player.rect = player.image.get_rect(center=player.rect.center)
elif keystate[pygame.K_d]:
player.modifyRotation1()
player.image = player.original_image.copy()
player.image = pygame.transform.rotate(player.image, player.getRotation1())
player.rect = player.image.get_rect(center=player.rect.center)
if keystate[pygame.K_LEFT]:
player2.modifyRotation()
player2.image = player2.original_image.copy()
player2.image = pygame.transform.rotate(player2.image, player2.getRotation())
player2.rect = player2.image.get_rect(center=player2.rect.center)
elif keystate[pygame.K_RIGHT]:
player2.modifyRotation1()
player2.image = player2.original_image.copy()
player2.image = pygame.transform.rotate(player2.image, player2.getRotation1())
player2.rect = player2.image.get_rect(center=player2.rect.center)
def redrawgame():
player.angle = 0
player2.angle = 0
player.rect.x = x
player.rect.y = y
player2.rect.x = nx
player2.rect.y = ny
def quitgame():
pygame.quit()
quit()
def text_objects(text, font):
textSurface = font.render(text, True, black)
return textSurface, textSurface.get_rect()
def game_loop():
x = (0)
y = (display_height * 0.37)
ny = (display_height * 0.37)
nx = (display_width * 0.87)
y_change = 0
x_change = 0
x_change1 = 0
y_change1 = 0
bullet1 = Bullet1(x, y)
bullet = Bullet(x, y)
#thing_startx = display_width/2
#thing_starty = display_height/2
#thing_startx1 = display_width / 2 - thing_width
#thing_starty1 = display_height / 2 - thing_height
#thing_startx2 = display_width / 2 + thing_width + 50
#thing_starty2 = display_height / 2 + 50
#thing_startx3 = display_width - 500
#thing_starty3 = display_height - 450
#thing_startx4 = display_width - 950
#thing_starty4 = display_height - 350
#thing_startx5 = display_width - 800
#thing_starty5 = display_height - 250
gameExit = False
player_health = 100
player2_health = 100
for bullet in bullets:
bullet.kill()
for bullet in bullets1:
bullet.kill()
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
keystate = pygame.key.get_pressed()
keystate1 = pygame.mouse.get_pressed()
if keystate[pygame.K_SPACE]:
player.shoot()
if keystate1[2]:
player2.shoot()
y += y_change
x += x_change
ny += y_change1
nx += x_change1
gameDisplay.fill(white)
#block = pygame.draw.rect(gameDisplay, black, (thing_startx, thing_starty, thing_width, thing_height))
#block1 = pygame.draw.rect(gameDisplay, black, (thing_startx1, thing_starty1, thing_width, thing_height))
#block2 = pygame.draw.rect(gameDisplay, black, (thing_startx2, thing_starty2, thing_width, thing_height))
#block3 = pygame.draw.rect(gameDisplay, black, (thing_startx3, thing_starty3, thing_width, thing_height))
#block4 = pygame.draw.rect(gameDisplay, black, (thing_startx4, thing_starty4, thing_width, thing_height))
#block5 = pygame.draw.rect(gameDisplay, black, (thing_startx5, thing_starty5, thing_width, thing_height))
#block
#block1
#block2
#block3
#block4
#block5
player
player2
Camera(map_width, map_height)
#if x > thing_startx and x < thing_startx + thing_width or x + person_width > thing_startx and x + person_width < thing_startx + thing_width:
#hit()
#if thing_starty > display_height:
#thing_starty = 0 - thing_height
#thing_startx = random.randrange(0, display_width)
#if pygame.Rect.colliderect(block, bullet) or pygame.Rect.colliderect(block1, bullet) or pygame.Rect.colliderect(block2, bullet) or pygame.Rect.colliderect(block3, bullet) or pygame.Rect.colliderect(block4, bullet) or pygame.Rect.colliderect(block5, bullet):
#bullet.kill()
#if pygame.Rect.colliderect(block, bullet1) or pygame.Rect.colliderect(block1, bullet1) or pygame.Rect.colliderect(block2, bullet1) or pygame.Rect.colliderect(block3, bullet1) or pygame.Rect.colliderect(block4, bullet1) or pygame.Rect.colliderect(block5, bullet1):
#bullet1.kill()
for bullet1 in bullets1:
if pygame.sprite.collide_rect(bullet1, player):
bullet1.kill()
player_health -= 5
break
if player_health == 0 or player_health <= 0:
died()
for bullet in bullets:
if pygame.sprite.collide_rect(bullet, player2):
bullet.kill()
player2_health -= 5
break
if player2_health == 0 or player2_health <= 0:
died1()
#zombie.move_towards_player(player)
#zombie1.move_towards_player(player)
#zombie2.move_towards_player2(player2)
#zombie3.move_towards_player2(player2)
newRotate()
all_sprites.update()
camera.update()
gameDisplay.blit(background, (0,0))
for sprite in all_sprites:
gameDisplay.blit(sprite.image, camera.apply(sprite))
#all_sprites.draw(gameDisplay)
pygame.display.flip()
pygame.display.update()
clock.tick(60)
game_loop()
pygame.quit()
quit()
答案 0 :(得分:2)
HealthBar
类仅在__init__()
内计算其位置一次,因此它将永远不会更新。
也许它需要一个update()
函数来重新生成它?
class Healthbar1(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.player_health = 100
self.drawn_health = 0 # don't regenerate unless it's changed
self.update() # generate the first bar
def change(self, new_health):
self.player_health = new_health
def update(self):
# DO THIS ALL SOMEWHERE ELSE, IT'S NOT PART OF A HEALTH-BAR
#for bullet1 in bullets1:
# if pygame.sprite.collide_rect(bullet1, player):
# bullet1.kill()
# player_health -= 5
# break
#if player_health == 0 or player_health <= 0:
# died()
# Only re-make the bar if it's different from last time
if self.player_health != self.drawn_health:
if self.player_health > 75:
player_health_color = green
elif self.player_health > 50:
player_health_color = yellow
else:
player_health_color = red
self.image = pygame.Surface((self.player_health, 25))
self.rect = self.image.get_rect()
self.image.fill(player_health_color)
self.drawn_health = self.player_health