-3

I have a problem with my code where I have space assigned to create a bullet then fire it and update it to the screen. What's the problem?

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()

Problem: Bullets not showing up, but no errors.

Tried: manually updating it using gameDisplay.blit inside the K_SPACE if statement.

HKVariant
  • 837
  • 10
  • 23

3 Answers3

2

The problem is in this code here:

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"))

The issue is that earlier you defined bulletlist:

bulletlist = ()

as an empty tuple. However, in the code above, your for loop won't do anything, as the length of bulletlist is 0, so there never is a value for i. This code will not error in Python, but it won't do anything, either.

However, even if it had a length, you are reassigning the bulletlist identifier to an instance of bullet within the for loop, which is a bad idea. I suggest you think about what you're trying to accomplish here, and perhaps come up with another way of doing it.

MattDMo
  • 100,794
  • 21
  • 241
  • 231
  • Instead of criticizing me, please tell me how to do it, I've only been using python for 7 months. – HKVariant Jan 12 '15 at 03:31
  • 1
    @HunterKepley I'm not criticizing you, I'm simply answering your question as to why your bullets are not showing up. Unfortunately, I don't fully understand what exactly you're trying to do here, which is why I didn't suggest anything else. – MattDMo Jan 12 '15 at 03:32
  • Im trying to make moving bullets appear when I press spacebar. – HKVariant Jan 12 '15 at 03:33
  • @HunterKepley after your `if` line, try removing the `for` line and the `bulletlist = bullet(...` line, and instead edit the remaining line to `bulletlist.append(bullet(playership.x, ...` – MattDMo Jan 12 '15 at 03:38
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()

Got rid of the tuple and it worked A OK.

HKVariant
  • 837
  • 10
  • 23
-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"))

In here;

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

This part is wrong, your bulletlist is not a tuple anymore. See that, your bullet class store it anymore you should fix that statement.

Also, I don't think you can make a fire with this method, check more information about Pygame. Here is pretty awesome tutorial.

GLHF
  • 3,835
  • 10
  • 38
  • 83