Created
July 2, 2016 18:52
-
-
Save SpaceVoyager/d9d481002b5f759b8ba44e2e934eb973 to your computer and use it in GitHub Desktop.
superalien.py
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 | |
| 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"}]''' | |
| def calculate_overlaps(actor_frame, tile_frame): | |
| if (actor_frame.x+actor_frame.w/2) <= (tile_frame.x+tile_frame.w/2): | |
| x_overlap = actor_frame.x + actor_frame.w - tile_frame.x | |
| x_position = 'left' | |
| else: | |
| x_overlap = tile_frame.x+tile_frame.w - actor_frame.x | |
| x_position = 'right' | |
| if (actor_frame.y+actor_frame.h/2) >=(tile_frame.y+tile_frame.h/2): | |
| y_overlap = tile_frame.y + tile_frame.h - actor_frame.y | |
| y_position = 'above' | |
| else: | |
| y_overlap = actor_frame.y+actor_frame.h - tile_frame.y | |
| y_position = 'below' | |
| return (x_overlap, y_overlap, x_position, y_position) | |
| print calculate_overlaps(Rect(500, 100, 50, 150), Rect(540, 0, 300, 200)) # should print (10, 100) | |
| print calculate_overlaps(Rect(500, 100, 30, 150), Rect(540, 0, 300, 200)) # should print (-10, 100) | |
| print calculate_overlaps(Rect(600, 220, 50, 150), Rect(540, 0, 300, 200)) # (110,-20) | |
| print calculate_overlaps(Rect(850, 190, 50, 150), Rect(540, 0, 300, 200)) # (-10,10) | |
| print calculate_overlaps(Rect(550, 200, 50, 110), Rect(540, 300, 300, 200)) # (60,10) | |
| class GameEnvironment(object): | |
| def __init__(self): | |
| self.background_speed = 1 | |
| self.jump = 500 | |
| self.gravity = -600 | |
| 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.player_jumped = False | |
| def update_player(self): | |
| g = gravity() | |
| oldy = self.player.position.y | |
| oldx = self.player.position.x | |
| x = oldx | |
| y = oldy | |
| if y > 63: | |
| self.player_landed = False | |
| else: | |
| self.player_landed = True | |
| if self.player_landed == False: | |
| self.player.texture = jump_texture | |
| if abs(g.y) <= 0.05 and self.player_landed: | |
| self.player_x_speed = 0 | |
| 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 | |
| else: | |
| if abs(g.y) > 0.05: | |
| x = max(0, min(self.size.w, x + self.player_x_speed)) | |
| if self.player_jumped: | |
| self.player_jumped = False | |
| self.player_landed = False | |
| self.player_y_speed = self.env.jump | |
| elif not self.player_landed: | |
| self.player_y_speed = self.player_y_speed + self.env.gravity * self.dt | |
| if not self.player_landed: | |
| y += self.player_y_speed * self.dt | |
| if y <= 63: | |
| y = 63 | |
| desired_frame = Rect(x-self.player.size.w/2, y, self.player.size.w, self.player.size.h) | |
| for object in self.background_objects: | |
| (x_overlap, y_overlap, x_position, y_position) = calculate_overlaps(desired_frame, object.frame) | |
| if x_overlap > 0 and y_overlap > 0: | |
| if x_overlap < y_overlap: | |
| if x_position == 'left': | |
| new_x = desired_frame.x - x_overlap | |
| else: | |
| new_x = desired_frame.x + x_overlap | |
| desired_frame = Rect(new_x, desired_frame.y, desired_frame.w, desired_frame.h) | |
| else: | |
| if y_position == 'above': | |
| new_y = desired_frame.y + y_overlap | |
| else: | |
| new_y = desired_frame.y - y_overlap | |
| desired_frame = Rect(desired_frame.x, new_y, desired_frame.w, desired_frame.h) | |
| self.player.position = (desired_frame.x + desired_frame.w/2, desired_frame.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): | |
| if self.player_landed: | |
| self.player_jumped = True | |
| sound.play_effect('game:Boing_1') | |
| run(MyScene(), LANDSCAPE) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment