| | |
Moving an object with control keys in a sprite
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Nov 2007
Posts: 3
Reputation:
Solved Threads: 0
Hello,
I know this is a very simple problem, but I am about to give up... Why couldn't I get the control keys do their job? Maybe someone whose eyes are more rested than mine can tell me. I will appreciate any help.
I am dealing with pygame, and I have two images loaded on the screen when running the program. However, what I want the first image in the first sprite to do is moving around with controlled keys by me, such as, move up, left, down, and right.
By the way, needless to say, I am new to pygame. It actually is so much fun, especially when things are smooth.
Here is my code:
I have been up for hours and hours and tried some searching. I also modified the code a little bit. When I hit the control keys, it used to shut itself down. At least, it doesn't do it anymore, but I am getting some attribute errors.
Thank you very much in advance.
I know this is a very simple problem, but I am about to give up... Why couldn't I get the control keys do their job? Maybe someone whose eyes are more rested than mine can tell me. I will appreciate any help.
I am dealing with pygame, and I have two images loaded on the screen when running the program. However, what I want the first image in the first sprite to do is moving around with controlled keys by me, such as, move up, left, down, and right.
By the way, needless to say, I am new to pygame. It actually is so much fun, especially when things are smooth.
Here is my code:
Python Syntax (Toggle Plain Text)
#Coding starts here """The elder moves when pressed the control keys & when collides with the scared guy, makes him disappear Modified on September 20th, 2009""" import pygame pygame.init() screen = pygame.display.set_mode((960, 720)) #Resolution has been changed by me class Elderly(pygame.sprite.Sprite): def __init__(self): pygame.sprite.Sprite.__init__(self) self.image = pygame.image.load("ELDERLY1.png") self.rect = self.image.get_rect() self.rect.centerx = 100 self.rect.centery = 150 def update(self): self.rect.center = pygame.image.load() #if self.rect.right > screen.get_width(): # self.rect.left = 0 class Ohno(pygame.sprite.Sprite): def __init__(self): pygame.sprite.Sprite.__init__(self) self.image = pygame.image.load("OHNO2.png") self.rect = self.image.get_rect() self.rect.centerx = 800 self.rect.centery = 450 def update(self): self.rect.center = pygame.image.load() #if self.rect.right > screen.get_width(): # self.rect.left = 0 def main(): #Entities for the background background = pygame.Surface(screen.get_size()) background = background.convert() background.fill((238, 232, 170)) # screen.blit(background, (0, 0)) elderly = Elderly() ohno = Ohno() allSprites = pygame.sprite.Group(elderly, ohno) clock =pygame.time.Clock() keepGoing = True while keepGoing: clock.tick(30) for event in pygame.event.get(): if event.type == pygame.QUIT: keepGoing = False #Starting from here, modifying the control keys elif event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: elderly.moveup() if event.key == pygame.K_DOWN: elderly.movedown() if event.key == pygame.K_RIGHT: elderly.moveright() if event.key == pygame.K.LEFT: elderly.moveleft() elif event.type == pygame.KEYUP: if event.key == pygame.K_UP or event.key == pygame.K_DOWN or event.key == pygame.K_RIGHT or event.key == pygame.K_LEFT: elderly.movepos = [0,0] elderly.state = "still" allSprites.clear(screen, background) allSprites.update() allSprites.draw(screen) pygame.display.flip() if __name__ == "__main__": main() #Coding ends here
I have been up for hours and hours and tried some searching. I also modified the code a little bit. When I hit the control keys, it used to shut itself down. At least, it doesn't do it anymore, but I am getting some attribute errors.
Thank you very much in advance.
This block of code looks like it's presenting the problem:
If you take another look at your Elderly class, it doesn't have any methods defined called moveright(), moveleft(), moveup(), or movedown(), or properties called movepos or state for that matter either!
So you'll need to define them in your Elderly class.
e.g.
inside your definition of Elderly you need to define the moveup function (and the other missing functions like this):
You should also add and initialise the missing properties (movepos and state) in the __init__ function for the Elderly class!
Give that a go and let us know how you get on!
Cheers for now,
Jas.
PYTHON Syntax (Toggle Plain Text)
elif event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: elderly.moveup() if event.key == pygame.K_DOWN: elderly.movedown() if event.key == pygame.K_RIGHT: elderly.moveright() if event.key == pygame.K.LEFT: elderly.moveleft() elif event.type == pygame.KEYUP: if event.key == pygame.K_UP or event.key == pygame.K_DOWN or event.key == pygame.K_RIGHT or event.key == pygame.K_LEFT: elderly.movepos = [0,0] elderly.state = "still"
If you take another look at your Elderly class, it doesn't have any methods defined called moveright(), moveleft(), moveup(), or movedown(), or properties called movepos or state for that matter either!
So you'll need to define them in your Elderly class.
e.g.
inside your definition of Elderly you need to define the moveup function (and the other missing functions like this):
PYTHON Syntax (Toggle Plain Text)
def moveup(): #TODO: add code to move your elder upwards def movedown(): #TODO: add code to move your elder downwards # etc etc.....
You should also add and initialise the missing properties (movepos and state) in the __init__ function for the Elderly class!
Give that a go and let us know how you get on!
Cheers for now,
Jas.
If you're into metal, check out my new band at:
http://www.myspace.com/kinasis
Now booking gigs for 2010....
http://www.myspace.com/kinasis
Now booking gigs for 2010....
•
•
Join Date: Nov 2007
Posts: 3
Reputation:
Solved Threads: 0
Thank you very much to all responders.
After working a while on this code, I got everything I needed smooth and working. Here is how I figured out the control keys:
I hope this will be helpful for someone else too.
pygame is fun!
Best wishes to everyone.
After working a while on this code, I got everything I needed smooth and working. Here is how I figured out the control keys:
Python Syntax (Toggle Plain Text)
#Starting from here, modifying the control keys elif event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: elderly.dy -= 20 if event.key == pygame.K_DOWN: elderly.dy += 20 if event.key == pygame.K_LEFT: elderly.dx -= 20 if event.key == pygame.K_RIGHT: elderly.dx += 20 #The part below is from http://www.pygame.org/docs/tut/tom/games5.html elif event.type == pygame.KEYUP: #Occurs when user releases the control keys if event.key == pygame.K_UP or event.key == pygame.K_DOWN: elderly.dy = 0 if event.key == pygame.K_RIGHT or event.key == pygame.K_LEFT: elderly.dx = 0
I hope this will be helpful for someone else too.
pygame is fun!

Best wishes to everyone.
•
•
Join Date: Nov 2007
Posts: 3
Reputation:
Solved Threads: 0
Oh, sorry, I forgot to add that the Elderly class should be modified as seen below:
Python Syntax (Toggle Plain Text)
class Elderly(pygame.sprite.Sprite): #Creating the first Sprite within its class for the first image def __init__(self): #Built-in Sprite method & Called when the object is created pygame.sprite.Sprite.__init__(self) #Call the constructor for Sprite self.image = pygame.image.load("ELDERLY1.png") #Load the first image self.image = self.image.convert_alpha() #Create the Sprite image & New copy of the surface with the desired pixel format. self.rect = self.image.get_rect() #Create the rect attribute for Sprite self.rect.centerx = 200 #Horizontal position of the image self.rect.centery = 200 #Vertical position of the image self.dx = 0 #Initialize x self.dy = 0
![]() |
Similar Threads
- Customizing VS DataSource object's control type (C#)
- image in picture control not displayed properly after moving (C++)
- Moving an object in a 2d-vector (C++)
- Custom user control key events problem (C#)
- moving an object... (Python)
- java project for moving object dtection (Java)
- Repeating An Action [Moving An Object Every Couple Of Secs..] (Java)
- Moving an object up and down in C++!! (C++)
Other Threads in the Python Forum
- Previous Thread: Python Xlib . Looking for basis docs or tut.
- Next Thread: python assignment help
Views: 755 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for keycontrol, movingimageswithpygame, pygame, python, sprite
accessdenied ajax anti arax array backend bash beginner book c++ calling class code color console csv curved cx-freeze dictionary digital django dynamic editing embed examples excel file ftp function generator google gui halp homework ideas iframe infosec input itunes java keycontrol leftmouse library line linux list lists loan microphone mouse mysql numbers obexftp prime program programming projects py2exe pygame pygtk pyopengl python random read recursive remote ruby script search server simple skinning slicenotation smtp software source sprite string sudokusolver syntax table tennis terminal text threading time tkinter tooltip tutorial ubuntu unicode update url urllib urllib2 variable voip web-scrape whileloop wxpython







