Naruto Abidal
Naruto Abidal

Reputation: 57

Python Space Invaders with PyGame Troubleshooting

I am working on a simple space invaders game with pygame and have been first creating classes for the different objects, or sprites, in my game. The goal right now is to create a character that can move back and forth on the x axis and shoot projectiles. I managed to get the character to move back and forth across the screen but when it came to shooting it won't work and I can't get it to run at all. It is telling me that x and y are missing positional arguments, yet when I'm appending the shot to the array I assign values to it. What am I doing wrong?

Here is how I think this part should go. I assign a class which is Shot, and this is the projectile I will be shooting. When defining it I put parameters so that the position from which the projectile fires changes based on where my character is. Then I load the image which I have in a separate folder and assign a velocity to the projectile. Next I put the update section which allows it to run in my main loop set up underneath. By setting if statements I make sure that if the projectile leaves the screen it disappears. By making an array I can have multiple projectiles on the screen at one time and they won't delete one another. If the user presses the space bar a new projectile should be added to the array and fired. Since I am new to this I don't know if my concept of this is correct and if it is what the little mistake is that I'm making. What I'm learning in tutorials is not in a class and does not have the update function so that is what I have been trying to implement myself.

import pygame, sys
from pygame import *
pygame.init()
screen = pygame.display.set_mode((800,600))
pygame.display.set_caption("Space Invaders")
pygame.mouse.set_visible(0)
WIDTH = 800
vel = 5
width = 64
BLACK = (0, 0, 0)

all_sprites = pygame.sprite.Group()
class Player(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("images\ship.png")
        self.rect = self.image.get_rect()
        self.rect.center = (WIDTH/2, 550)
    def update(self, keys, *args):
        if keys[pygame.K_LEFT] and self.rect.x > vel:
            self.rect.x -= vel
        if keys[pygame.K_RIGHT] and self.rect.x < 800 - width - vel:
            self.rect.x += vel 
        screen.blit(self.image, self.rect)

class Shot(pygame.sprite.Sprite):
    def __init__(self,x,y):
        pygame.sprite.Sprite.__init__(self)
        self.x = x
        self.y = y
        self.image = pygame.image.load("images\laser.png")
        self.rect = self.image.get_rect()
        self.vel = 10
    def update(self, keys, *args):
        for shot in shots:
            if shot.x < 500 and shot.x >0:
                shot.y -= shot.vel
            else:
                self.kill()
            screen.blit(self.image, self.rect)
        if keys[pygame.K_SPACE]:
            shots.append(Shot(x, 550))

#class EnemyAlien(pygame.sprite.Sprite):
 #   def __init__(self):



player = Player() 
all_sprites.add(player)
shot = Shot(player.rect.centerx, 550)
all_sprites.add(shot)

shots = []
run = True
while run:
    pygame.time.delay(60)
    x = player.rect.centerx
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
    keys = pygame.key.get_pressed()
    all_sprites.update(keys)
    pygame.display.update()
    screen.fill(BLACK)
    all_sprites.draw(screen)

    pygame.display


pygame.quit()

If anyone would like to take a look and let me know what I might have to alter or if I'm just completely wrong here. Thank you.

EDIT I updated it and now I don't get an error message anymore and the file will run but nothing happens when I press space. I can't fire, and my character has weird lag where it looks like there's more being dragged behind whenever its moving.

EDIT I am trying out a different method as well, this has no errors but when I press space simply spawns the projectile in the top left corner and then it disappears after some seconds.

here is the code parts for that

class Shot(pygame.sprite.Sprite):
    def __init__(self,x,y):
        pygame.sprite.Sprite.__init__(self)
        self.x = x
        self.y = y
        self.image = pygame.image.load("images\laser.png")
        self.rect = self.image.get_rect()
        self.vel = 10
    def update(self, keys, *args):
        screen.blit(self.image, self.rect)
        self.y -= self.vel
        if self.y < 15 or self.y > 600:
            self.kill()

run = True
while run:
    pygame.time.delay(60)
    x = player.rect.centerx
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                shot = Shot(player.rect.centerx, 550)
                shots.add(shot)
                all_sprites.add(shots)
    keys = pygame.key.get_pressed()
    all_sprites.update(keys)
    pygame.display.update()
    screen.fill(BLACK)
    all_sprites.draw(screen)

    pygame.display

Which would be better and what is wrong in either of them?

Upvotes: 1

Views: 748

Answers (2)

Rabbid76
Rabbid76

Reputation: 211135

There is a basic misunderstanding. Shot is derived from pygame.sprite.Sprite. The only container where it has to be is the pygame.sprite.Group (all_sprites).
You don't need a separate container shots:

shots = []

Akso you don't need an initial Shot object, because not a Shot generates new Shot, but Player generates a new Shot:

class Player(pygame.sprite.Sprite):

    # [...]

    def update(self, keys, *args):
        if keys[pygame.K_LEFT] and self.rect.x > vel:
            self.rect.x -= vel
        if keys[pygame.K_RIGHT] and self.rect.x < 800 - width - vel:
            self.rect.x += vel 

        if keys[pygame.K_SPACE]:
            all_sprites.add(Shot(self.rect.centerx, 550)) # <---- new shot 

all_sprites = pygame.sprite.Group()
player = Player() 
all_sprites.add(player)

The class Shot, just has to update its own position (.rect) and to .kill() itself.

class Shot(pygame.sprite.Sprite):
    def __init__(self,x,y):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("images\laser.png")
        self.rect = self.image.get_rect()
        self.rect.center = (x, y) 
        self.vel = 10
    def update(self, keys, *args):
        if self.rect.x < 500 and self.rect.x > 0:
            self.rect = self.rect.move(0, -self.vel)
        else:
            self.kill()

Note, you don't need any screen.blit(self.image, self.rect) in the .update methods of Player and Shot, because that is what pygame.sprite.Group.draw() does:

screen.blit(self.image, self.rect)

Upvotes: 1

AzMoo
AzMoo

Reputation: 506

This line is causing the error: shot = Shot()

The __init__ method of Shot takes an x and y positional argument but you're not passing them.

Upvotes: 0

Related Questions