微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

pygame 项目中的项目符号不会在游戏重新启动时删除

如何解决pygame 项目中的项目符号不会在游戏重新启动时删除

我是 Python 和编码的新手,所以我正在关注这个初学者 tutorial,在 pygame 中制作一个两人太空入侵者类型的游戏。

游戏中机制的所有代码都可以正常工作。但是,每当我尝试通过调用 main() 函数重新启动游戏时,除了子弹之外的所有内容都会重置(这意味着在上一场比赛中发射的子弹会在下一场比赛中击中玩家)

这里是主要功能

def main():
    Player_One_HitBox = pygame.Rect(WIDTH/4-SPACESHIP_WIDTH,HEIGHT/2-SPACESHIP_HEIGHT,SPACESHIP_WIDTH,SPACESHIP_HEIGHT)
    Player_Two_HitBox = pygame.Rect(3*WIDTH/4-SPACESHIP_WIDTH,SPACESHIP_HEIGHT)
    Player_One_Bullets = []
    Player_Two_Bullets = []
    clock = pygame.time.Clock()
    run = True
    global Player_One_Health
    global Player_Two_Health
    Player_One_Health = 20
    Player_Two_Health = 20
    while run:
        clock.tick(FPS)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
                pygame.quit()
        #DO STUFF HERE
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE and len(Player_One_Bullets) < NUM_BULLETS:
                    bullet = pygame.Rect(Player_One_HitBox.x,Player_One_HitBox.y + Player_One_HitBox.height//2,20,5)
                    Player_One_Bullets.append(bullet)
                if event.key == pygame.K_KP0 and len(Player_Two_Bullets) < NUM_BULLETS:
                    bullet = pygame.Rect(Player_Two_HitBox.x,Player_Two_HitBox.y + Player_Two_HitBox.height//2,5)
                    Player_Two_Bullets.append(bullet)
            if event.type == PLAYER_ONE_HIT:
                Player_One_Health -= 1
            if event.type == PLAYER_TWO_HIT:
                Player_Two_Health -= 1
        winner_text = ""
        if Player_One_Health <= 0:
           winner_text = "Player One Wins!"
        if Player_Two_Health <= 0:
            winner_text = "Player Two Wins!"
        if winner_text != "":
            draw_winner(winner_text,Player_One_Bullets,Player_Two_Bullets)
            break

        keys_pressed = pygame.key.get_pressed()
        Player_One_Movement(keys_pressed,Player_One_HitBox)
        Player_Two_Movement(keys_pressed,Player_Two_HitBox)

        handle_bullets(Player_One_Bullets,Player_Two_Bullets,Player_One_HitBox,Player_Two_HitBox)

        draw_window(Player_One_HitBox,Player_Two_HitBox,Player_One_Health,Player_Two_Health)
    main()

为什么使用 Player_One_Bullets = []Player_Two_Bullets = [] 清空项目符号列表不会使项目符号消失?这是 pygame 的东西还是 Python 的东西?

完整代码

from PIL import Image
import random
import pygame
pygame.font.init()

#Pygame Parameters config
WIDTH,HEIGHT = 1600,800
WIN = pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption("Battle Stars")
SPACESHIP_WIDTH,SPACESHIP_HEIGHT = 55,55

BORDER = pygame.Rect(WIDTH//2 - 5,10,HEIGHT)

HEALTH_FONT = pygame.font.SysFont('comicsans',30)
WINNER_FONT = pygame.font.SysFont('conicsans',200)

FPS = 240

PLAYER_ONE_HIT = pygame.USEREVENT + 1
PLAYER_TWO_HIT = pygame.USEREVENT + 2

LOW_VEL = 1
MED_VEL = 5
HIGH_VEL = 15

RED = (255,0)
GREEN = (0,255,0)

NUM_BULLETS = 3

Player_One_Health = 20
Player_Two_Health = 20

Backdrop = pygame.image.load(r"C:\Users\Garrett\PycharmProjects\pythonProject\Backdrop.png").convert()


#Load Player Sprites from Sprite Sheet
Sprite_Sheet = Image.open(r"C:\Users\Garrett\PycharmProjects\pythonProject\Spaceships.png")
def Load_Sprite(row,column):
    sprite_x_coord = ((35*(column-1))+(10*(column-1)))+5
    sprite_y_coord = ((35*(row-1))+(10*(row-1)))+5
    crop_rectangle = (sprite_x_coord,sprite_y_coord,sprite_x_coord + 35,sprite_y_coord + 35)
    image = Sprite_Sheet.crop(crop_rectangle)
    return image

Player_One = Load_Sprite(random.randint(1,24),random.randint(1,24))
Player_Two = Load_Sprite(random.randint(1,24))

Player_One_Image = pygame.image.fromstring(Player_One.tobytes(),Player_One.size,Player_One.mode).convert()
Player_Two_Image = pygame.image.fromstring(Player_Two.tobytes(),Player_Two.size,Player_Two.mode).convert()

Player_One_Sprite = pygame.transform.rotate(pygame.transform.scale(Player_One_Image,(SPACESHIP_WIDTH,SPACESHIP_HEIGHT)),270)
Player_Two_Sprite = pygame.transform.rotate(pygame.transform.scale(Player_Two_Image,90)

def draw_window(Player_One_HitBox,Player_Two_Health):
    WIN.blit(Backdrop,(0,0))
    WIN.blit(Backdrop,(853,480))
    WIN.blit(Backdrop,480))

    Player_One_Health_Text = HEALTH_FONT.render("Health:" + str(Player_One_Health),1,(255,255))
    Player_Two_Health_Text = HEALTH_FONT.render("Health:" + str(Player_Two_Health),255))
    WIN.blit(Player_One_Health_Text,(10,20))
    WIN.blit(Player_Two_Health_Text,(WIDTH-Player_Two_Health_Text.get_width(),20))
    pygame.draw.rect(WIN,255),BORDER)
    WIN.blit(Player_One_Sprite,(Player_One_HitBox.x,Player_One_HitBox.y))
    WIN.blit(Player_Two_Sprite,(Player_Two_HitBox.x,Player_Two_HitBox.y))

    for bullet in Player_One_Bullets:
        pygame.draw.rect(WIN,RED,bullet)
    for bullet in Player_Two_Bullets:
        pygame.draw.rect(WIN,GREEN,bullet)
    pygame.display.update()

def Player_One_Movement(keys_pressed,Player_One_HitBox):
    if keys_pressed[pygame.K_s] and Player_One_HitBox.y < HEIGHT-50-MED_VEL:
        Player_One_HitBox.y += MED_VEL
    if keys_pressed[pygame.K_w] and Player_One_HitBox.y > MED_VEL:
        Player_One_HitBox.y -= MED_VEL
    if keys_pressed[pygame.K_a] and Player_One_HitBox.x > 0:
        Player_One_HitBox.x -= MED_VEL
    if keys_pressed[pygame.K_d] and Player_One_HitBox.x < (WIDTH/2)-SPACESHIP_WIDTH:
        Player_One_HitBox.x += MED_VEL

def Player_Two_Movement(keys_pressed,Player_Two_HitBox):
    if keys_pressed[pygame.K_DOWN] and Player_Two_HitBox.y < HEIGHT-50-MED_VEL:
        Player_Two_HitBox.y += MED_VEL
    if keys_pressed[pygame.K_UP] and Player_Two_HitBox.y > MED_VEL:
        Player_Two_HitBox.y -= MED_VEL
    if keys_pressed[pygame.K_LEFT] and Player_Two_HitBox.x > (WIDTH/2):
        Player_Two_HitBox.x -= MED_VEL
    if keys_pressed[pygame.K_RIGHT] and Player_Two_HitBox.x < WIDTH-SPACESHIP_WIDTH:
        Player_Two_HitBox.x += MED_VEL

def handle_bullets(Player_One_Bullets,Player_Two_HitBox):
    for bullets in Player_One_Bullets:
        bullets.x += HIGH_VEL
        if Player_Two_HitBox.colliderect(bullets):
            Player_One_Bullets.remove(bullets)
            pygame.event.post(pygame.event.Event(PLAYER_TWO_HIT))
        elif bullets.x > WIDTH:
            Player_One_Bullets.remove(bullets)
        for Green_Bullets in Player_Two_Bullets:
            if Green_Bullets.colliderect(bullets):
                try:
                    Player_Two_Bullets.remove(Green_Bullets)
                    Player_One_Bullets.remove(bullets)
                except:
                    pass

    for bullets in Player_Two_Bullets:
        bullets.x -= HIGH_VEL
        if Player_One_HitBox.colliderect(bullets):
            pygame.event.post(pygame.event.Event(PLAYER_ONE_HIT))
            Player_Two_Bullets.remove(bullets)
        elif bullets.x < 0:
            Player_Two_Bullets.remove(bullets)

def draw_winner(text,Player_Two_Bullets):
    draw_text = WINNER_FONT.render(text,0))
    WIN.blit(draw_text,(WIDTH//2 - draw_text.get_width()/2,HEIGHT/2 - draw_text.get_height()/2))
    pygame.display.update()
    pygame.time.delay(1000)

def main():
    Player_One_HitBox = pygame.Rect(WIDTH/4-SPACESHIP_WIDTH,Player_Two_Health)
    main()

if __name__ == "__main__":
    main()

解决方法

不要递归调用main(),而是循环调用它:

def main():
    # [...]

    while run:
        # [...]

    # main() <--- DELETE 

if __name__ == "__main__":

    while True:
        main()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。