Skip to content

Instantly share code, notes, and snippets.

@apselon
Created May 12, 2016 16:56
Show Gist options
  • Save apselon/83dd0d2710d0341b00fece0270370812 to your computer and use it in GitHub Desktop.
Save apselon/83dd0d2710d0341b00fece0270370812 to your computer and use it in GitHub Desktop.
bug_game.py
#coding:utf-8
from scene import *
import random
import sound
import time
import sys
class Box(SpriteNode):
def __init__(self,**kwargs):
SpriteNode.__init__(self,'plf:Tile_BoxItem_disabled',size=(45,45) ,**kwargs)
class Item(SpriteNode):
def __init__(self,**kwargs):
SpriteNode.__init__(self,'plf:HudCoin', size=(45,45), **kwargs)
class Game(Scene):
def setup(self):
sound.set_volume(1)
self.background_color = '#c5f2f9'
self.player = SpriteNode('plc:Enemy_Bug',size = (75.5,127.5),position = (600, 70),parent=self)
self.score = 0
self.items = []
self.score_label = LabelNode('0', color='#ffffff', font=('Inconsolata', 80),parent=self,position=(self.size.w/2,self.size.h-50))
x=0
while x <= self.size.w + 256:
block = SpriteNode('plf:Ground_Grass', position=(x, 13), size = (32,32) ,parent=self)
if random.random() < 0.01:
pass
x+=32
def collect_item(self, item):
sound.play_effect('digital:PhaseJump1')
item.remove_from_parent()
self.items.remove(item)
self.score += 1
self.score_label.text = str(self.score)
def spawn_item(self):
if random.random() < 0.3:
box = Box(parent=self)
box.position = (random.uniform(20, self.size.w-20), self.size.h + 30)
d = random.uniform(2.0, 4.0)
actions = [Action.move_to(random.uniform(0, self.size.w), -100, d), Action.remove()]
box.run_action(Action.sequence(actions))
self.items.append(box)
else:
coin = Item(parent=self)
coin.position = (random.uniform(20, self.size.w-20), self.size.h + 30)
d = random.uniform(2.0, 4.0)
actions = [Action.move_by(0, -(self.size.h + 60), d), Action.remove()]
coin.run_action(Action.sequence(actions))
self.items.append(coin)
self.speed = min(5, self.speed + 0.02)
def update(self):
#self.play_musik()
player_hitbox = Rect(self.player.position.x - 20, 32, 40, 65)
for item in list(self.items):
if item.frame.intersects(player_hitbox):
if isinstance(item, Item):
self.collect_item(item)
elif isinstance(item, Box):
self.collect_item(item)
self.score = 0
self.score_label.text = str(self.score)
elif not item.parent:
self.items.remove(item)
if random.random() < 0.05:
self.spawn_item()
def touch_began(self,touch):
x = touch.location.x
self.player.run_action(Action.move_to(x, self.player.position.y, 1))
self.player.x_scale = 1 if touch.location.x > self.player.position.x else -1
def touch_moved(self,touch):
x = touch.location.x
self.player.run_action(Action.move_to(x, self.player.position.y,1))
self.player.x_scale = 1 if touch.location.x > self.player.position.x else -1
def stop(self):
sound.set_volume(0)
run(Game(), show_fps=True)
scn = Game()
while True:
for x in ['digital:LowThreeTone','digital:LowDown']:
sound.play_effect(x)
time.sleep(0.8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment