0

When I run this, my charactor won't move, i don't get any errors, he just doesn't move. I've been trying to fix this for ages to no avail, heres my code:

import pygame
pygame.init()

class Player:
    def __init__(self, speed, image):
        self.move_speed = speed
        ##self.x = 640/2
        ##self.y = 480/2
        self.speed = [640/2,480/2]
        self.image = pygame.image.load(image)

    def update(self):
        self.x = self.speed[0]
        self.y = self.speed[1]

    def walk_left(self):
        self.speed[0] -= self.move_speed

    def walk_right(self):
        self.speed[0] += self.move_speed

    def walk_up(self):
        self.speed[1] += self.move_speed

    def walk_down(self):
        self.speed[1] -= self.move_speed
    

def main():
    clock = pygame.time.Clock()
    p = Player(2, "rifleman_blu.png")
    screen = pygame.display.set_mode((640,480))
    while True:
        clock.tick(60)
        p.update()
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                return
            elif event.type == pygame.K_LEFT:
                p.walk_left()
            elif event.type == pygame.K_RIGHT:
                p.walk_right()
            elif event.type == pygame.K_UP:
                p.walk_up()
            elif event.type == pygame.K_DOWN:
                p.walk_down()

        screen.blit(p.image, (p.speed[0], p.speed[1]))
        pygame.display.flip()

if __name__ == '__main__': main()

Any help would be greatly appreciated, thanks in advance!

2
Contributors
4
Replies
5
Views
8 Years
Discussion Span
Last Post by slate
0

You should check the type first, and if it is a keyboard event, than get the key pressed.

while True:
        clock.tick(60)
        p.update()
        for event in pygame.event.get():
            if  event.type == pygame.KEYDOWN:
                if event.key == pygame.K_q:
                    pygame.quit()
                    return
                elif event.key == pygame.K_LEFT:
                    p.walk_left()
                elif event.key == pygame.K_RIGHT:
                    p.walk_right()
                elif event.key == pygame.K_UP:
                    p.walk_up()
                elif event.key == pygame.K_DOWN:
                    p.walk_down()
0

Maybe reading a tutorial is helpful. I have never written a pygame game, all my answers come from googling pygame tutorial and getting the code from there.
For example, the problem in this thread is solved here.

0

I've been looking through this website for ages and I can't seem to find the answer :(

0

Maybe I do not see your problem clearly.

I thought the problem was, that your blue riflemen does not move.
In my previous post, I showed you how you can move it.

I took this answer from the website I mentioned, and tested on my system. On this website the first program "bounce.py" contains the code:

events = pygame.event.get( )
        for e in events:
            if( e.type == QUIT ):
                done = True
                break
            elif (e.type == KEYDOWN):
                if( e.key == K_ESCAPE ):
                    done = True
                    break
                if( e.key == K_f ):
                    pygame.display.toggle_fullscreen()

I think it is clear from that code, that the main loop looks at the event type, and if this is a keyboard event, then checks, which key is pressed.
Your code, however checks the type of the event, and checks if this type equals a key code. This is a mismatch, but causes no errors, because both type and key is defined as integer.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.