Re: [Tutor] Keyboard Module

2011-10-17 Thread Ryan Strunk
> So use raw wx key events. They include key down,up and press events.
I'll give this a shot. Thank you. 
> Bloated carries the connotation of unnecessary code, but pygame provides 
a lot of functionality. You may not need it all, and using pygame may 
require you to import more than you need. But thats not quite the same 
as saying pygame is bloated.
> And its not that huge that it should stop you using it.
> If it works, why not?
Thanks for the clarification on that. Would there be any  benefit to using
Pygame in addition to other libraries for game development, or is it robust
enough that it should be able to stand on its own?
Thanks as always,
Ryan

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Keyboard Module

2011-10-11 Thread Ryan Strunk
Hello everyone,
I'm still hard at work on this boxing game; I know, I think it's taking
forever too. I've recently run into some trouble with my current keyboard
handler, and I'm hoping someone here might have a recommendation as to what
I could use to handle keyboard input for this and future projects.
The keyboard module I currently have access to is a wrapper for the wx
keyboard handler, which is fine, but it prevents me from holding one key and
pressing another. In an ideal world, I'd love something like this:

def left_hand(self):
if key_down('up'):
self.left_head_hook()
elif key_down('down'):
self.left_uppercut()
elif key_down('left'):
self.left_body_hook()
else:
self.jab()

The above functionality could be useful in other places as well. I would
like to some day create a keyboard trainer for my students, giving them the
ability to practice key combinations like capslock+t, control+alt+pgdn,
numpad0+numpad4, and so on.
I thought of using Pygame to pull this off, but I gather from my research
that I can't just use certain pieces of Pygame; in order to get the
functionality I want out of the keyboard module, I'll need other modules as
well. I don't want to work specifically in Pygame, because I've heard Pygame
is quite bloated and requires a good deal of overhead. (I wouldn't mind
being proven wrong on this)
All of which leads me to my question. Can anyone recommend a keyboard module
which will allow me to work with various facets of the keyboard to execute
functions. I would love to have access to presses, holds, and releases, and
it would be helpful to use unorthodox combinations of keys.
Thanks for any recommendations or advice you all have.
Best,
Ryan

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Flat is better than Nested

2011-09-08 Thread Ryan Strunk
Hello everyone,
I am still learning to program by writing this boxing game. I'm running into
a problem with how to organize data. My current setup looks something like
this:
"""This feels incredibly disgusting to me."""
self.behaviors = {
'offensive_combos': {
'combo1': {
1: { #time in seconds
'punch': {
'speed': 2,
'strength': 4,
'side': 'left'}
}
1.5: { #time in seconds
'punch': {
'speed': 4,
'strength': 8,
'side': 'right'}
}
}
}
}
By the time I write this all into a file, the end user will never even know
this crazy hierarchy exists, but I will, and I don't like it. Do I just need
to get over it and realize that sometimes nested is necessary, or is there a
better way I might consider?
Thanks for any help you can provide.
Best,
Ryan

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Copying Variables

2011-07-23 Thread Ryan Strunk
Hello everyone,
How can I make two copies of a dictionary that don't point to the same
location in memory? My plan is to generate d1 and make d2 a copy of d1.
After the user modifies d1 I want him/her to be able to return to the
initial dictionary (d2) values. I tried:
d1 = {values}
d2 = dict(d1)
then later in the code when I want to re-initialize d1:
d1 = dict(d2)
but this won't work. Any suggestions you have as to how I can make this work
are welcome.
Best,
Ryan

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Viability of Python

2011-07-21 Thread Ryan Strunk
Hello everyone,
I have been reading a lot of different articles recently, and I have
found a divergence of opinions on the viability of Python as a viable
language for high-end programs. At the same time, even sites that
recommend Python seem to recommend it as a good first language.
This email is not written to stir up controversy. I am a fan of Python
myself and use it for all of my programming. But therein lies the crux
of my question. If Python has limitations, what are they? What sorts
of things is Python useful for and what things is it not? And finally,
if there is code after Python, what’s a good second language, and when
should someone start learning it?
Thanks for any help you can give.
Best,
Ryan
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] (no subject)

2011-07-21 Thread Ryan Strunk

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Floating Point Craziness

