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:

#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:

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):

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.

And don't forget to use self to turn those functions into methods for the instance, like
def moveup(self):

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:

#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.

Oh, sorry, I forgot to add that the Elderly class should be modified as seen below:

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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.