Public Group

# [Python] Firing the laser

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

## 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 *

fullname = os.path.join('data', name)
try:
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.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.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 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 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.

1. 1
2. 2
frob
16
3. 3
4. 4
5. 5
Rutin
10

• 18
• 13
• 14
• 76
• 22
• ### Forum Statistics

• Total Topics
632140
• Total Posts
3004355

×