GPWiki.org
GPWiki.org
It is currently Thu May 23, 2013 9:44 am

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Thu Jun 12, 2008 7:45 pm 
Rookie

Joined: Thu Jun 12, 2008 7:36 pm
Posts: 3
Hellooooo, I'm totaly new to programming and just started reading some tutorials for python.

I use the Pyglet library.

I know how to blit images to a window and how to make text come up when I push buttons. And I've managed to make an image move across the screen slowly. I also understand if, else if, and else, and while loops.

I'm still trying to figure out how classes work and how I can add variables to ranges, though.

My problem is, I want to try and make an image move when I push the left and right arrow keys. And... It dosn't work.

Code:
import pyglet

from pyglet import image
from pyglet import window

win = window.Window()

img = image.load('image directory')

s = 50

def img_move(direction, s):
 s = s
 win.clear()
 img.blit(s, 0)
 win.flip()
 if direction == key.RIGHT:
  s + 50
  return img_move()
 elif direction == key.LEFT:
  s - 50
  return img_move()
 else:
  return img_move()
win.img_move = img_move

while not win.has_exit:
    win.dispatch_events()


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 13, 2008 2:52 pm 
Funky Monkey

Joined: Tue Nov 16, 2004 3:49 pm
Posts: 1560
Location: Houston, TX
Welcome RaptorFarmer!

I'm a python newb myself, and I don't have an environment handy, but I think I see the problem.

When you try to increment the "s" variable, your put this:
Code:
s + 50


Instead try this:
Code:
s += 50


This might work as well:
Code:
s = s + 50


Hope this helps... Good luck!

Source: http://www.poromenos.org/tutorials/python


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 13, 2008 8:54 pm 
Rookie

Joined: Thu Jun 12, 2008 7:36 pm
Posts: 3
Hey, thanks a lot, that realy helped.

I still have a problem. The image dosn't come up on the screen unless I push left or right. And it moves left and right now, but only goes 5 pixels then stops.

It dosn't move once and then not move anymore, but it only moves 5 pixels from its origional position.

Code:
import pyglet

from pyglet import image
from pyglet import window
from pyglet.window import key

win = window.Window()

img = image.load('image directory')

s = 50

win.clear()
img.blit(50, 0) #The image is supposed to be up when the
win.flip()         #window pops up.

def on_key_press(direction, s):
 s = s
 win.clear()
 img.blit(s, 0)
 win.flip()
 if direction == key.RIGHT:
  s += 5
  return on_key_press(0, s)
 elif direction == key.LEFT:
  s -= 5
  return on_key_press(0, s)
win.on_key_press = on_key_press

while not win.has_exit:
    win.dispatch_events()


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 18, 2008 2:29 pm 
Rookie
User avatar

Joined: Wed Jun 18, 2008 2:22 pm
Posts: 3
Location: New Jersey
I believe you have a variable-scope problem with using 's' as the name of both your global position variable and the local position variable in the function. Try changing the name of one of them, or just not using a local variable.

_________________
Chaos
Lost Souls: text-based RPG
MUDseek: MUD games search


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group