Pythonistapro777
Aug 10, 2015 - 14:16
Still in the early process of my game. However, I want it so the coin sound effect doesn't start playing (when it intersects) until the FIRST tap of the screen. Then the rest is the same.
Thanks in advance!
Here's the code:
#coding: utf-8
import random, scene, sound
from scene import *
import sys
g_speed = 5
g_number_of_enemies = 10
def random_color():
return scene.Color(random.random(), random.random(), random.random())
class Particle(object):
def __init__(self, frame=None, velocity=None, color=None):
self.frame = frame or scene.Rect(0, 0, 25, 25)
self.velocity = velocity or scene.Point(0, 0)
self.color = color or random_color()
def update(self):
self.frame.x += self.velocity.x
self.frame.y += self.velocity.y
def draw(self):
self.update()
scene.stroke(0, 0, 0)
scene.fill(*self.color)
scene.rect(*self.frame)
@property
def is_moving(self):
return self.velocity.x or self.velocity.y
def reverse_x(self):
self.velocity.x *= -1
def reverse_y(self):
self.velocity.y *= -1
def keep_in_bounds(self, bounds):
if (self.frame.x <= bounds.x
or self.frame.x + self.frame.w >= bounds.x + bounds.w):
self.reverse_x()
if (self.frame.y <= bounds.y
or self.frame.y + self.frame.h >= bounds.y + bounds.h):
self.reverse_y()
def random_particle():
def random_velocity():
return random.randint(-g_speed, g_speed) or random_velocity()
return Particle(velocity=scene.Point(random_velocity(), random_velocity()))
class MyScene(scene.Scene):
def __init__(self):
self.save_velocity_x = g_speed
self.enemies = [random_particle() for i in xrange(g_number_of_enemies)]
scene.run(self, scene.PORTRAIT)
def setup(self):
self.player = self.make_player()
for enemy in self.enemies: # place enemies randomly in self.bounds
enemy.frame.x = random.randint(0, self.bounds.w - enemy.frame.w)
enemy.frame.y = random.randint(0, self.bounds.h - enemy.frame.h)
self.pts=0
self.dead=False
def make_player(self):
player_size = 35
frame = scene.Rect(140, (self.bounds.h - player_size) / 2, player_size, player_size)
color = scene.Color(1, 1, 1)
return Particle(frame=frame, color=color)
def draw(self):
if self.dead==False:
self.pts += 1
scene.background(0, 0.5, 1)
self.player.draw()
self.player.keep_in_bounds(self.bounds)
player_frame = self.player.frame
for enemy in self.enemies:
enemy.draw()
enemy.keep_in_bounds(self.bounds)
if enemy.frame.intersects(player_frame) and not self.player.is_moving:
sound.play_effect('Coin_2')
self.dead=True
text(' Points: {}'.format(self.pts), x=self.size.w/3, y=self.size.h/3.3*3, font_size=40)
def touch_began(self, touch):
self.player.frame = scene.Rect(touch.location.x-15, touch.location.y-15, 35, 35)
run(MyScene())