Python Forum
[PyGame] Space Invaders in PyGame
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] Space Invaders in PyGame
#1
Video 
Hi Folks,

This is my clone of space invaders with an explanation of how I coded it. Smile




Code
nilamo and michael1789 like this post
Reply
#2
I watch your videos. Is this something you plan on doing in an ongoing way?

Do Bubble Bobble! :)
Reply
#3
great start- I like the art work. some suggestions: if the aliens reach the width of the screen they drop down and speed up. If you shoot the left side the remaining aliens kept travelling until they reach the width. more levels. replay option.
Reply
#4
Thanks! Those are good suggestions, I'll definitely polish it up with those kinds of features now that the basic mechanic of the game is done.

I'll add Bubble Bobble to the list :). I've actually been working on a little platformer but there's still a lot to do.
Reply
#5
Trying to work through this wonderful tutorial series and having issues getting my spaceship to move...Is there someway to show what keys are being pressed so i can determine if the Key press is not being registered or if my update is just wrong...

Its pretty sad when you cant copy the code from the video and still make mistakes, i even raided the Github and copied and pasted in his movement commands to try to fix my issue as i could not see the fault. Cry

Since the game did not throw an error i am unsure how to determine where the issue is.

Quote:import pygame
from pygame import mixer
from pygame.locals import *
import random


#fps
clock = pygame.time.Clock()
fps = 60


#screen res
screen_width = 600
screen_height = 800

screen = pygame.display.set_mode((screen_width, screen_height))

pygame.display.set_caption("Space Invaders")

#load image
bg = pygame.image.load("img/bg.png")

def draw_bg():
screen.blit(bg,(0,0))



#create spacship class
class Spaceship(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("img/spaceship.png") # load image
self.rect = self.image.get_rect() # force image to rectangle
self.rect.center = [x, y]

def update(self):
# set movement speed
speed = 8

# get key press
key = pygame.key.get_pressed()
if key[pygame.K_LEFT] and self.rect.left > 0:
self.rect.x -= speed
if key[pygame.K_RIGHT] and self.rect.right < screen_width:
self.rect.x += speed


#create sprite group
spaceship_group = pygame.sprite.Group()

# create player
spaceship = Spaceship(int(screen_width/2), screen_height - 100)
spaceship_group.add(spaceship)



run = True # Bools need upper case "true" is not correct, must be "True"

while run:
clock.tick(fps)


#draw background
draw_bg()


# event handlers
for event in pygame.event.get():
if event.type == pygame.QUIT: # Quit must be upper case, this one controls the X on display
run = False

#update spaceship
spaceship.update()

#draw sprite groups
spaceship_group.draw(screen)


pygame.display.update()

pygame.quit()
Reply
#6
I ran the code that you've pasted above and it works fine here, but I'm wondering if you have got the "update" method indented correctly within the spaceship class.

it should be:
#create spaceship class
class Spaceship(pygame.sprite.Sprite):
	def __init__(self, x, y):
		pygame.sprite.Sprite.__init__(self)
		self.image = pygame.image.load("img/spaceship.png") # load image
		self.rect = self.image.get_rect() # force image to rectangle
		self.rect.center = [x, y]

	def update(self):
		# set movement speed
		speed = 8

		# get key press
		key = pygame.key.get_pressed()
		if key[pygame.K_LEFT] and self.rect.left > 0:
			print('Move Left')
			self.rect.x -= speed
		if key[pygame.K_RIGHT] and self.rect.right < screen_width:
			self.rect.x += speed
This ensures the update method is part of that class rather than a method on its own.

Also, if you want to troubleshoot and check whether something is working as it should, you can print it. In the code above I've put a print statement which will say 'Move Left' when the conditions are met. It's a quick way to see what's going on and where the problems might be. Of course you'll want to remove that once you get it working :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  question about my pygame code for space shooter Than999 4 2,459 Feb-07-2022, 03:55 AM
Last Post: metulburr
  [PyGame] newbie needs help with space invaders n0x 2 2,151 Jul-04-2020, 11:32 AM
Last Post: n0x
  [PyGame] Space Invaders Python Bug? CJMinecraft 2 4,389 Mar-31-2017, 03:37 PM
Last Post: georgecoopers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020