Pygame - 2d space shooter

need help shooting bullets

Page 1 of 1

0 Replies - 2594 Views - Last Post: 13 October 2009 - 06:00 PM Rate Topic: -----

#1 Animech  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 16-September 08

Pygame - 2d space shooter

Posted 13 October 2009 - 06:00 PM

I'm making a side scroller shooting game using pygame where the player shoots bullets by pressing the space bar and enemies shoot bullets randomly, but I can seem to get it right. here's the part involving the ship, the enemy and their bullets:

import pygame, random
pygame.init()

screen = pygame.display.set_mode((640, 480))

class Ship(pygame.sprite.Sprite):
	def __init__(self, bullet):
		self.bullet = bullet
		pygame.sprite.Sprite.__init__(self)
		self.screen = screen
		self.image = pygame.image.load("ship.jpg")
		self.image = self.image.convert()
		self.rect = self.image.get_rect()
		self.rect.center = (50, 240)
		self.dx = 10
		self.dy = 10

		if not pygame.mixer:
			print "problem with sound"
		else:
			pygame.mixer.init()
			self.sndCrash = pygame.mixer.Sound("shipcrash2.ogg")
			self.sndFire = pygame.mixer.Sound("gunfire2.ogg")
			self.sndExplosion = pygame.mixer.Sound("explosion2.ogg")

	def update(self):
		self.checkKeys()
		self.checkBounds()

	def checkKeys(self):
		keys = pygame.key.get_pressed()
		if keys[pygame.K_UP]:
			self.rect.centery -= self.dy
		if keys[pygame.K_DOWN]:
			self.rect.centery += self.dy
		if keys[pygame.K_SPACE]:
			self.bullet.x = self.rect.centerx
			self.bullet.y = self.rect.centery
			self.bullet.speed = self.dx



		   
	   
	def checkBounds(self):
		screen = self.screen
		if self.rect.centery > 420:
			self.rect.centery = 420
		if self.rect.centery < 50:
			self.rect.centery = 50

class Bullet(pygame.sprite.Sprite):
	def __init__(self, screen):
		pygame.sprite.Sprite.__init__(self)
		self.screen = screen
	   
		self.image = pygame.image.load("bullet.jpg")
		self.image = self.image.convert()
		self.rect = self.image.get_rect()
		self.rect.center = (-100, -100)
		self.speed = 0
		self.reset()

		if not pygame.mixer:
			print "problem with sound"
		else:
			pygame.mixer.init()
			self.sndImpact = pygame.mixer.Sound("impact2.ogg")


	def update(self):
		self.position()
		self.checkBounds()
		self.rect.center = (self.x, self.y)

	def position(self):
		self.x += self.speed

	def checkBounds(self):
		screen = self.screen
		if self.rect.left > screen.get_width():
			self.reset()
   

	def reset(self):
		self.x = -100
		self.y = -100
		self.speed = 0

class Ufo(pygame.sprite.Sprite):
	def __init__(self):
		pygame.sprite.Sprite.__init__(self)
		self.image = pygame.image.load("ufo.jpg")
		self.image = self.image.convert()
		self.rect = self.image.get_rect()
		self.reset()

	def update(self):
		self.rect.centerx -= self.dx
		self.rect.centery -= self.dy
		if self.rect.right <= 0:
			self.reset()
   
	def reset(self):
		self.rect.left = screen.get_width()
		self.rect.centery = random.randrange(0, screen.get_height())
		self.dy = random.randrange(-2, 2)
		self.dx = random.randrange(4, 6)

class UfoBullet(pygame.sprite.Sprite):
	def __init__(self, screen):
		pygame.sprite.Sprite.__init__(self)
		self.image = pygame.image.load("enemybullet.jpg")
		self.image = self.image.convert()
		self.rect = self.image.get_rect()
		self.reset()
		self.rect.centerx = 490
		self.dx = 10

		def update(self):
			self.rect.centerx -= self.dx
			if self.rect.right <= 0:
				self.reset()

		def reset(self):
			self.x = -50
			self.dx = 10




I know I need to add or change a few things in the bullet classes, but I just can't think of what it is. if someone could help me with this part, I should be able to get the rest of the program on my own

Is This A Good Question/Topic? 0
  • +

Page 1 of 1