gpt4 book ai didi

python - 为什么我的敌人与子弹相撞后生命条不能缩小?

转载 作者:行者123 更新时间:2023-12-02 02:22:09 25 4
gpt4 key购买 nike

所以我目前正在制作一款游戏,其中热狗发射的子弹应该会伤害敌人。但在子弹与敌人碰撞之后,表示自身生命值的红色生命条却丝毫没有缩小。关于如何解决这个问题有什么建议吗?提前致谢。

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()

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

最佳答案

您的代码有两个问题。首先,正如 @Apple 指出的,您没有正确调用 enemy.hit()

另一个问题是 enemy.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))

如果将其更改为在射弹碰撞时应用 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]:
# 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))

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

关于python - 为什么我的敌人与子弹相撞后生命条不能缩小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66234617/

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