尝试在pygame中射击时没有出现子弹

时间:2015-01-12 03:12:40

标签: python pygame projectile

我的代码存在问题,我在其中分配了空间来创建项目符号然后将其激活并将其更新到屏幕。问题是什么?

import pygame, random, os, sys

pygame.init()
pygame.mixer.pre_init(441100, -16, 2, 2048)
pygame.mixer.init()

width = 640
height = 480

black = (  0,  0,  0)
white = (255,255,255)

playerimg = pygame.image.load("images/shipup.png")

class player: # Class for player ship
    def __init__(self, x, y, xc, yc, w, h, angle, img, lives, direc):
        self.x = x
        self.y = y
        self.xc = xc
        self.yc = yc
        self.w = w
        self.h = h
        self.img = img
        self.lives = lives
        self.angle = angle
        self.direc = direc

    def update(self):
        gameDisplay.blit(self.img, (self.x, self.y))
        self.x += self.xc
        self.y += self.yc

playership = player(10, 10, 0, 0, 30, 50, 0, playerimg, 3, "up")

bulletxc = 0
bulletyc = 0

class bullet: # Class for shooting bullets
    def __init__(self, x, y, xc, yc, w, h, img):
        self.x = x
        self.y = y
        self.xc = xc
        self.yc = yc
        self.w = w
        self.h = h
        self.img = img

    def update(self):
        gameDisplay.blit(self.img, (self.x, self.y))
        self.x += self.xc
        self.y += self.yc
        if playership.direc == "right": # Determines what direction to shoot
            bulletxc = 6
            bulletyc = 0
        if playership.direc == "left":
            bulletxc = -6
            bulletyc = 0
        if playership.direc == "up":
            bulletyc = -6
            bulletxc = 0
        if playership.direc == "down":
            bulletyc = 6
            bulletxc = 0

bulletlist = ()

gameDisplay = pygame.display.set_mode((width, height))
pygame.display.set_caption("Asteroids", "Ast...")
clock = pygame.time.Clock()

def mainloop():
    global bulletlist
    gameExit = False

    while not gameExit:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

                                                # Basic movements V
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_w:
                    playership.yc = -3
                    playership.img = pygame.image.load("images/shipup.png")

                if event.key == pygame.K_s:
                    playership.yc = 3
                    playership.img = pygame.image.load("images/shipdown.png")

                if event.key == pygame.K_d:
                    playership.xc = 3
                    playership.img = pygame.image.load("images/shipright.png")

                if event.key == pygame.K_a:
                    playership.xc = -3
                    playership.img = pygame.image.load("images/shipleft.png")

                if event.key == pygame.K_SPACE: # Here is where the bullet fires.
                    for i in bulletlist:
                        bulletlist.append(bullet)
                        bulletlist = bullet(playership.x + 25, playership.y + 25, bulletxc, bulletyc, 5, 10, pygame.image.load("images/bullet.png"))

            elif event.type == pygame.KEYUP:
                if event.key == pygame.K_w:
                    playership.yc = 0

                if event.key == pygame.K_s:
                    playership.yc = 0

                if event.key == pygame.K_d:
                    playership.xc = 0

                if event.key == pygame.K_a:
                    playership.xc = 0

        gameDisplay.fill(black)

        playership.update()
        for i in bulletlist:
            i.update()

        if playership.x < 0: # Collisions against the sides of the screen.
            playership.x = -1

        if playership.y < 0:
            playership.y = -1

        if playership.x + playership.h > width:
            playership.x = width - playership.h

        if playership.y + playership.h > height:
            playership.y = height - playership.h

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


if __name__ == "__main__":
    mainloop()


pygame.quit()
quit()

问题:项目符号没有显示,但没有错误。

尝试:使用K_SPACE if语句中的gameDisplay.blit手动更新它。

3 个答案:

答案 0 :(得分:2)

问题在于此代码:

if event.key == pygame.K_SPACE: # Here is where the bullet fires.
    for i in bulletlist:
        bulletlist.append(bullet)
        bulletlist = bullet(playership.x + 25, playership.y + 25, 
            bulletxc, bulletyc, 5, 10, pygame.image.load("images/bullet.png"))

问题是您之前定义了bulletlist

bulletlist = ()

作为一个空元组。但是,在上面的代码中,for循环无法执行任何操作,因为bulletlist的长度为0,因此i永远不会有值。这段代码在Python中不会出错,但它也不会做任何事情。

但是,即使它有一个长度,您也会将bulletlist标识符重新分配给bullet循环中for 的实例,这是一个馊主意。我建议你考虑一下你在这里想要完成的事情,也许还想出另一种方法。

答案 1 :(得分:0)

import pygame, random, os, sys

pygame.init()
pygame.mixer.pre_init(441100, -16, 2, 2048)
pygame.mixer.init()

width = 640
height = 480

black = (  0,  0,  0)
white = (255,255,255)

playerimg = pygame.image.load("images/shipup.png")

class player: # Class for player ship
    def __init__(self, x, y, xc, yc, w, h, angle, img, lives, direc):
        self.x = x
        self.y = y
        self.xc = xc
        self.yc = yc
        self.w = w
        self.h = h
        self.img = img
        self.lives = lives
        self.angle = angle
        self.direc = direc

    def update(self):
        gameDisplay.blit(self.img, (self.x, self.y))
        self.x += self.xc
        self.y += self.yc

playership = player(10, 10, 0, 0, 30, 50, 0, playerimg, 3, "up")

bulletxc = 0
bulletyc = 0

class bullet: # Class for shooting bullets
    def __init__(self, x, y, xc, yc, w, h, img):
        self.x = x
        self.y = y
        self.xc = xc
        self.yc = yc
        self.w = w
        self.h = h
        self.img = img

    def update(self):
        gameDisplay.blit(self.img, (self.x, self.y))
        self.x += self.xc
        self.y += self.yc
        if playership.direc == "right": # Determines what direction to shoot
            bulletxc = 6
            bulletyc = 0
        if playership.direc == "left":
            bulletxc = -6
            bulletyc = 0
        if playership.direc == "up":
            bulletyc = -6
            bulletxc = 0
        if playership.direc == "down":
            bulletyc = 6
            bulletxc = 0

bulletlist = bullet(-33, -33, bulletxc, bulletyc, 5, 5, pygame.image.load("images/bullet.png"))

gameDisplay = pygame.display.set_mode((width, height))
pygame.display.set_caption("Asteroids", "Ast...")
clock = pygame.time.Clock()

def mainloop():
    global bulletlist
    gameExit = False

    while not gameExit:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

                                                # Basic movements V
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_w:
                    playership.yc = -3
                    playership.img = pygame.image.load("images/shipup.png")
                    bulletyc = -6
                    bulletxc = 0

                if event.key == pygame.K_s:
                    playership.yc = 3
                    playership.img = pygame.image.load("images/shipdown.png")
                    bulletyc = 6
                    bulletxc = 0

                if event.key == pygame.K_d:
                    playership.xc = 3
                    playership.img = pygame.image.load("images/shipright.png")
                    bulletxc = 6
                    bulletyc = 0

                if event.key == pygame.K_a:
                    playership.xc = -3
                    playership.img = pygame.image.load("images/shipleft.png")
                    bulletxc = -6
                    bulletyc = 0

                if event.key == pygame.K_SPACE: # Here is where the bullet fires.
                    bulletlist = bullet(playership.x + 15, playership.y + 15, bulletxc, bulletyc, 5, 5, pygame.image.load("images/bullet.png"))

            elif event.type == pygame.KEYUP:
                if event.key == pygame.K_w:
                    playership.yc = 0

                if event.key == pygame.K_s:
                    playership.yc = 0

                if event.key == pygame.K_d:
                    playership.xc = 0

                if event.key == pygame.K_a:
                    playership.xc = 0

        gameDisplay.fill(black)

        playership.update()
        bulletlist.update()

        if playership.x < 0: # Collisions against the sides of the screen.
            playership.x = -1

        if playership.y < 0:
            playership.y = -1

        if playership.x + playership.h > width:
            playership.x = width - playership.h

        if playership.y + playership.h > height:
            playership.y = height - playership.h

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


if __name__ == "__main__":
    mainloop()


pygame.quit()
quit()

摆脱了元组,它工作正常。

答案 2 :(得分:-1)

if event.key == pygame.K_SPACE: # Here is where the bullet fires.
                    for i in bulletlist:
                        bulletlist.append(bullet)
                        bulletlist = bullet(playership.x + 25, playership.y + 25, bulletxc, bulletyc, 5, 10, pygame.image.load("images/bullet.png"))

在这里;

bulletlist = bullet(playership.x + 25, playership.y + 25, bulletxc, bulletyc, 5, 10, pygame.image.load("images/bullet.png"))

这部分错了,你的bulletlist不再是元组了。看到你的bullet类,你应该再存储它。

另外,我认为你不能用这种方法解决问题,查看有关Pygame的更多信息。 Here是非常棒的教程。