Re: [Tutor] simon game issues

2012-09-14 Thread Matthew Ngaha
> It would appear you're running some form of event loop, such as
> wxpython, tkinter, pygame, or whatever.  If so, you have to do your
> delays using the mechanism that system defines, not using sleep().  As
> you've discovered, sleep() stops the whole thread, and that's not what
> you want.  What you want is to set an event to fire at some measured
> time in the future, so the actual waiting is done inside the event loop.
>

hey i looked at the livewires documentation and pulled up the timer
information. im struggling to figure out how to use its explanation. i
need to use it at 2 different parts of the program. 1st when the game
starts i need to create my sprite objects so they appear before my
animation objects:

red = Sprite(colour = Colours.red, x = 250, y = 255)
games.screen.add(red)

#timer event delay goes here so the animations don't start as soon as
the game is opened, before the coloured sprites as they've been doing.

self.create_animations()
-


i also need a timer event when i display my sequence of animation
objects so they dont show on screen simultaneously. so:

#1st animation
red_animation = Animation(images = c_red, x = 250, y = 255,
 repeat_interval = 4, n_repeats = 1)
games.screen.add(red_ani)
red_sound.play()

#Timer event to go here, so next animation in sequence goes after the
timed pause.

#2nd animation
blue_animation = Animation(images = c_blue, x = 373, y = 255,
   repeat_interval = 4, n_repeats = 1)
games.screen.add(blue_ani)
blue_sound.play()

do you think you can offer any help how to implement the timer code or
function? i will provide the link to the page, but here's the relevant
code:


Timer
The Timer class is a class you can add to something which is also a
subclass of Object, to make an object that performs actions at regular
intervals. A class which is intended to be used with another class is
called a mix-in. For instance, if you wanted to make a new class of
your own which was a Circle and also a Timer, you would define the
class by saying class MyClass (games.Circle, games.Timer):
 • init_timer (interval)

interval is how often the tick method is called, measured in timer
ticks. How long a tick is depends on the fps argument you give to the
Screen‘s mainloop method. Setting fps to 50 means a tick is 1/50 of a
second.

You must call this method emph{after} you have called the init_ method
for the Object subclass you are using.

• stop ()

Stop the timer running. It continues to exist, but doesn’t count any more.

• start ()

Starts the timer again. A full interval will elapse before it ticks.

• get_interval ()

Gets the current interval.

• set_interval (interval)

Sets the current interval.

• tick ()

This method must be supplied by you, by subclassing the Timer class.

http://www.geon.wz.cz/livewires/w-livewires.html
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] simon game issues

2012-09-13 Thread Dave Angel
On 09/13/2012 10:07 AM, Matthew Ngaha wrote:
> Hi guys. my Python tutorial set me a task to recreate a simon game
> using livewires.
> http://www.youtube.com/watch?v=agUABjGAJww
> this is quite long, i understand if you can't help right away
>
> summary:
> i create 4 squared shaped objects(red, blue, green, yellow) as
> instances for a class derived from the Sprite class
> i create 4 animation objects to represent the 4 colour shaped objects.
> the animations get brighter in colour so the user can understand which
> colour he has to follow
> the animations also light up when the user presses the keyboard key
> they are assigned too.
> they are instances for a class derived from the Animation class.
> i have also included 4 sound files, assigned to 1 animation object to
> be played each time that animation lights up
>
> My problems:
> everytime i want to play a sequence for the user to follow. i run into
> this problem. i create 1 animation say red, so the user can see the
> 1st colour he has to follow. then i put a timed delay time.sleep(1)
> before i display the next animation, say blue. i have tried many other
> timing methods to achieve this, but once there is some sort of timer,
> the whole screen pauses for the duration of the timed delay (but the
> sound files play simultaneously during that freeze) then after that,
> the sequence is displayed at the same time. not 1 after the other. so
> even though i put the timer after the creation of the 1st animation,
> the timer for some reason happens before it. is there a way i can get
> around this?
>
> I also start the game/program by displaying the 4 square objects
> (Sprite Class) before adding their animation. after the creation of
> the sprites i play the animation sequence. The problem is they play as
> soon as the game starts before the Sprites are even displayed, and
> also simultaneously (not in a sequnce) making it confusng. so after
> creating the sprites, i added a timer so the animations happens after
> the sprites are shown. But again because of this timer, which is meant
> to occur after the creation of the sprites, when i start the game, i
> get a blank screen for the duration of the timer((but the sound files,
> again play simultaneously during that time). After that i still don't
> get the sprites, i get their animatons instead, then finally the
> sprites are displayed. i dont understand, the Sprites are meant to be
> the 1st things on the screen, is there a solution?
>
> Getting user input:
> the last problem i have is when the user presses a key to select a
> colour. i have the script check for the key press, then return the
> colour to a function/method called def result(self, color): that
> determines if he got the right colour. if he does i increase score by
> 1. to check results i create 2 lists. one with all 4 colours:
>
> self.animation = [Pressed.dict_red, Pressed.dict_blue,
> Pressed.dict_green, Pressed.dict_yellow]
> then i create an empty list to append the sequence in the order i
> randomly played them, so i can check if the user got each colour:
> self.get_animation = [] < they append from the 1st list above and my
> result function uses it to check colours
>
> here's how it goes.
> if keyboard for colour red is pressed:
> create red animation:
> send results back to result method: self.game.result(red)
>
> my result method and its class:
> self.stage = 3   ..#say if we were on stage 3. i would play 3
> colours. stage 4 would be 4 colours
>
> self.counter = -1 .#to correctly index list
> def result(self, COLOUR):
> self.counter += 1#count is now at 0
> self.end_testing = self.stage..#this will count down from
> the stages value. once it reaches 0, there is no more colours to check
> for
>
> if COLOUR in self.get_animation[self.counter]:
> self.score.value += 1
> self.end_testing -= 1
>
> even though i am using a counter here to count down: self.end_testing
> < i have used while and for loops also. My problem is none of these
> work because a keypress from the user, even if tapped, registers as
> multiple key presses, the self.score that i increment by 1 flies to
> about 40 from 1 key press. as a result my counter i use to index the
> list is way out of range. i tried to add a time.sleep(.03) as soon as
> a key is pressed, so it would register once. it managed to limit it to
> 4 presses, so self.score would equal 4 after 1 press, 8 after 2.. in
> that i started my counter at -4. then the code self.counter /= 4 would
> give me 0. after the next 4 presses, my counter would be at 4, the
> same code self.counter /= 4 would give me 1. after the next 4 presses,
> my counter would be at 8, the same code self.counter /= 4 would give
> me 2. this successfully fixed my indexing issue, but it needs A LOT of
> if statements and it is getting to confusing. isnt there a way to make
> 1 key press mean 1? and not 40? other wise how can i keep track of the
> score. any help? do i quit this