Python 为什么可以';我的敌人healthbar在与子弹相撞后不会退缩吗?

Python 为什么可以';我的敌人healthbar在与子弹相撞后不会退缩吗?,python,pygame,Python,Pygame,所以我现在正在做一个游戏,热狗射出的子弹应该会伤害敌人。但在子弹与敌人碰撞后,表示其健康的红色健康条丝毫没有收缩。关于如何解决这个问题有什么建议吗?提前谢谢 import pygame import random import math # Screen parameters pygame.init() screen = pygame.display.set_mode((640, 480)) pygame.display.set_caption("SPPACCE") bg

所以我现在正在做一个游戏,热狗射出的子弹应该会伤害敌人。但在子弹与敌人碰撞后,表示其健康的红色健康条丝毫没有收缩。关于如何解决这个问题有什么建议吗?提前谢谢

import pygame
import random
import math

# Screen parameters
pygame.init()
screen = pygame.display.set_mode((640, 480))
pygame.display.set_caption("SPPACCE")
bg = pygame.image.load("bg.png")
font = pygame.font.SysFont('comicsans', 30, True)
clock = pygame.time.Clock()
score = 0

# Player parameters
class Player(object):
    def __init__(self, x, y, height, width):
        self.x = x
        self.y = y
        self.height = height
        self.width = width
        self.player_vel = 5

    def draw(self, screen):
        screen.blit(player_char, (self.x, self.y))
       

# Enemy parameters
class Enemy(object):
    def __init__(self, x, y, height, width, end):
        self.x = x
        self.y = y
        self.height = height
        self.width = width
        self.enemy_vel = 1.5
        self.end = end   
        self.path = [self.x, self.end]
        self.hitbox = (self.x + 17, self.y + 2, 65, 65)
        self.health = 10
        self.visible = True


    def draw(self, screen):
        self.move()
        if self.visible:
            self.hitbox = (self.x + 0, self.y, 65, 65)
            pygame.draw.rect(screen, (255, 0, 0), self.hitbox, 2)
            screen.blit(enemy_char, (self.x, self.y))

            # Health bars
            pygame.draw.rect(screen, (0, 0, 0), (self.hitbox[0], self.hitbox[1] - 20, 65, 10))
            pygame.draw.rect(screen, (255, 0, 0), (self.hitbox[0], self.hitbox[1] - 20, 65 - (6.5 * (10 - self.health)), 10))            
    
    def move(self):
        if self.enemy_vel > 0:
            if self.x < self.path[1] + self.enemy_vel:
                self.x += self.enemy_vel
            else:
                self.enemy_vel = self.enemy_vel * -1
                self.x += self.enemy_vel
        else:
            if self.x > self.path[0] - self.enemy_vel:
                self.x += self.enemy_vel
            else:
                self.enemy_vel = self.enemy_vel * -1
                self.x += self.enemy_vel

    def hit(self):
        if self.health > 0:
            self.health -= 1
        else: 
            self.visible = False

                
# Player Projectile parameters
class Projectile(object):
    def __init__(self, x, y, color, radius):
        self.x = x
        self.y = y
        self.color = color
        self.radius = radius
        self.vel = 12.5
    
    def draw(self, screen):
        pygame.draw.circle(screen, self.color, (self.x, self.y), self.radius)

# Images
player_char = pygame.image.load('sprites/hotdog.png')
enemy_char = pygame.image.load('sprites/hamburger.png')

def blit(): # This draws the sprites
    player.draw(screen)
    enemy.draw(screen)
    for projectile in projectiles:
        projectile.draw(screen)
    score_text = font.render("Score: " + str(score), 1, (0, 109, 255))
    version = font.render("Version 01 ", 1, (51, 153, 255))
    screen.blit(score_text, (0, 0))
    screen.blit(version, (520, 0))


shootloop = 0

if shootloop > 0:
    shootloop += 1
if shootloop > 2:
    shootloop = 0

player = Player(300, 400, 64, 64) 
enemy = Enemy(random.randint(10, 100), random.randint(20, 100), 64, 64, 480)
projectiles = []
run = True


while run:
    clock.tick(60)
    screen.fill((0, 0, 0))
    screen.blit(bg, (0, 0))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    # Movement keys with playeborders
    keys = pygame.key.get_pressed()
    if keys[pygame.K_s] and player.y < 480 - player.height - player.player_vel:
        player.y += player.player_vel
    if keys[pygame.K_w] and player.y > 280:
        player.y -= player.player_vel
    if keys[pygame.K_d] and player.x < 640 - player.width - player.player_vel:
        player.x += player.player_vel
    if keys[pygame.K_a] and player.x > player.player_vel:
        player.x -= player.player_vel
    
    for projectile in projectiles:
        if projectile.y - projectile.radius < enemy.hitbox[1] + enemy.hitbox[3] and projectile.y + projectile.radius > enemy.hitbox[1]:
            if projectile.x + projectile.radius > enemy.hitbox[0] and projectile.x - projectile.radius < enemy.hitbox[0] + enemy.hitbox[2]:
                score += 1
                projectiles.pop(projectiles.index(projectile))
                

        if projectile.y < 640 and projectile.y > 0:
            enemy.hit
            projectile.y -= projectile.vel
            
        else:
            projectiles.pop(projectiles.index(projectile))
    
    # Player shooting
    if keys[pygame.K_SPACE] and shootloop == 0:
        if len(projectiles) < 1:
            projectiles.append(Projectile(round(player.x + player.width //2), 
            round(player.y + player.height //2), [255, 150, 0], 7))
    
    blit()
    pygame.display.update()
导入pygame
随机输入
输入数学
#屏幕参数
pygame.init()
screen=pygame.display.set_模式((640480))
pygame.display.set_标题(“SPPACCE”)
bg=pygame.image.load(“bg.png”)
font=pygame.font.SysFont('comicsans',30,True)
clock=pygame.time.clock()
分数=0
#玩家参数
类播放器(对象):
定义初始值(自、x、y、高度、宽度):
self.x=x
self.y=y
自我高度=高度
self.width=宽度
self.player_vel=5
def提取(自身,屏幕):
blit(player_char,(self.x,self.y))
#敌方参数
类敌人(对象):
定义初始(自、x、y、高度、宽度、终点):
self.x=x
self.y=y
自我高度=高度
self.width=宽度
自身敌人等级=1.5
self.end=结束
self.path=[self.x,self.end]
self.hitbox=(self.x+17,self.y+2,65,65)
自我健康=10
self.visible=True
def提取(自身,屏幕):
self.move()
如果自可见:
self.hitbox=(self.x+0,self.y,65,65)
pygame.draw.rect(屏幕,(255,0,0),self.hitbox,2)
blit(敌方字符,(self.x,self.y))
#健康酒吧
pygame.draw.rect(屏幕,(0,0,0),(self.hitbox[0],self.hitbox[1]-20,65,10))
pygame.draw.rect(屏幕,(255,0,0),(self.hitbox[0],self.hitbox[1]-2065-(6.5*(10-自我健康)),10))
def移动(自我):
如果self.敌方等级>0:
如果self.xself.path[0]-self.敌方级别:
self.x+=self.敌军等级
其他:
self.敌军等级=self.敌军等级*-1
self.x+=self.敌军等级
def命中(自身):
如果self.health>0:
自我健康-=1
其他:
self.visible=False
#播放机射弹参数
类(对象):
定义初始值(自、x、y、颜色、半径):
self.x=x
self.y=y
self.color=颜色
自半径=半径
self.vel=12.5
def提取(自身,屏幕):
pygame.draw.circle(屏幕,self.color,(self.x,self.y),self.radius)
#图像
player_char=pygame.image.load('sprites/hotdog.png')
敌人_char=pygame.image.load('sprites/hamburger.png')
def blit():#这将绘制精灵
玩家。抽签(屏幕)
敌人。抽签(屏幕)
对于射弹中的射弹:
投射物。绘制(屏幕)
score_text=font.render(“分数:+str(分数),1,(0,109,255))
version=font.render(“版本01”,1,(51153255))
屏幕。blit(分数文本,(0,0))
屏幕blit(版本,(520,0))
shootloop=0
如果shootloop>0:
shootloop+=1
如果shootloop>2:
shootloop=0
玩家=玩家(300、400、64、64)
敌人=敌人(random.randint(10100),random.randint(20100),6464480)
射弹=[]
运行=真
运行时:
时钟滴答(60)
屏幕填充((0,0,0))
屏幕光点(背景,(0,0))
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
运行=错误
#带有播放顺序的移动键
keys=pygame.key.get_pressed()
如果键[pygame.K_s]和player.y<480-player.height-player.player_等级:
player.y+=player.player_等级
如果键[pygame.K_w]和player.y>280:
player.y-=player.player_等级
如果键[pygame.K_d]和player.x<640-player.width-player.player_级别:
player.x+=player.player_等级
如果键[pygame.K_a]和player.x>player.player_级别:
player.x-=player.player_等级
对于射弹中的射弹:
如果射弹.y-射弹.radius<敌方.hitbox[1]+敌方.hitbox[3]和射弹.y+射弹.radius>敌方.hitbox[1]:
如果射弹.x+射弹.radius>敌方.hitbox[0]和射弹.x-射弹.radius<敌方.hitbox[0]+敌方.hitbox[2]:
分数+=1
射弹.pop(射弹.指数(射弹))
如果射弹.y<640且射弹.y>0:
敌人,击中
射弹.y-=射弹.vel
其他:
射弹.pop(射弹.指数(射弹))
#运动员射击
如果键[pygame.K_SPACE]和shootloop==0:
如果len(射弹)<1:
投射物。附加(投射物(圆形(player.x+player.width//2),
圆形(player.y+player.height//2),[255,150,0,7))
blit()
pygame.display.update()

顺便说一句,我遵循了蒂姆的教程。另外,我认为这足以运行代码。

我不知道这是否是唯一的问题,但当您尝试调用hit方法时,缺少一组括号:

enemy.hit
应该是

enemy.hit()

您的代码有两个问题。正如@Apple所指出的,第一个问题是您没有正确地调用
敌方.hit()

另一个问题是没有在正确的位置调用敌方.hit()。它需要在检测到碰撞后调用,并且现有代码在投射物向上移动的位置有它:

for projectile in projectiles:
    if projectile.y - projectile.radius < enemy.hitbox[1] + enemy.hitbox[3] and projectile.y + projectile.radius > enemy.hitbox[1]:
        if projectile.x + projectile.radius > enemy.hitbox[0] and projectile.x - projectile.radius < enemy.hitbox[0] + enemy.hitbox[2]:
            score += 1
            projectiles.pop(projectiles.index(projectile))

    if projectile.y < 640 and projectile.y > 0:
        enemy.hit                                   # <<-- HERE
        projectile.y -= projectile.vel
    else:
        projectiles.pop(projectiles.index(projectile))

请不要忘记在代码中添加注释,它们真的很有帮助。

您能确认文章中的缩进与代码匹配吗?我认为这会在类声明之后抛出一个缩进错误。我假设它在实际代码中缩进了一级?否则,这将是金融机构的问题
for projectile in projectiles:
    if projectile.y - projectile.radius < enemy.hitbox[1] + enemy.hitbox[3] and projectile.y + projectile.radius > enemy.hitbox[1]:
        if projectile.x + projectile.radius > enemy.hitbox[0] and projectile.x - projectile.radius < enemy.hitbox[0] + enemy.hitbox[2]:
            # Enemy was hit by the projectile
            score += 1
            enemy.hit()                                 # <<-- HERE
            projectiles.pop(projectiles.index(projectile))

    if projectile.y < 640 and projectile.y > 0:
        # move the projectile up
        projectile.y -= projectile.vel
    else:
        # projectile went off-screen
        projectiles.pop(projectiles.index(projectile))