2011-06-12 Thread Ryan Strunk
Hi everyone,
I'm designing a timeline. When the user presses the right arrow, 0.1 is
added to the current position. The user can add events to the timeline, and
can later scroll back across those events to see what they are. But
something I absolutely don't understand is happening:
I used the program to place events at 1.1, 2.1, and 3.1. Here is the end of
the debug output for arrowing to 3.1 and placing the event:
position = 2.7
position = 2.8
position = 2.9
position = 3.0
position = 3.1
event placed.
Everything appears straight forward. But then when I check the dictionary's
contents:
dictionary = {3.1014: value, 2.1005: value,
1.0999: value}
Why is this happening? The output is telling me 3.1, but the value isn't
being placed there. I've tried rounding the 0.1 interval increase to one
decimal point, but the dictionary values are still being improperly placed.
Thanks for any help you can provide.
Best,
Ryan

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Combining two Dictionaries

2011-04-30 Thread Ryan Strunk
Hello everyone,

I had an interesting thing come up earlier in my programming, and I'm trying
to wrap my mind around why it occurred.
I wanted to take two dictionaries with the same keys and combine their
values to make one, big super dictionary.

def combine(d1, d2):
for key in d1:
if key in d2:
d2[key] += d1[key]

When I assign values to each dictionary, this works perfectly.
d1 = {'a': 1, 'b': 2, 'c': 3}
d2 = {'a': 10, 'b': 20, 'c': 30}
combine(d1, d2)
d1 = {'a': 1, 'b': 2, 'c': 3}
d2 = {'a': 11, 'b': 22, 'c': 33}

When I initialize the class which holds these dictionaries, though, I need
to make sure that all the keys contained in d2 match the keys of d1. Thus I
tried:
d1 = {'a': 0, 'b': 0, 'c': 0}
d2 = d1
My understanding was that d2 looked at d1 once, grabbed its keys and values,
and went off to do its own thing.  Just as if you typed:
x = 3
y = x
x = 6
y still holds the value 3.
This turns out not to be the case with dictionaries, and I'm not sure why
this is so. Why when you change a dictionary's keys in place does a copied
dictionary take on the new values?
Thanks for any help you can provide.

Best,
Ryan

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Passing a Variable

2011-04-03 Thread Ryan Strunk
> I've read your code. Frankly I don't understand your problem. I also don't
see any occurrence of "health".
There isn't a reference to health here. My goal is to have this code act as
a checker for health, fatigue, time_remaining, or any other sort of
statistic you'd like to throw into it. My problem is that when I try:
instance = Statistic(stat=health, sound=spam, low=1, mid=15, high=30)
health can change elsewhere in the program, but the instance of statistic
class won't automatically see it.
> Also your description of the program and the program itself is kinda
overwhelming, and so much of that information is not relevant to your
question. That makes it hard to understand the question.
My apologies if this came across as verbose. I'm a newbie at all things
python, so I'm still learning everything from code to conventions.
> You can simplify the above logic:
Thank you for that. I will happily accept style suggestions whenever I can
get them.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Passing a Variable

2011-04-03 Thread Ryan Strunk
Hi list,

I am in the midst of trying to code a game based entirely on audio cues, and
I've run into a bit of a snag when trying to monitor certain variables. I'll
lay out the framework of what I'm going for in the hope that it makes sense
when written down.
In a standard video game I could have a health bar go from normal to yellow
to red as it diminishes. In audio, though, I don't have that luxury. As a
result, I have conceptualized a system whereby a player hears a sound every
so often if a particular stat drops into the caution range. If the player
drops into the danger range, the sound loops continuously. I also wanted to
make sure that if the player dropped from caution to danger, there wasn't a
big, awkward pause in the sound loop and that the player would know
immediately that his stat had dropped (see first and second if checks in the
check method).
The problem:
My existing methods directly update stats. For example: the player class has
a self.health stat which is directly affected by other methods. This has
caused no problem up until now. When I pass self.health to the code I will
paste below, however, the Statistic class does not receive health, but
rather health's value.
I understand that python passes variables by value and not by reference, and
this has not been a problem up until now. Now that I am trying to design a
class which explicitly checks a specific variable, though, I can't fathom a
way to do it unless I pass a direct reference, and I'm not sure that can be
done. I need to figure out a way for the below code to check the value of
the health variable and act on it. This way, if player's self.health
changes, the static class will take note of that and respond accordingly.
It occurred to me to make Statistic a child of int, but I'm told that's more
trouble than I probably want to deal with.
Any suggestions/advice anyone has would be greatly appreciated.

