gpt4 book ai didi

python - Pygame 弹雨碰撞和残像

转载 作者:行者123 更新时间:2023-12-02 02:21:16 24 4
gpt4 key购买 nike

我正在制作一个简单的游戏,其中有一个敌​​人,你可以射击,并且你可以在一定程度上从左到右移动。这样做之后,我想当敌人的生命值大于 70 时,你必须躲避一波子弹。但我发现它有 3 个问题,一是它有残像,不是一颗子弹从它上面下来,就像把它一直画下来一样。两个我显示了文本,如果点击它应该将该文本更改为“你迷失了”,但这不起作用。三,即使我输入代码,它应该在 >+ 70 生命值时触发,在 70 生命值时它会产生子弹,它们停留在顶部,然后当我尝试射击时,它们就会落下。抱歉,如果这个问题很多,但我不知道如何解决。这是我的代码:

import pygame
import time
pygame.font.init()
WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Boss Fight")
FPS = 60
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
BLUE = (0, 0, 255)
RED = (255,0,0)
FLOOR = pygame.Rect(0, 400, 900, 100)
SPEED = 1
BULLET_SPEED = 1
player = pygame.Rect(50, 350, 50, 50)
numJumps = []
MAXBULLETS = 2
BOSS = pygame.Rect(700, 300, 100, 100)
FONT = pygame.font.SysFont('comicsans', 30)
BOSSHEALTH = 100
winText = ""
RESTART = pygame.USEREVENT + 1
LOST = pygame.USEREVENT + 2


def draw_window(playerBullets, rainBullets):
WIN.fill(WHITE)
pygame.draw.rect(WIN, BLACK, FLOOR)
pygame.draw.rect(WIN, BLUE, player)
pygame.draw.rect(WIN, RED, BOSS)
boss_health = FONT.render("BOSS HEALTH: " + str(BOSSHEALTH), 1,BLACK)
WIN.blit(boss_health, (450, 50))
won = FONT.render(winText,1,BLACK)
WIN.blit(won, (100, 50))
for bullet in playerBullets:
pygame.draw.rect(WIN, BLACK, bullet)
for b in rainBullets:
pygame.draw.rect(WIN, BLACK, b)
pygame.display.update()




def movement(p):
keys_helds = pygame.key.get_pressed()
if keys_helds[pygame.K_a] and p.x + SPEED > 0:
p.x -= SPEED
if keys_helds[pygame.K_d] and p.x < 400 + p.width:
p.x += SPEED

def p_bullet_movement(playerBullets, boss, raining, player, rainBullets):
for bullet in playerBullets:
bullet.x += BULLET_SPEED
if boss.colliderect(bullet):
global BOSSHEALTH
BOSSHEALTH-= 10
playerBullets.remove(bullet)
if BOSSHEALTH <= 70:
global winText
winText = "Bullet Rain!"
raining = True
for b in rainBullets:
b.y += 1
if player.colliderect(b):
pygame.event.post(pygame.event.Event(LOST))
rainBullets.remove(b)
if b.y + BULLET_SPEED > HEIGHT:
rainBullets.remove(b)
bullet1 = pygame.Rect(20, 0, 40, 25)
bullet2 = pygame.Rect(170, 0, 40, 25)
bullet3 = pygame.Rect(370, 0, 40, 25)
bullet4 = pygame.Rect(570, 0, 40, 25)
rainBullets.append(bullet1)
rainBullets.append(bullet2)
rainBullets.append(bullet3)
rainBullets.append(bullet4)

if BOSSHEALTH == 0:
winText = "You Won!"





def main():
playerBullets = []
raining = False
rainBullets = []
clock = pygame.time.Clock()
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE and not(raining):
bullet = pygame.Rect(player.x, player.y, 40, 25)
playerBullets.append(bullet)
if event.type == LOST:
global winText
wintext = "You Lost!"


draw_window(playerBullets, rainBullets)
movement(player)
p_bullet_movement(playerBullets, BOSS, raining, player, rainBullets)
clock.tick(FPS)



if __name__ == '__main__':
main()

最佳答案

one it has an afterimage, its not a single bullet coming down it like draw it all the way down.

这是因为您每帧为每个玩家子弹创建 4 个新子弹。

Two I have text displayed and if hit it is supposed to change that text to You Lost and that doesn't work.

那是因为你设置了wintext ,不是winText这里:

    if event.type == LOST:
global winText
wintext = "You Lost!"

Three even though I put in the code it should trigger at >+ 70 health at 70 health ...

不,您检查 <= 70,而不是 > 70:

...
if BOSSHEALTH <= 70:
...

it spawns the bullets and they stay at the top then when I try to shoot is when they come down.

看看您在此处缩进代码的方式:

def p_bullet_movement(playerBullets, boss, raining, player, rainBullets):
for bullet in playerBullets:
bullet.x += BULLET_SPEED
if boss.colliderect(bullet):
global BOSSHEALTH
BOSSHEALTH-= 10
playerBullets.remove(bullet)
if BOSSHEALTH <= 70:
global winText
winText = "Bullet Rain!"
raining = True
for b in rainBullets:
b.y += 1
...

您移动 rainBullets 中的每个对象对于每个 bulletplayerBullets如果BOSSHEALTH <= 70 .

所以如果 BOSSHEALTH <= 70不正确或者 playerBullets 中没有对象,for b in rainBullets:永远不会到达线。

关于python - Pygame 弹雨碰撞和残像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66387193/

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