pygame中玩家进化成方块时无法发射子弹

问题描述

我正在 pygame 中制作形状防御游戏。你向敌人发射子弹并在每次获得 100 分时进化。可是,进化到方格的时候就不能开火了?有人知道为什么会这样吗?

这是我当前的代码

import pygame
import random
from pygame.locals import *
from pygame.color import THECOLORS

pygame.init()
pygame.font.init()

screen = pygame.display.set_mode((800,600))
pygame.display.set_caption("Shape Defense")


class Sprite(pygame.sprite.Sprite):
    """Base class for all sprites"""

    def __init__(self,window,x,y,image,size=None):
        pygame.sprite.Sprite.__init__(self)

        self.screen = window
        self.x = x
        self.y = y
        self.image = pygame.image.load(image)

        if size is not None:
            self.image = pygame.transform.scale(self.image,size)

        self.rect = self.image.get_rect()
        self.rect.center = (self.x,self.y)

    def update(self):
        self.rect.center = (self.x,self.y)

        if isinstance(self.image,pygame.Surface):
            self.screen.blit(self.image,self.rect)
        elif isinstance(self.image,str):
            self.screen.blit(pygame.image.load(self.image),self.rect)


class polygon(Sprite):
    """Is it going to be a triangle,a square,a pentagon,or something else?"""

    def __init__(self,sides,size=None):
        self.sides = sides
        self.window = window
        self.x = x
        self.y = y
        self.check_image()

        Sprite.__init__(self,self.window,self.x,self.y,self.image,size)

    def check_for_movement(self,move_amount=1):
        """Let's get moving!"""

        key = pygame.key.get_pressed()

        if key[K_w]:
            self.y -= move_amount
        if key[K_s]:
            self.y += move_amount
        if key[K_a]:
            self.x -= move_amount
        if key[K_d]:
            self.x += move_amount

    def evolve(self):
        self.sides += 1
        self.check_image()

    def check_image(self):
        if self.sides == 3:  # Is it a triangle?
            self.image = "C:/Users/linpang2018/Desktop/TRASHY JUNK/PICTURES/FOR PYGAME GAMES/triangle.png"
        elif self.sides == 4:
            self.image = "C:/Users/linpang2018/Desktop/TRASHY JUNK/PICTURES/FOR PYGAME GAMES/square.png"


class Bullet(polygon):
    """
    I guess I need to change my mind about thinking no shape
    games have bullets
    """

    def __init__(self,size=None):
        self.sides = sides
        self.screen = window
        self.x = x
        self.y = y

        polygon.__init__(self,self.sides,self.screen,size)

    def update(self):
        """Bombs away!"""

        self.y -= 10
        self.rect.center = (self.x,self.y)

        if self.y < 0:
            self.kill()


class Enemy(polygon):
    """This game even has enemies?!"""

    def __init__(self,size)

    def update(self):
        """Prepare yourself! A heavy attack is incoming! -the enemies"""

        self.y += 4
        self.rect.center = (self.x,self.y)

        if self.y > 650:
            self.kill()


class Image(Sprite):
    """Base class for all buttons and images that cover the whole screen"""

    def __init__(self,image):
        self.window = window
        self.x = x
        self.y = y
        filename = image

        Sprite.__init__(self,filename)


bullet_group = pygame.sprite.Group()
enemy_group = pygame.sprite.Group()

player = polygon(3,screen,400,size=(285,200))
lives = 5
score = 0
collided = False

"""Creates score text"""
font = pygame.font.SysFont("Arial",30)
score_text_surface = font.render("score: {:,}".format(score),True,(0,0))

enemy_interval = 2000
enemy_event = USEREVENT + 1
pygame.time.set_timer(enemy_event,enemy_interval)

clock = pygame.time.Clock()
running = True

while running:
    """Event loop"""
    for event in pygame.event.get():
        if event.type == QUIT:
            running = False

        """These lines creates a bullet object when the player clicks the screen"""
        if event.type == MOUSEBUTTONDOWN:
            if player.sides == 3:
                bullet_group.add(Bullet(3,player.x + 4,player.y - 15,size=(170,100)))
            elif player.sides == 4:
                bullet_group.add(Bullet(3,100)))
        if event.type == enemy_event:
            enemy_group.add(Enemy(3,random.randint(0,900),50,size=(230,160)))

    screen.fill(THECOLORS["skyblue"])

    """Updates the player position and drawing the player"""
    player.update()
    player.check_for_movement(9)

    """Allows the bullets to appear and fire towards the top of the screen"""
    bullet_group.draw(screen)
    bullet_group.update()

    """
    Updates the enemy sprite(s) and allows the enemy to fly 
    toward the bottem of the screen
    """
    enemy_group.draw(screen)
    enemy_group.update()

    """Updates and blits the score text"""
    score_text_surface = font.render("score: {:,0))
    screen.blit(score_text_surface,(650,10))

    """This blits the game over image if it's game over"""
    if lives <= 0:
        game_over_bg = Image(screen,390,300,"C:/Users/linpang2018/Desktop/TRASHY JUNK/PICTURES/FOR PYGAME GAMES/game_over.png")
        screen.blit(game_over_bg.image,game_over_bg.rect)

    """If a bullet and a enemy collide,delete them both and increase score by 1"""
    for i in enemy_group:
        if pygame.sprite.spritecollide(i,bullet_group,True):
            score += 10
            i.kill()

    """If a enemy and the player collide,decrease lives by 1"""
    if pygame.sprite.spritecollide(player,enemy_group,True):
        lives -= 1

    """Evolves the player if every time score is a multiple of 100 (and it's not 0)"""
    if score % 100 == 0 and score != 0:
        player.evolve()

    pygame.display.update()
    clock.tick(40)

我认为这与 player.sides 有关,但可能是其他原因导致问题。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)