Best,
Ryan

import sound_lib
from game_utils import delay
#this encapsulates threading.Timer's assignment and start method

class Statistic(object):

def __init__(self, stat=None, sound=None, low=None, mid=None,
high=None):
self.stat = stat
self.sound = sound
self.low = low
self.mid = mid
self.high = high
self.status = 'safe'
self.auto_check_timer = None

def auto_check(self):
if self.stat > self.high:
self.status = 'safe'
return
if self.mid <= self.stat <= self.high:
self.status = 'caution'
self.sound.play(True)
self.auto_check_timer =
delay(self.sound.bytes_to_seconds(len(self.sound))*2, self.auto_check)
return
if self.low <= self.stat < self.mid:
self.status = 'danger'
self.sound.play(True)
self.auto_check_timer =
delay(self.sound.bytes_to_seconds(len(self.sound)), self.auto_check)

def check(self):
if self.status = 'caution' and self.low <= self.stat < self.mid:
#This will set the program to start a constant alarm when the
stat level has dropped below caution
self.auto_check_timer.cancel()
if self.sound.is_playing:
#to assist in setting up the caution to danger transition
#a standard playing sound will have a timer running alongside
it, so skip the next guard and return
if self.auto_check_timer.is_alive() == False:
#guard to make sure program doesn't catch every playing
sound, should prevent repeated checks from recalling auto_check
sound_duration =
self.sound.bytes_to_seconds(len(self.sound)) -
self.sound.bytes_to_seconds(self.sound.position)
self.auto_check_timer = delay(sound_duration,
self.auto_check)
return
if self.auto_check_timer == False:
#if the timer has never been called, call auto_check
self.auto_check()
return
if self.auto_check_timer.is_alive == True:
#there's already a timer running. return
return
#If it gets this far, it's because the timer already ran, the player
is 'safe', and another check is being performed
self.auto_check()

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Working with some sort of Timer

2011-02-16 Thread Ryan Strunk
Hello everyone,

I'm a first-time poster and--in fact--first time programmer, so a lot of
this is new to me. I hope to learn a lot about programming from this list,
and I hope you all won't be shy about such feedback as "you're top-posting
again, silly newbie."

I am currently in the midst of producing an audio-only boxing game similar
to Nintendo's Mike Tyson's Punch-Out!!, and I'm running into difficulty when
it comes to working with timed events.

I have begun developing a round class whose job it is to keep an eye on the
clock and the match as a whole, checking for things like how long a fighter
has been lying on the canvas. The relevant code currently looks like this:

def heartbeat(self):
   if self.round_clock_counter > self.round_clock_max:
   #todo, call end_round
   return
   if global_vars.player.fatigue < 100:
   global_vars.player.fatigue += 1
   self.round_clock = delay(1, heartbeat)

(My delay function simply encompasses calling a Timer and starting it.)

I am hoping to use the above-referenced self.round_clock as a measuring
stick to call various punching methods which I programmed into an Opponent
class. For example: at 1 minute, 30 seconds, perform maneuver x. It strikes
me that using threading.Timer to set up all of the possible maneuvers would
be an inelegant solution to the problem, but I'm not certain which other
modules/functions I could use to better carry this out.

My first question, then, pertains to the above implementation of a round
clock. I know that this code can work, and it has the additional benefit of
eventually keeping the round alive even when a fighter is being counted
after the bell rings. At the same time, however, the code carries the burden
of being infinitely recursive without the proper checks in place.
Nevertheless, would the code generally be considered acceptable, or is there
a more elegant solution I should use?

Second, I would welcome any suggestions anyone has as to how I might time
events within a round. Even pointers to specific modules or sections of the
Python documentation would be greatly appreciated.

Thank you all for any help you can provide, and I look forward to learning
and growing with everyone.

Best,
Ryan

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor