Skip to content

Instantly share code, notes, and snippets.

@ssophwang
Created June 11, 2016 16:31
Show Gist options
  • Save ssophwang/b4fbe69d654dcf6a433cdd622ee1579f to your computer and use it in GitHub Desktop.
Save ssophwang/b4fbe69d654dcf6a433cdd622ee1579f to your computer and use it in GitHub Desktop.
Super_Alien.py
from scene import *
import json
import sound
A = Action
standing_texture = Texture('plf:AlienBlue_front')
walk_textures = [Texture('plf:AlienBlue_walk1'), Texture('plf:AlienBlue_walk2')]
jump_texture = Texture('plf:AlienBlue_jump')
background_objects_json_str = '''
[{"x": 700, "y": 64, "w": 65, "h": 50, "img": "plf:Ground_SandMid"},
{"x": 635, "y": 64, "w": 65, "h": 50, "img": "plf:Ground_SandMid"},
{"x": 570, "y": 64, "w": 65, "h": 50, "img": "plf:Ground_SandMid"},
{"x": 505, "y": 64, "w": 65, "h": 50, "img": "plf:Ground_SandMid"},
{"x": 825, "y": 125, "w": 65, "h": 50, "img": "plf:Ground_SandHalf_mid"},
{"x": 675, "y": 185, "w": 65, "h": 50, "img": "plf:Ground_SandHalf_mid"}]'''
class GameEnvironment(object):
def __init__(self):
self.background_speed = 1
class MyScene (Scene):
def setup(self):
self.env = GameEnvironment()
self.ground = Node(parent=self)
groundimage = 'plf:Ground_SandCenter'
w = SpriteNode(groundimage).size.w
h = SpriteNode(groundimage).size.h
x = 0
while x <= self.size.w + w:
tile = SpriteNode(groundimage, position=(x, h/2))
self.ground.add_child(tile)
x += w
self.background_color = 'midnightblue'
self.background_objects = []
json_object = json.loads(background_objects_json_str)
for x in json_object:
item = SpriteNode(x['img'])
item.anchor_point = (0.5, 0)
item.position = (x['x'], x['y'])
item.size = (x['w'], x['h'])
self.add_child(item)
self.background_objects.append(item)
self.player = SpriteNode(standing_texture)
self.player.anchor_point = (0.5, 0)
self.add_child(self.player)
self.new_game()
def new_game(self):
self.walk_step = -1
self.player.position = (80, 63)
self.player.texture = standing_texture
self.player_y_speed = 0.0
self.player_x_speed = 0.0
self.max_speed = 40.0
self.player_landed = True
self.g = 2
def update_player(self):
g = gravity()
y = self.player.position.y + self.player_y_speed
x = self.player.position.x
if y > 63:
self.player_landed = False
if self.player_landed == False:
self.player.texture = jump_texture
if abs(g.y) > 0.05 and self.player_landed:
self.player_x_speed = g.y * self.max_speed
self.player.x_scale = cmp(g.y, 0)
step = int(self.player.position.x / 40) % 2
if step != self.walk_step:
self.player.texture = walk_textures[step]
sound.play_effect('rpg:Footstep00', 0.05, 1.0 + 0.5 * step)
self.walk_step = step
elif self.player_landed:
self.player.texture = standing_texture
self.walk_step = -1
if not self.player_landed:
x = x + self.player_x_speed
y = y - self.g
else:
if abs(g.y) > 0.05:
x = max(0, min(self.size.w, x + self.player_x_speed))
self.player.position = (x, y)
def update(self):
self.update_player()
'''
for dirtblock in self.background_objects:
pos = dirtblock.position
pos.x -= self.env.background_speed
dirtblock.position = pos'''
def touch_began(self, touch):
self.player_y_speed += 2.5
run(MyScene(), LANDSCAPE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment