如何让玩家射击子弹

时间:2019-05-09 18:21:28

标签: python pygame

我一直在到处搜索,但是没有找到答案。问题是如何让玩家用枪射击子弹(bullet-image.jpg),该子弹会一直走到窗口宽度的尽头或击中敌人。

我尝试过这种方法-How to create bullets in pygame?,但是每当我按下分配用于发射子弹的键时,它都不会发射它们。我也尝试过类似此处的其他方法-https://www.youtube.com/watch?v=-5GNbL33hz0,但没有用。代码在下面给出

玩家类

class thePlayer(pygame.sprite.Sprite):

heroWalkRight = [pygame.image.load('HeroWalkRight/PenRunR0.png'), pygame.image.load('HeroWalkRight/PenRunR1.png'),
                 pygame.image.load('HeroWalkRight/PenRunR2.png'), pygame.image.load('HeroWalkRight/PenRunR3.png'),
                 pygame.image.load('HeroWalkRight/PenRunR4.png'), pygame.image.load('HeroWalkRight/PenRunR5.png'),
                 pygame.image.load('HeroWalkRight/PenRunR6.png'), pygame.image.load('HeroWalkRight/PenRunR7.png'),
                 pygame.image.load('HeroWalkRight/PenRunR8.png'), pygame.image.load('HeroWalkRight/PenRunR9.png'),
                 pygame.image.load('HeroWalkRight/PenRunR10.png'), pygame.image.load('HeroWalkRight/PenRunR11.png'),
                 pygame.image.load('HeroWalkRight/PenRunR12.png'), pygame.image.load('HeroWalkRight/PenRunR13.png'),
                 pygame.image.load('HeroWalkRight/PenRunR14.png'), pygame.image.load('HeroWalkRight/PenRunR15.png')]
heroWalkLeft = [pygame.image.load('HeroWalkLeft/PenRunL0.png'), pygame.image.load('HeroWalkLeft/PenRunL1.png'),
                pygame.image.load('HeroWalkLeft/PenRunL2.png'), pygame.image.load('HeroWalkLeft/PenRunL3.png'),
                pygame.image.load('HeroWalkLeft/PenRunL4.png'), pygame.image.load('HeroWalkLeft/PenRunL5.png'),
                pygame.image.load('HeroWalkLeft/PenRunL6.png'), pygame.image.load('HeroWalkLeft/PenRunL7.png'),
                pygame.image.load('HeroWalkLeft/PenRunL8.png'), pygame.image.load('HeroWalkLeft/PenRunL9.png'),
                pygame.image.load('HeroWalkLeft/PenRunL10.png'), pygame.image.load('HeroWalkLeft/PenRunL11.png'),
                pygame.image.load('HeroWalkLeft/PenRunL12.png'), pygame.image.load('HeroWalkLeft/PenRunL13.png'),
                pygame.image.load('HeroWalkLeft/PenRunL14.png'), pygame.image.load('HeroWalkLeft/PenRunL15.png')]

heroShootR = [pygame.image.load('HeroShootR/shot0.png'), pygame.image.load('HeroShootR/shot1.png'),
              pygame.image.load('HeroShootR/shot2.png'), pygame.image.load('HeroShootR/shot3.png'),
              pygame.image.load('HeroShootR/shot4.png'), pygame.image.load('HeroShootR/shot5.png'),
              pygame.image.load('HeroShootR/shot6.png'), pygame.image.load('HeroShootR/shot7.png')]

heroShootL = [pygame.image.load('HeroShootL/shot0.png'), pygame.image.load('HeroShootL/shot1.png'),
              pygame.image.load('HeroShootL/shot2.png'), pygame.image.load('HeroShootL/shot3.png'),
              pygame.image.load('HeroShootL/shot4.png'), pygame.image.load('HeroShootL/shot5.png'),
              pygame.image.load('HeroShootL/shot6.png'), pygame.image.load('HeroShootL/shot7.png')]

hero_charR = pygame.image.load('HeroStandChar/PStandStillR.png')
hero_charL = pygame.image.load('HeroStandChar/PStandStillL.png')

def __init__(self, x, y, width, height):
    pygame.sprite.Sprite.__init__(self)
    self.x = x
    self.y = y
    self.width = width
    self.height = height
    self.velocity = 5
    self.isJump = False
    self.jumpCount = 10
    self.left = False
    self.right = False
    self.shoot = False
    self.walkCount = 0
    self.shootCount = 0
    self.standing = True
    self.hitbox = (self.x + 20, self.y, 28, 60)
    self.image = pygame.image.load('HeroStandChar/PStandStillR.png')
    self.rect = self.image.get_rect()

def draw(self, window):
    if self.walkCount + 1 >= 15:
        self.walkCount = 0

    if self.shootCount + 1 >= 7:
        self.shootCount = 0

    if not self.standing:
        if self.left:
            window.blit(self.heroWalkLeft[self.walkCount], (self.x, self.y))
            self.walkCount += 1

        elif self.right:
            window.blit(self.heroWalkRight[self.walkCount], (self.x, self.y))
            self.walkCount += 1

    else:
        if self.left:
            if self.shoot:
                window.blit(self.heroShootL[self.shootCount], (self.x, self.y))
                self.shootCount += 1
            else:
                window.blit(self.hero_charL, (self.x, self.y))
        elif self.right:
            if self.shoot:
                window.blit(self.heroShootR[self.shootCount], (self.x, self.y))
                self.shootCount += 1
            else:
                window.blit(self.hero_charR, (self.x, self.y))

    self.hitbox = (self.x + 20, self.y, 28, 60)

def shootB(self):
    bullet = Bullet(self.x, self.y)
    bullets.add(bullet)
    allSprites.add(bullet)

敌人的班级

class theEnemy(pygame.sprite.Sprite):

enemyWalkRight = [pygame.image.load('enemy_right/1R.png'), pygame.image.load('enemy_right/2R.png'),
                  pygame.image.load('enemy_right/3R.png'), pygame.image.load('enemy_right/4R.png'),
                  pygame.image.load('enemy_right/5R.png'), pygame.image.load('enemy_right/6R.png'),
                  pygame.image.load('enemy_right/7R.png'), pygame.image.load('enemy_right/8R.png'),
                  pygame.image.load('enemy_right/9R.png'), pygame.image.load('enemy_right/10R.png'),
                  pygame.image.load('enemy_right/11R.png'), pygame.image.load('enemy_right/12R.png'),
                  pygame.image.load('enemy_right/13R.png'), pygame.image.load('enemy_right/14R.png')]

enemyWalkLeft = [pygame.image.load('enemy_left/1L.png'), pygame.image.load('enemy_left/2L.png'),
                  pygame.image.load('enemy_left/3L.png'), pygame.image.load('enemy_left/4L.png'),
                  pygame.image.load('enemy_left/5L.png'), pygame.image.load('enemy_left/6L.png'),
                  pygame.image.load('enemy_left/7L.png'), pygame.image.load('enemy_left/8L.png'),
                  pygame.image.load('enemy_left/9L.png'), pygame.image.load('enemy_left/10L.png'),
                  pygame.image.load('enemy_left/11L.png'), pygame.image.load('enemy_left/12L.png'),
                  pygame.image.load('enemy_left/13L.png'), pygame.image.load('enemy_left/14L.png')]

enemyDeath = [pygame.image.load('enemy_dead/1.png'), pygame.image.load('enemy_dead/2.png'),
              pygame.image.load('enemy_dead/3.png'), pygame.image.load('enemy_dead/4.png')]

enemy_CharR = pygame.image.load('enemy_idle/EnemyLeft.png')
enemy_CharL = pygame.image.load('enemy_idle/EnemyRight.png')

def __init__(self, x, y, width, height, end):
    pygame.sprite.Sprite.__init__(self)
    self.x = x
    self.y = y
    self.width = width
    self.height = height
    self.end = end
    self.path = [self.x, self.end]
    self.walkCount = 0
    self.velocity = 3
    self.hitbox = (self.x + 20, self.y, 28, 60)
    self.health = 5
    self.visible = True
    self.image = pygame.image.load('enemy_idle/EnemyLeft.png')
    self.rect = self.image.get_rect()

def draw(self, window):
    self.move()
    if self.walkCount + 1 >= 14:
        self.walkCount = 0

    if self.velocity > 0:
        window.blit(self.enemyWalkRight[self.walkCount], (self.x, self.y))
        self.walkCount += 1
    else:
        window.blit(self.enemyWalkLeft[self.walkCount], (self.x, self.y))
        self.walkCount += 1

    self.hitbox = (self.x + 20, self.y, 28, 60)

    #pygame.draw.rect(win, (0, 0, 0), self.hitbox, 2)

def move(self):
    if self.velocity > 0:
        if self.x + self.velocity < self.path[1]:
            self.x += self.velocity
        else:
            self.velocity = self.velocity * -1
            self.walkCount = 0
    else:
        if self.x - self.velocity > self.path[0]:
            self.x += self.velocity
        else:
            self.velocity = self.velocity * -1
            self.walkCount = 0

class Bullet(pygame.sprite.Sprite):
def __init__(self, x, y):
    pygame.sprite.Sprite.__init__(self)
    self.bullet_image = pygame.image.load('Bullet/BulletR.png')
    self.rect = self.bullet_image.get_rect()
    self.velocity = 10

def update(self):
    self.rect.x += self.velocity
    if self.rect.x > 928:
        self.kill()

def redrawGameWindow():
win.blit(bg, (0, 0))
hero.draw(win)
enemy.draw(win)
pygame.display.flip()


pygame.init()

win = pygame.display.set_mode((928, 793))
pygame.display.set_caption("First Game")

bg = pygame.image.load('darkForestBG.png')

clock = pygame.time.Clock()

allSprites = pygame.sprite.Group()
enemies = pygame.sprite.Group()

# Main Loop
font = pygame.font.SysFont('comicsans', 30, True, True)
hero = thePlayer(45, 625, 40, 40)
allSprites.add(hero)

enemy = theEnemy(400, 625, 40, 40, 928)
allSprites.add(enemy)
enemies.add(enemy)
run = True
while run:
clock.tick(60)

for event in pygame.event.get():
    if event.type == pygame.QUIT:
        run = False



keys = pygame.key.get_pressed()

if keys[pygame.K_LEFT] and hero.x > hero.velocity:
    hero.standing = False
    hero.x -= hero.velocity
    hero.left = True
    hero.right = False

elif keys[pygame.K_RIGHT] and hero.x < 928 - hero.velocity - hero.width:
    hero.standing = False
    hero.x += hero.velocity
    hero.left = False
    hero.right = True

elif keys[pygame.K_r]:
    hero.shoot = True

else:
    hero.standing = True
    hero.shoot = False
    hero.walkCount = 0

if not hero.isJump:
    if keys[pygame.K_SPACE]:
        hero.isJump = True
        hero.walkCount = 0
else:
    if hero.jumpCount >= -10:
        hero.y -= (hero.jumpCount * abs(hero.jumpCount)) * 0.5
        hero.jumpCount -= 1
    else:
        hero.jumpCount = 10
        hero.isJump = False

redrawGameWindow()

allSprites.draw(win)

pygame.quit()

按下按钮时,子弹必须被发射,但那永远不会发生。如前所述,项目符号不是由pygame.draw形成的圆形或矩形。...

感谢您的帮助!

0 个答案:

没有答案