Sign in to follow this  
Aik

[Python] Firing the laser

Recommended Posts

Aik    122
As much as I hate asking for help - this is getting a bit ridiculous. I've been playing around with Python and Pygames for a little while now as the first programming language that I've seriously tried to learn, though most of that time I've been stuck trying to get my first attempt at a game to work. The situation hasn't changed for a few months now (over which I've given up on it and come back again several times). The game is a 2D space shooter thingy in the vein of Space Invaders (though with no enemies or anything at this point). The problem I'm having is that I can't get the laser to fire. Any help here would be appreciated. (Note that there are a good number of things in my code that I don't actually understand all that well, having trawled through other people's source code looking for hints. If anything looks stupid, it more than likely is).
import os, pygame
from pygame.locals import *

def load_image(name, colorkey=None):
    fullname = os.path.join('data', name)
    try:
        image = pygame.image.load(fullname)
    except pygame.error, message:
        print "cannot load image: ", fullname
        raise SystemExit, message
    image = image.convert()
    if colorkey is not None:
        if colorkey is -1:
            colorkey = image.get_at((0,0))
        image.set_colorkey(colorkey, RLEACCEL)
    return image, image.get_rect()

class Player(pygame.sprite.Sprite):
    speed = 5
    offset = 11
    def __init__(self, position):
        pygame.sprite.Sprite.__init__(self)
        self.image, self.rect = load_image("player.jpg", -1)
        self.rect.x = position[1]
        self.rect.y = position[0]

    def move(self, lrdirection):
        self.rect.move_ip(lrdirection*self.speed,0)
        
    def gunpos(self):
        pos = self.offset + self.rect.centerx
        return pos
        
class Laser(pygame.sprite.Sprite):
    speed = -11
    def __init__(self, pos):
        pygame.sprite.Sprite.__init__(self)
        self.image, self.rect = load_image("laser.jpg", None)
        self.rect.centerx = pos
        
    def update(self):
        self.rect.move(0, self.speed)

def main():
    pygame.init()
    screen = pygame.display.set_mode((400,500))
    
    background = pygame.Surface(screen.get_size())
    background = background.convert()
    background.fill((0,0,0))
    
    screen.blit(background, (0,0))
    pygame.display.flip()
    
    clock = pygame.time.Clock()
    player = Player((400,50))
    laser = Laser(0)
    
    player_sprite = pygame.sprite.RenderPlain(player)
    laser_sprite = pygame.sprite.RenderPlain(laser)
    
    
    while 1:
        clock.tick(60)
        for event in pygame.event.get():
            if event.type == QUIT:
                return
        keystate = pygame.key.get_pressed()
        
        
        lrdirection = keystate[K_RIGHT] - keystate[K_LEFT]
        player.move(lrdirection)
        firing = keystate[K_SPACE]
        if firing:
            Laser(player.gunpos())
            laser.update()

        screen.blit(background, (0,0))
        player_sprite.draw(screen)
        laser_sprite.draw(screen)
        pygame.display.flip()
        
if __name__ == '__main__': main()

Share this post


Link to post
Share on other sites
Oluseyi    2109
First of all, you should post a summary of your understanding of the current state of the program - what you think it does, or what you expect it to do.

Second, pygame.Rect.move creates a second pygame.Rect with the same width and height, but different x and y coordinates (a pygame.Rect is, at root, a 4-tuple consisting of (x, y, width, height)). What you want - and what you use in Player.move is pygame.Rect.move_ip. The _ip suffix stands for in place.

Third, when you call Laser(player.gunpos()), you are actually creating a new Laser object - and not modifying your existing instance, laser. What you want for that line, instead, is
laser = Laser(player.gunpos())


Those are the three things that jump out at me after a cursory inspection. If I was at work, I might actually load the code into my interprter and see what happens.

Which brings me to another point: because of Python's dynamic, interpreted nature, debugging it is easier than with most languages. If you don't have a nice IDE, grab Pythonwin (it comes with ActivePython). Set a break point and use the Locals window (or add a variable to Watch) to see what happens with them. That way, you can see what's going on inside your program.

Happy hacking!

Share this post


Link to post
Share on other sites
Aik    122
Thanks - I'll kick it around a bit more when I find the time (hopefully tomorrow).

As for my understanding of it:
You should be able to move the ship from side to side (which it does), and fire the laser from one of the guns when you press the spacebar. When this happens the game figures out where the position of the gun is at that time and when that's sorted the laser sprite should move up the screen from that point.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this