Created
April 3, 2013 01:20
-
-
Save SebastianJarsve/5297707 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: UTF-8 -*- | |
# Written by Sebastian Jarsve | |
from scene import * | |
from sound import * | |
from random import randint, uniform | |
from math import sin | |
GAME_PLAYING = 1 | |
GAME_DEAD = 0 | |
score = 0 | |
touched = False | |
game_state = GAME_PLAYING | |
def load_highscore(): | |
global highscore | |
try: | |
f = open('spaceimpact_highscore.txt', 'r').read() | |
highscore = int(f) | |
except IOError: | |
highscore = 0 | |
def save_highscore(): | |
global highscore; highscore = score | |
f = open('spaceimpact_highscore.txt', 'w') | |
f.write(str(highscore)) | |
f.close() | |
class Space(Scene): | |
def setup(self): | |
global WIDTH; WIDTH = self.size.w | |
global HEIGHT; HEIGHT = self.size.h | |
self.root_layer = Layer(self.bounds) | |
self.stars = passing_stars() | |
self.spaceship = Spaceship() | |
self.enemy = Enemy() | |
self.enemy.spaceship_bullets = self.spaceship.bullet | |
self.enemy.spaceship_location = self.spaceship.pos | |
load_highscore() | |
def new_game(self): | |
global game_state; game_state = GAME_PLAYING | |
global score; score = 0 | |
self.enemy.units = [] | |
self.spaceship.bullet = [] | |
self.spaceship.pos.x = WIDTH/2 | |
self.enemy.spaceship_bullets = self.spaceship.bullet | |
self.enemy.spaceship_location = self.spaceship.pos | |
self.enemy.spawn_rate = 100 | |
self.enemy.spawn_pattern = 0 | |
self.enemy.kill_count = 0 | |
def draw(self): | |
global highscore | |
background(0.10, 0.10, 0.10) | |
if game_state == GAME_PLAYING: | |
self.stars.moving() | |
self.spaceship.move_spaceship() | |
self.spaceship.draw_bullet() | |
self.enemy.draw() | |
text("Score: "+str(score), 'ArialRoundedMTBold', 32, WIDTH-150, HEIGHT-30) | |
text("Bullets: "+str(10 - len(self.spaceship.bullet)), 'ArialRoundedMTBold', 32, 100, HEIGHT-30) | |
elif game_state == GAME_DEAD: | |
text('Game Over!', 'BradleyHandITCTT-Bold', 64, WIDTH/2, HEIGHT*3/4) | |
text('Touch screen to restart', 'BradleyHandITCTT-Bold', 32, WIDTH/2, HEIGHT/4) | |
if score > highscore: | |
text('New Highscore: '+str(score), 'ArialRoundedMTBold', 64, WIDTH/2, HEIGHT/2) | |
if touched == True: | |
if score > highscore: | |
save_highscore() | |
self.new_game() | |
def touch_began(self, touch): | |
global touched | |
touched = True | |
def touch_moved(self, touch): | |
global touched | |
touched = True | |
def touch_ended(self, touch): | |
global touched | |
touched = False | |
class passing_stars(): | |
def __init__(self): | |
self.layer = Layer(Rect(0,0,WIDTH, HEIGHT)) | |
self.pos = Point([], []) | |
self.stars = [] | |
self.speed = 1 | |
for i in xrange(50): | |
self.pos.x.append(randint(0, WIDTH-25)) | |
self.pos.y.append(randint(0, HEIGHT-25)) | |
self.stars.append(Layer(Rect(self.pos.x[i], self.pos.y[i], 25, 40))) | |
self.stars[i].image = 'PC_Star' | |
self.stars[i].alpha = uniform(0.05, 0.7) | |
self.layer.add_layer(self.stars[i]) | |
def moving(self): | |
for i in xrange(50): | |
self.stars[i].frame.y -= self.speed | |
if self.stars[i].frame.y < -20: | |
self.stars[i].frame.y = HEIGHT | |
self.stars[i].frame.x = randint(0, WIDTH-25) | |
if self.speed < 5: | |
self.speed += 0.1 | |
self.layer.draw() | |
class Spaceship(): | |
def __init__(self): | |
self.pos = Point(WIDTH*0.5, 20) | |
self.size = (75, 75) | |
self.frame = 0 | |
self.bullet = [] | |
self.bullet_speed = 3 | |
def move_spaceship(self): | |
self.frame = (self.frame + 1)%128 | |
g = gravity() | |
self.ship_layer = Layer(Rect(self.pos.x, self.pos.y, *self.size)) | |
self.ship_layer.image = 'Rocket' | |
self.ship_layer.rotation = 45 | |
self.pos.x += g.x * 20 | |
self.pos.x = min(WIDTH - self.size[0], max(0, self.pos.x)) | |
if self.pos.x > WIDTH - self.size[0]: | |
self.pos.x = WIDTH - self.size[0] | |
elif self.pos.x < 0: | |
self.pos.x = 0 | |
self.ship_layer.draw() | |
def spawn_bullet(self): | |
play_effect('Laser_4') | |
self.bullet.append(Point(self.ship_layer.frame.center().x-5, 98)) | |
def draw_bullet(self): | |
if self.frame%5 == 0: | |
if touched == True: | |
if len(self.bullet) < 10: | |
self.spawn_bullet() | |
for v in self.bullet: | |
v.y += 3 | |
bullet_layer = Layer(Rect(v.x, v.y, 10, 10)) | |
bullet_layer.image = 'Blue_Circle' | |
bullet_layer.draw() | |
if v.y > HEIGHT: | |
del self.bullet[self.bullet.index(v)] | |
class Enemy(): | |
def __init__(self): | |
self.frame = 0 | |
self.spaceship_bullets = [] | |
self.spaceship_location = Point() | |
self.units = [] | |
self.enemy_size = (50, 50) | |
self.kill_count = 0 | |
self.spawn_pattern = 0 | |
self.spawn_rate = 100 | |
def spawn_enemy(self): | |
if self.frame % self.spawn_rate == 0: | |
if self.spawn_pattern != 1: | |
self.units.append(Point(randint(1, WIDTH-50), HEIGHT)) | |
else: | |
for v in self.units: | |
del v | |
self.units.append(Point(WIDTH-100, HEIGHT)) | |
def draw(self): | |
global score | |
self.frame = (self.frame + 1)%100 | |
self.spawn_enemy() | |
for v in self.units: | |
v.y -= 3 | |
enemy_layer = Layer(Rect(v.x-10, v.y, *self.enemy_size)) | |
enemy_layer.image = 'Alien_Monster' | |
enemy_layer.draw() | |
if self.spawn_pattern == 1: | |
side_move = (sin(v.y * 0.006)) * 6 | |
v.x += side_move | |
elif self.spawn_pattern == 2: | |
side_move = (sin(v.y * 0.02)) * 6 | |
v.x += side_move | |
elif self.spawn_pattern == 3: | |
side_move = (sin(v.y * 0.02)) * 10 | |
v.x += side_move | |
elif self.spawn_pattern == 4: | |
pass | |
rm_enemy = False | |
if v.y + self.enemy_size[0] < 0: | |
del self.units[self.units.index(v)] | |
if score > 0: | |
score -= 1 | |
rm_enemy = True | |
if rm_enemy == False: | |
for b in self.spaceship_bullets: | |
if v.distance(b)-10 < self.enemy_size[0]/2: | |
try: | |
del self.units[self.units.index(v)] | |
del self.spaceship_bullets[self.spaceship_bullets.index(b)] | |
except Exception: | |
pass | |
play_effect('Explosion_5') | |
self.kill_count += 1 | |
score += 5 | |
if v.distance(self.spaceship_location) < 60: | |
del self.units[self.units.index(v)] | |
play_effect('Explosion_4') | |
global game_state; game_state = GAME_DEAD | |
if self.kill_count >= 10: | |
self.spawn_pattern = 1 | |
self.spawn_rate = 50 | |
if self.kill_count >= 20: | |
self.spawn_pattern = 2 | |
self.spawn_rate = 75 | |
if self.kill_count >= 30: | |
self.spawn_pattern = 3 | |
self.spawn_rate = 50 | |
run(Space(), PORTRAIT) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment