Attached is a pygame-boilerplate, which has a basic class to handle draw,
events, and looping. It has keyboard input, and draws a blank screen.

To make sure your classes are new-style, define them like:

*class Foo():*
or

*class Foo(object):*
instead of

class Foo:

Otherwise you will run into strange problems, later on.

Here's some snakes-and-ladders code.

   - Player() class saves key, tile, and name
   - Game.players is a list, so you can add more players without modifying
   the code
   - I found the names of key 1 and 2, at :
   http://www.pygame.org/docs/ref/key.html


import pygame
from pygame.locals import * # Color() and Rect() are defined here
import random

class Player(object):
    def __init__( name, key_move ):
        self.name = name
        self.key_move = key_move
        self.tile = 0

    def move(self):
        # moves dice + self
        print "%s: moved" % (self.name)
        roll = random.choice( [1,2,3,4,5,6] )

        print "roll: %s" % (roll)
        self.tile += roll

class GameMain():
    """members:
        cur_player    : index for players
        cur_players    : list of Player()s
    """
    def __init__(self):
        p1 = Player('fred', K_1 )
        p2 = Player('bob', K_2 )


        # save as a list, easier to use variable number of players, later
on.
        self.players = [self.p1, self.p2]
        self.cur_player = 0

    def next_turn(self):
        # change value of .cur_player to next valid index
        self.cur_player += 1
        if self.cur_player >= len(a):
            self.cur_player = 0

    def main_loop(self):
        """Game() main loop"""
        while not self.done:
            self.handle_events()
            self.update()
            self.draw()

            # delay here: toggle FPS cap
            if self.bLimitFPS: self.clock.tick( self.fps_limit )
            else: self.clock.tick()    def handle_events(self):

    def handle_events(self):
        # draw bg

        # handle all events , like in boilerplate

            # but also add:
            # if key == current player's key_move , then move him.

            player = self.players[self.cur_player]
            key = player.key_move

            if event.key == cur_key:
                cur_player.move()
                self.next_turn()

On Mon, May 9, 2011 at 1:51 AM, Mr. Singh <[email protected]> wrote:

>
>
> On May 9, 10:52 am, Jake b <[email protected]> wrote:
> >"Where are you started / stuck?"
> I have just started and have not any graphics still. I am just
> creating .py file that include turns of two players.
> It's my first game or a program in python.
> Here is a simple code if you can read it:
>
>
> class Step:
>
>    posP1=0
>    posP2=0
>
>
>    def __init__(self,dice):
>
>        self.dice=dice
>        Step.posP1+=dice
>        Step.posP2+=dice
>
>    def displayMove(self):
>        print "total move: %d"%Step.move
>
>    def displayStep(self):
>        print ",dice: ",self.dice
>
> import random
>
> print '\n\n\n\n'
>
>
> def player1():
>    while Step.posP1!=100:
>        loop1()
>
>
> def player2():
>    while Step.posP2!=100:
>        loop2()
>
>
>
> def loop2():
>    input=2
>    reqd=int(raw_input('Player2:- press 2 to throw dice: '))
>    if reqd==input:
>        dice=random.choice([1,2,3,4,5,6])
>        num=Step(dice)
>        num.displayStep()
>        print "Player2 position: %d"%Step.posP2
>        player1()
>    else:
>        print "player2 pressed wrong key"
>
> def loop1():
>    input=1
>    reqd=int(raw_input('Player1:- press 1 to throw dice: '))
>    if reqd==input:
>        dice=random.choice([1,2,3,4,5,6])
>        num=Step(dice)
>        num.displayStep()
>        print "Player1 position: %d"%Step.posP1
>        player2()
>    else:
>        print 'no its wrong'
>
>
>
>
> def main():
>    player1()
>
> if __name__ == '__main__': main()
>
> #End of program
>
> # I hope you understand my problem
> # It's both players using same data
> #how should i improve it?
> # Thanks for your reply as well as help
>



-- 
Jake

Attachment: pygame-boilerplate.py
Description: Binary data

Reply via email to