0

right now it just moves in the direction i tell it to put it just stops. How do i go about making it KEEP going in the direction I inputted by? And make it constantly go in the direction, but be able to control how fast it moves, right now i want it to move every 4 minutes

import pygame
from pygame.locals import *


pygame.init()
surface = pygame.display.set_mode((600, 600))
background = pygame.image.load('back.png')
surface.blit(background, (0, 0))
block = pygame.image.load('block.png').convert()
block_y = 0
block_x = 0
surface.blit(block, (block_x, block_y))

def draw():
    surface.blit(background, (0, 0))
    surface.blit(block, (block_x, block_y))
    pygame.display.flip()

direction = ''

pygame.display.flip()
running = True
while running:
    for event in pygame.event.get():
        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                running = False
            if event.key == K_UP:
                block_y -= 10
                draw()
            if event.key == K_DOWN:
                block_y += 10
                draw()
            if event.key == K_LEFT:
                block_x -= 10
                draw()
            if event.key == K_RIGHT:
                block_x += 10
                draw()
        elif event.type == QUIT:
            running = False
    ```
bad_coder
  • 11,289
  • 20
  • 44
  • 72
  • Instead of having your keys move your player, they should just set the direction. Then every interval move your player one more unit in the direction you set. – RufusVS Jul 01 '21 at 01:08

2 Answers2

0

Here's an option with minimal changes to your program. I changed the event handlers to update speed variables, which update the position in an added update() function. The speeds are zeroed when the key is released. I've also removed the duplicated draw() functions. Note this code is untested.

import pygame
from pygame.locals import *

pygame.init()
surface = pygame.display.set_mode((600, 600))
background = pygame.image.load('back.png')
surface.blit(background, (0, 0))
block = pygame.image.load('block.png').convert()
block_y = 0
block_x = 0
speed_x = 0
speed_y = 0
surface.blit(block, (block_x, block_y))

def draw():
    surface.blit(background, (0, 0))
    surface.blit(block, (block_x, block_y))
    pygame.display.flip()

def update():
    """Update game state"""
    block_x += speed_x
    block_y += speed_y

direction = ''

pygame.display.flip()
clock = pygame.time.Clock()
running = True
while running:
    for event in pygame.event.get():
        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                running = False
            elif event.key == K_UP:
                speed_y -= 10
            elif event.key == K_DOWN:
                speed_y += 10
            elif event.key == K_LEFT:
                speed_x -= 10
            elif event.key == K_RIGHT:
                speed_x += 10
        elif event.type == KEYUP:
            if event.key in (K_DOWN, K_UP):
                speed_y = 0
            elif event.key in (K_LEFT, K_RIGHT):
                speed_x = 0
        elif event.type == QUIT:
            running = False
    update()
    draw()
    clock.tick(60)  # limit frame rate to 60 FPS

EDIT: Added frame rate limiting

import random
  • 3,054
  • 1
  • 17
  • 22
0

Use pygame.key.get_pressed() instead of the KEYDOWN event.

The keyboard events (see pygame.event module) occur only once when the state of a key changes. The KEYDOWN event occurs once every time a key is pressed. KEYUP occurs once every time a key is released. Use the keyboard events for a single action or a step-by-step movement.
pygame.key.get_pressed() returns a sequence with the state of each key. If a key is held down, the state for the key is True, otherwise False. Use pygame.key.get_pressed() to evaluate the current state of a button and get continuous movement

running = True
while running:
    for event in pygame.event.get():
        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                running = False    
        elif event.type == QUIT:
            running = False

    keys = pygame.key.get_pressed()
    if keys[pygame.K_UP]:
        block_y -= 10
    if keys[pygame.K_DOWN]:
        block_y += 10
    if keys[pygame.K_LEFT]:
        block_x -= 10
    if keys[pygame.K_RIGHT]:
        block_x += 10

    draw()

The code can be further simplified:

running = True
while running:
    for event in pygame.event.get():
        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                running = False    
        elif event.type == QUIT:
            running = False

    keys = pygame.key.get_pressed()
    block_x += (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * 10
    block_y += (keys[pygame.K_DOWN] - keys[pygame.K_UP]) * 10

    draw()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174