Connecting Tech Pros Worldwide Forums | Help | Site Map

Moving an object by user input

Newbie
 
Join Date: Nov 2008
Posts: 9
#1: Mar 10 '09
I'm looking for a tutorial (preferably with livewires) or some sort of documentation on how to control an object with user input.

Basically what i want to do is move an object with the arrow keys.

Any suggestions?

boxfish's Avatar
Expert
 
Join Date: Mar 2008
Location: California
Posts: 478
#2: Mar 10 '09

re: Moving an object by user input


In the book I have, Python for the Absolute Beginner, it's done something like this. The object has a member named screen, which is a reference to the main Screen object.
Expand|Select|Wrap|Line Numbers
  1. def moved(self):
  2.     x, y = self.get_pos()
  3.     if self.screen.is_pressed(games.K_LEFT):
  4.         x -= 1
  5.     if self.screen.is_pressed(games.K_UP):
  6.         y -= 1
  7.     if self.screen.is_pressed(games.K_RIGHT):
  8.         x += 1
  9.     if self.screen.is_pressed(games.K_DOWN):
  10.         y += 1
  11.     self.move_to(x, y)
  12.  
I hope this helps.
Newbie
 
Join Date: Nov 2008
Posts: 9
#3: Mar 11 '09

re: Moving an object by user input


Used that code and added

self.init_mover(dx=0, dy=0)

and it works perfectly.

Thanks man!
Reply