-1

I need to make the player move continuously when I press a certain key.The problem that I have is that the image of the player is moving once (when I press one of the defined keys) and then it stops. enter code here

import pygame
import sys
import os
import random
import time
from pygame.locals import *

pygame.init()

white = ( 255, 255, 255 )
black = ( 0, 0, 0 ) 

screenw = 800
screenh = 600 

screen = pygame.display.set_mode( ( screenw, screenh ) ) 

pygame.display.set_caption( "Game" ) # Here I create a display.

clock = pygame.time.Clock()

class Car(pygame.sprite.Sprite): # Here I create a class.

    def __init__( self, color = black, width = 100, height = 100 ):

        pygame.sprite.Sprite.__init__( self )

        self.image = pygame.Surface( ( width, height ) )

        self.image.fill( color )

        self.rect = self.image.get_rect()

    def set_pos(self, x, y):
        self.rect.x = x
        self.rect.y = y 

    def set_img( self, filename = None):
        if filename != None:

            self.image = pygame.image.load( filename )

            self.rect = self.image.get_rect()

def main():*I create a game loop
    x_change = 0
    y_change = 0 

    x = 0
    y = 0

    car_group = pygame.sprite.Group() # Make a group

    player = Car()

    player.set_img( "images.jpg" )

    car_group.add( player )

    exit = False

    FPS = 60

    while not exit:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

            if event.type == pygame.KEYDOWN:

                if event.key == pygame.K_LEFT:
                    x_change = -10

                elif event.key == pygame.K_RIGHT:
                    x_change = 10

                elif event.key == pygame.K_UP:
                    y_change = -10

                elif event.key == pygame.K_DOWN:
                    y_change = 10

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT or event.key == pygame.K_UP or event.type == pygame.K_DOWN:
                    x_change = 0
                    y_change = 0

            x += x_change
            y += y_change


        screen.fill( white )
        player.set_pos( x, y ) # Blit the player to the screen
        car_group.draw( screen )
        clock.tick( FPS )
        pygame.display.update()

main()
pygame.quit()
quit()  
jsbueno
  • 99,910
  • 10
  • 151
  • 209
  • http://stackoverflow.com/questions/16044229/how-to-get-keyboard-input-in-pygame Please learn to use the search feature. Also read the documentation from Pygame http://www.pygame.org/docs/ref/key.html – RNikoopour Jan 21 '15 at 18:03
  • 3
    I believe if you unindent the `x+= ... y+=...` one indent it may do what you want. – user2097159 Jan 21 '15 at 18:03
  • At first sight. things look in place - try adding a "pygame.event.pump" call in your main while loop. The need for that varies across specific calls to event.* functions and systems. – jsbueno Jan 21 '15 at 18:14
  • Also, note a typo in your "big if": `event.type == pygame.K_DOWN:` it should be event.key. Actually, this if could be rewritten as `if event.key in (K_DOWN, K_UP, K_LEFT, K_RIGHT):` ( you have the key constants in your names space with the `from pygame.locals import *` statement) – jsbueno Jan 21 '15 at 18:17

1 Answers1

0

Well the problem is that you are only allowing the sprite to move once. For example, take this piece of your code:

if event.key == pygame.K_LEFT:
    x_change = -10

This will allow the x position to be smaller by 10. Then it stops. You maybe want to make a update function. A variable will also be made to allow the function to be used or not to be used. Another one will be used to control the direction it goes. Here is the update function. Feel free to change it to be able to fit your needs:

def update():
    global direction
    if direction == 'LEFT':
        x -= 10 
    elif direction == 'RIGHT':
        x += 10
    elif direction == 'UP'
        y -= 10
    elif direction == 'DOWN':
        y += 10

Now we will need the variable to control whether the function will run or not. Add these two lines for our new two variables:

run = 0
direction = 'NONE'

They should be before the code for the class. You absolutely change the lines from your event.key lines, it should be (in proper indention):

if event.key == pygame.K_LEFT:
    direction = 'LEFT'
    run = 1
elif event.key == pygame.K_RIGHT:
    direction = 'RIGHT'
    run = 1
elif event.key == pygame.K_UP:
    direction = 'UP'
    run = 1
elif event.key == pygame.K_DOWN:
    direction = 'DOWN'
    run = 1

Of course, there must be something to stop the car from repeatedly moving infinitely right? Add these lines with the lines above:

elif event.key == pygame.K_SPACE:
    direction = 'NONE'
    run = 0

Now put these two lines in the while loop but before you for loop:

if run == 1:
    Car.update()
else:
    pass   

This should work because as long run is equal to 1, the car will continue its movement in the given direction until stopped by pressing the spacebar. I hope this helps you!

Anthony Pham
  • 3,096
  • 5
  • 29
  • 38