Space invaders using Phython
Sorry Roz and Frank in advance for this Blip
It's a giant leap forward from the Pontoon game of yesterday to Space Invaders written in Phython, This time I can't take any credit for the code, So if you are interested in what the code does below go to Space Invaders clone, but I did get it to work on my Netbook after using apt-get to download and install all the packages requires. Zibi I presume this should work on the Raspberry Pie, Please let me know if you get River Raid to work.
from pygame import *
import random
class Sprite:
def __init__(self, xpos, ypos, filename):
self.x = xpos
self.y = ypos
self.bitmap = image.load(filename)
self.bitmap.set_colorkey((0,0,0))
def set_position(self, xpos, ypos):
self.x = xpos
self.y = ypos
def render(self):
screen.blit(self.bitmap, (self.x, self.y))
def Intersect(s1_x, s1_y, s2_x, s2_y):
if (s1_x > s2_x - 32) and (s1_x < s2_x + 32) and (s1_y > s2_y - 32) and (s1_y < s2_y + 32):
return 1
else:
return 0
init()
screen = display.set_mode((640,480))
key.set_repeat(1, 1)
display.set_caption('PyInvaders')
backdrop = image.load('data/backdrop.bmp')
enemies = []
x = 0
for count in range(10):
enemies.append(Sprite(50 * x + 50, 50, 'data/baddie.bmp'))
x += 1
hero = Sprite(20, 400, 'data/hero.bmp')
ourmissile = Sprite(0, 480, 'data/heromissile.bmp')
enemymissile = Sprite(0, 480, 'data/baddiemissile.bmp')
quit = 0
enemyspeed = 3
while quit == 0:
screen.blit(backdrop, (0, 0))
for count in range(len(enemies)):
enemies.x += + enemyspeed
enemies.render()
if enemies.x > 590:
enemyspeed = -3
for count in range(len(enemies)):
enemies.y += 5
if enemies.x < 10:
enemyspeed = 3
for count in range(len(enemies)):
enemies.y += 5
if ourmissile.y < 479 and ourmissile.y > 0:
ourmissile.render()
ourmissile.y -= 5
if enemymissile.y >= 480 and len(enemies) > 0:
enemymissile.x = enemies.x
enemymissile.y = enemies.y
if Intersect(hero.x, hero.y, enemymissile.x, enemymissile.y):
quit = 1
for count in range(0, len(enemies)):
if Intersect(ourmissile.x, ourmissile.y, enemies.x, enemies.y):
del enemies
break
if len(enemies) == 0:
quit = 1
for ourevent in event.get():
if ourevent.type == QUIT:
quit = 1
if ourevent.type == KEYDOWN:
if ourevent.key == K_RIGHT and hero.x < 590:
hero.x += 5
if ourevent.key == K_LEFT and hero.x > 10:
hero.x -= 5
if ourevent.key == K_SPACE:
ourmissile.x = hero.x
ourmissile.y = hero.y
enemymissile.render()
enemymissile.y += 5
hero.render()
display.update()
time.delay(5)
- 0
- 0
- Apple iPhone
- f/2.8
Comments
Sign in or get an account to comment.