Troubles with simple events in Pygame
For a little background I just started attempting to program some things in Pygame this week and I'm having mad difficulties with some of the tutorials provided. Anywho this is a little adaption of one of thoes tutorials. I'm having issues what what I think is the event grabbing. This is my code
The goal is to have a picture move a spot on the background, and over write the foreground with the background and then the player image on a keystroke, I'm aware there are probly easier ways to get this done, but I'd like to get it working almost as is anyways.
The problem is some events are printed and then the program freezes shortly after(before I can even test if they keydown code is working)....this may not be relative but I'm not sure what these events are and the program freezes right after they are called <Event(17-VideoExpose {})>
<Event(1-ActiveEvent {'state': 6, 'gain': 1})>
Any help would be greatly apreciated, THANKS!
Code:
#the code works from here
import pygame,sys,os
from pygame.locals import *
screen = pygame.display.set_mode((1000,100))
def image_load(name):
print 'image loaded' #I like to randomly print things for error checking
path = os.path.join('my images', name)
return pygame.image.load(path).convert()
type1 = image_load('background1.bmp')
type2 = image_load('background2.bmp')
player = image_load('player.bmp')
background = [type1, type2, type1, type2, type2, type1, type1, type1, type2, type1]
whatyousee = [0]*10
for i in range(0, 10):
whatyousee[i] = background[i]
def setcharposition(playerpos):
for i in range(0,10):
whatyousee[i] = background[i]
if i == 9:
whatyousee[playerpos] = player
setwhatyousee(0)
def setwhatyousee(x):
print 'setbackground', playerpos #another example of random printing
for item in whatyousee:
screen.blit(item, (x*100,0))
pygame.display.flip()
x = x+1
playerpos = 5
setcharposition(5)
#all the way to here
for event in pygame.event.get():
print(event)
if event == KEYDOWN:
playerpos = playerpos - 1
if playerpos <0:
playerpos = 9
setcharposition(playerpos)The problem is some events are printed and then the program freezes shortly after(before I can even test if they keydown code is working)....this may not be relative but I'm not sure what these events are and the program freezes right after they are called <Event(17-VideoExpose {})>
<Event(1-ActiveEvent {'state': 6, 'gain': 1})>
Any help would be greatly apreciated, THANKS!
I didn't realise that tabs would be deleted in the forum, that is not my problem BTW
perks Wrote:I didn't realise that tabs would be deleted in the forum, that is not my problem BTW
Wrap your code in code tags and you won't lose your tabs (although the forum makes the tabs way more than they should be IMHO). The code tags is the # button above the message editor. Or you can use [COD E] and [/COD E] (with no space between the D and E of course).
well I managed to fix the problem after a while of checking, decided to post so everyone that checked sleeps well at night knowing I'm not pulling my hair out over a trivial problem. all it really took was adding an infinate loop and fixing some syntaxes IE=event.TYPE
Now that I've goten it working if somone has suggestions on how to improve it I'm all ears
Code:
while 1 is 1:
for event in pygame.event.get():
print(event)
if event.type == QUIT:
sys.exit()
if event.type == KEYDOWN and event.key == K_l:
playerpos = playerpos - 1
if playerpos <0:
playerpos = 9
setcharposition(playerpos)
if event.type == KEYDOWN and event.key == K_r:
playerpos = playerpos + 1
if playerpos >9:
playerpos = 0
setcharposition(playerpos)Now that I've goten it working if somone has suggestions on how to improve it I'm all ears
First, if you haven't already, bookmark this Python 2.5 quick reference. Even if you're using the version of Python that comes with Tiger (v2.3, I believe), it's still pretty handy.
So some comments.
"while 1" is pretty much the same as "while 1 is 1"
I'd check for the KEYDOWN event type once and put the handling of which key was pressed in an if block in that one. I also don't think you need setcharposition in there twice. You're duplicating quite a bit of code there.
Python does have a += and the like operator so you don't need to type x = x + 1.
So some comments.
"while 1" is pretty much the same as "while 1 is 1"
I'd check for the KEYDOWN event type once and put the handling of which key was pressed in an if block in that one. I also don't think you need setcharposition in there twice. You're duplicating quite a bit of code there.
Python does have a += and the like operator so you don't need to type x = x + 1.
The brains and fingers behind Malarkey Software (plus caretaker of the world's two brattiest felines).
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| Coding efficiently (pygame) | perks | 10 | 3,878 |
Sep 11, 2007 08:09 AM Last Post: Blacktiger |
|
| Interface Troubles | DylanE | 2 | 2,335 |
Nov 17, 2006 04:30 PM Last Post: DylanE |
|
| Troubles With Dynamic Memory | Nick | 1 | 2,134 |
Sep 7, 2005 08:56 PM Last Post: Nick |
|
| Carbon Apple quit Event troubles | deekpyro | 3 | 4,944 |
May 7, 2002 06:24 AM Last Post: deekpyro |
|

