Re: [pygame] Writing a simple GUI

2010-06-03 Thread Herman Chau
I've managed to get the menu working like I wanted it to. Thanks everyone!

On Thu, Jun 3, 2010 at 2:32 PM, Jake Bolton  wrote:

> Yes.
>
> You have currently selected unit on menu click.
> Clicking "attack" let's you select a target.
> Save / store these now.
>  DoAttack( unitCur, unitSelected )
>
>  might be easier to send an event.
>
> Pseudo:   # event type as string
>  # then keyword list, args can be different between types.
>  send_event( type="attack",
>  attacker=curSelectedUnit() , target=clickedUnit(), weapon="primary" )
>
> Another option is define menu , set function reference:
>
> # func Menu.additem=
> Class Menu()
>  AddItem( desc, func )
>
> # didn't use target as args, keeping logic outside menu
> M.addItem( "attack" , game.handleAttack() )
>
> # game class containing units. Def func=
> Game.handleAttack( self )
>  Attacker= self.curUnit()
>  Defender=self.selectedUnit()
>  self.damage(defender)
>
> Monkey
>
> On Jun 2, 2010, at 9:24 AM, B W  wrote:
>
> > In my mind, the player has to select which unit to perform an action on.
> That target would be stored as state info in your game engine. Then your
> attack() callback simply chooses the attack action, and the game engine has
> the info it needs to execute the turn: player->attacking->target.
> >
> > Gumm
>


Re: [pygame] Writing a simple GUI

2010-06-02 Thread Jake Bolton
Yes. 

You have currently selected unit on menu click. 
Clicking "attack" let's you select a target. 
Save / store these now.
  DoAttack( unitCur, unitSelected )

 might be easier to send an event.

Pseudo:   # event type as string
  # then keyword list, args can be different between types. 
  send_event( type="attack", 
  attacker=curSelectedUnit() , target=clickedUnit(), weapon="primary" ) 

Another option is define menu , set function reference:

# func Menu.additem=
Class Menu()
  AddItem( desc, func )

# didn't use target as args, keeping logic outside menu
M.addItem( "attack" , game.handleAttack() )

# game class containing units. Def func=
Game.handleAttack( self )
  Attacker= self.curUnit()
  Defender=self.selectedUnit()
  self.damage(defender)

Monkey

On Jun 2, 2010, at 9:24 AM, B W  wrote:

> In my mind, the player has to select which unit to perform an action on. That 
> target would be stored as state info in your game engine. Then your attack() 
> callback simply chooses the attack action, and the game engine has the info 
> it needs to execute the turn: player->attacking->target.
> 
> Gumm


Re: [pygame] Writing a simple GUI

2010-06-02 Thread B W
In my mind, the player has to select which unit to perform an action on.
That target would be stored as state info in your game engine. Then your
attack() callback simply chooses the attack action, and the game engine has
the info it needs to execute the turn: player->attacking->target.

Gumm


Re: [pygame] Writing a simple GUI

2010-06-02 Thread Jason M. Marshall
Generate a new callback function for each unit instance. In the example below, 
each instance has its own lambda function that is assigned to func. Each lambda 
function remembers the instance's label as the argument's default value.

>>> class unit(object):
def __init__(self, label):
self.func = lambda x=label: another_function(x)

>>> def another_function(x):
print x

>>> u1 = unit(1)
>>> u2 = unit(2)
>>> u1.func()
1
>>> u2.func()
2

Jason





From: Herman 
To: pygame-users@seul.org
Sent: Wed, June 2, 2010 3:18:03 AM
Subject: [pygame] Writing a simple GUI

Hi everyone,

I've just started learning Pygame (wrote a tetris game) and currently
I'm trying to write a simple turn-based strategy game, similar to
Advance Wars. What I need to implement now is a simple GUI system
which essentially provides menus to select what each unit does.

I decided to try to have a Tmenu class which consists of several
Toption instances (each one representing a menu option). The Toption
basically consists of a "label" member, a "image" member and a "func"
member. The func member is supposed to be a function that will be
executed when that specific option is selected.

But if I wanted to say, have an option for the selected unit to attack
an adjacent unit, the function I assigned to "func" couldn't possibly
know which unit is attacking which (since the arguments between
different functions should vary).

I thought about having a global variable that stores arguments that
the function could access but that seems to complicate the code and I
read that global variable use should be minimized.

So I'm wondering if someone could show me how I could solve this
problem so that the each menu option's function will have the
information it needs when it executes?



  

[pygame] Writing a simple GUI

2010-06-02 Thread Herman
Hi everyone,

I've just started learning Pygame (wrote a tetris game) and currently
I'm trying to write a simple turn-based strategy game, similar to
Advance Wars. What I need to implement now is a simple GUI system
which essentially provides menus to select what each unit does.

I decided to try to have a Tmenu class which consists of several
Toption instances (each one representing a menu option). The Toption
basically consists of a "label" member, a "image" member and a "func"
member. The func member is supposed to be a function that will be
executed when that specific option is selected.

But if I wanted to say, have an option for the selected unit to attack
an adjacent unit, the function I assigned to "func" couldn't possibly
know which unit is attacking which (since the arguments between
different functions should vary).

I thought about having a global variable that stores arguments that
the function could access but that seems to complicate the code and I
read that global variable use should be minimized.

So I'm wondering if someone could show me how I could solve this
problem so that the each menu option's function will have the
information it needs when it executes?