Controls With Pygame


Sephiroth

Active Member
Joined
Jul 11, 2006
Messages
658
Age
33
Location
Germany>kaiserslautern>Hohenöllen
Website
Visit site
Hi everybody,

a week ago, i started developing with python/pygame.
after reading a bunch of tutorials, i started my first "game" for the gp2x.

The game is a 2player game. Every Player controlls a square(a blue or a red one). And both have to "eat" little bit smaller green squares. The goal is to eat as much as you can in 60seconds.

It works very good, but i have a problem with the controls.
After a few optimizations, it is still tricky..:(

About the game:
The squares should move as long as the button's are pressed. That works, but if i change while the left button is pressed to the up button, there are complications.

Sourcecode
(in the def handleEventgp2x2 in the main class is the problem)
(I know, there are some german expressions in the code. If anything isn't clear, please post it in the thread.)


I hope somebody could give me an advice, what's my fault and how to fix it.


btw: Sorry for my bad english.. ;)

greetings from germany
 
i have no time check your source but look at my controller what im using in old spokoban.

CODE

import pygame
from pygame.locals import *

ACTION_WAIT = 19
ACTION_FIRE = 18
ACTION_VOLDOWN = 17
ACTION_VOLUP = 16
ACTION_Y = 15
ACTION_X = 14
ACTION_B = 13
ACTION_A = 12
ACTION_R = 11
ACTION_L = 10
ACTION_SELECT = 9
ACTION_START = 8
ACTION_UPRIGHT = 7
ACTION_RIGHT = 6
ACTION_DOWNRIGHT = 5
ACTION_DOWN = 4
ACTION_DOWNLEFT = 3
ACTION_LEFT = 2
ACTION_UPLEFT = 1
ACTION_UP = 0

class Action:
"""
akce ktere muze uzivatel delat.delano tak,
aby se dala snadno doplnit dalsi fce
"""
def __init__(self, action):
self.action = action

class gp2x:
def __init__(self):
self.name = "GP2X Controller v0.0.1 by spookyln"
num_joy = pygame.joystick.get_count()
if num_joy > 0:
joy = pygame.joystick.Joystick(0)
joy.init()

def next(self):
for event in pygame.event.get():
if event.type == JOYBUTTONDOWN:
if joy.get_button(0) == True : return Action(ACTION_UP)
elif joy.get_button(1) == True: return Action(ACTION_UPLEFT)
elif joy.get_button(2) == True: return Action(ACTION_LEFT)
elif joy.get_button(3) == True: return Action(ACTION_DOWNLEFT)
elif joy.get_button(4) == True: return Action(ACTION_DOWN)
elif joy.get_button(5) == True: return Action(ACTION_DOWNRIGHT)
elif joy.get_button(6) == True: return Action(ACTION_RIGHT)
elif joy.get_button(7) == True: return Action(ACTION_UPRIGHT)
elif joy.get_button(8) == True: return Action(ACTION_START)
elif joy.get_button(9) == True: return Action(ACTION_SELECT)
elif joy.get_button(10) == True: return Action(ACTION_L)
elif joy.get_button(11) == True: return Action(ACTION_R)
elif joy.get_button(12) == True: return Action(ACTION_A)
elif joy.get_button(13) == True: return Action(ACTION_B)
elif joy.get_button(14) == True: return Action(ACTION_X)
elif joy.get_button(15) == True: return Action(ACTION_Y)
elif joy.get_button(16) == True: return Action(ACTION_VOLUP)
elif joy.get_button(17) == True: return Action(ACTION_VOLDOWN)
elif joy.get_button(18) == True: return Action(ACTION_FIRE)

return Action(ACTION_WAIT)



And usage
CODE

import controllers
def DialogBox(text):

img = pygame.Surface((320, 60))
img.fill((200, 200, 200, 170))
controller = controllers.gp2x()
while 1:
clock.tick(FPS)
action = controller.next()
if action.action == ACTION_START:
return 1

screen.blit(img, (0, 90))
wavy_write(screen, 90, text, font16, (255, 0, 0),wavy=0)
screen.blit(LevelMain.buttons[0], (70, 110))
write(screen,(105,117),"YES",font16,(255, 255, 255))
screen.blit(LevelMain.buttons[1], (170, 110))
write(screen,(205,117),"NO",font16,(255, 255, 255))
pygame.display.flip()


Maybe this help a little
 
and if you want check press and release try this.
CODE

class gp2x_controller:
""" trida na zpracovani eventu v ovladani gp2x """
def __init__(self):
num_joy = pygame.joystick.get_count()
if num_joy > 0:
self.joy = pygame.joystick.Joystick(0)
self.joy.init()
self.up = self.down = self.left = self.right = self.l = self.r = 0
self.joyb = self.joy.get_button

def next(self):
""" vraci dalsi akci """
for event in pygame.event.get():
if event.type == JOYBUTTONDOWN:
if self.joyb(2) == True: self.left = 1
elif self.joyb(6) == True: self.right = 1
elif self.joyb(0) == True: self.up = 1
elif self.joyb(4) == True: self.down = 1
elif self.joyb(10)== True: self.l = 1
elif self.joyb(11)== True: self.r = 1

elif event.type == JOYBUTTONUP:

if self.joyb(2) == True: self.left = 0
elif self.joyb(6) == True: self.right = 0
elif self.joyb(0) == True: self.up = 0
elif self.joyb(4) == True: self.down = 0
elif self.joyb(10)== True: self.l = 0
elif self.joyb(11)== True: self.r = 0

pygame.event.pump()
return Action({(0, 0, 1, 0, 0, 0): ACTION_UP,
(1, 0, 0, 0, 0, 0): ACTION_LEFT,
(0, 1, 0, 0, 0, 0): ACTION_RIGHT,
(0, 0, 0, 1, 0, 0): ACTION_DOWN,
(0, 0, 0, 0, 0, 1): ACTION_PAUSE,
(0, 0, 0, 0, 1, 0): ACTION_RESET,
(0, 0, 0, 0, 1, 1): ACTION_QUIT,
}.get((self.left, self.right, self.up, self.down,self.l, self.r),ACTION_WAIT))
 
Back
Top