Jump to content
  • Advertisement
Sign in to follow this  

[Python] Firing the laser

This topic is 4307 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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)
        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):
        self.image, self.rect = load_image("player.jpg", -1)
        self.rect.x = position[1]
        self.rect.y = position[0]

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

def main():
    screen = pygame.display.set_mode((400,500))
    background = pygame.Surface(screen.get_size())
    background = background.convert()
    screen.blit(background, (0,0))
    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:
        for event in pygame.event.get():
            if event.type == QUIT:
        keystate = pygame.key.get_pressed()
        lrdirection = keystate[K_RIGHT] - keystate[K_LEFT]
        firing = keystate[K_SPACE]
        if firing:

        screen.blit(background, (0,0))
if __name__ == '__main__': main()

Share this post

Link to post
Share on other sites
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
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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!