-1

click here to see the image

PROGRAMING LANGUAGE THAT I USE IS PYTHON

As on the photo,i have a character(square) and i want when i press the left, right, up, down keys, it has to keep moving in that direction until it hits the wall then stops.But when i press those keys, it only moves once and not continuously like i thought.how to make it move continuously

import pygame

pygame.init()
clock = pygame.time.Clock()
#display
window = pygame.display.set_mode((600,600))
caption = pygame.display.set_caption('SwipeIt')
#player
square = pygame.image.load('asset/square.png').convert_alpha()
square_rect = square.get_rect(center = (100,150))
#wall
wall1 = pygame.image.load('asset/wall1.png').convert()
wall2 = pygame.image.load('asset/wall2.png').convert()
screw = pygame.image.load('asset/screw.png').convert()
white = (255,252,252)
gravity = 0.25
swipe = 0
loop = True
while loop:
   print(window)
   window.fill(white)
   #wall
   window.blit(wall1,(0,0))
   window.blit(wall2,(0,0))
   window.blit(wall2,(565,0))
   window.blit(wall1,(0,566))
   window.blit(screw,(0,0))
   window.blit(screw,(0,566))
   window.blit(screw,(565,0))
   window.blit(screw,(565,566))
   #player
   window.blit(square,(square_rect))

   for event in pygame.event.get():
       if event.type == pygame.KEYDOWN:
           if event.key == pygame.K_DOWN:
                   swipe += gravity
                   square_rect.centery += swipe
           if event.key == pygame.K_UP:
                   swipe -= gravity
                   square_rect.centery -= swipe  
           if event.key == pygame.K_RIGHT:
                   swipe += gravity
                   square_rect.centerx += swipe   
           if event.key == pygame.K_LEFT:
                   swipe -= gravity
                   square_rect.centerx -= swipe
       if event.type == pygame.QUIT:
           loop = False
   
   pygame.display.flip()
   pygame.display.update()
   clock.tick(120)

If you need, here is the link to download the files I have used . ''https://www.mediafire.com/file/ilogoklz3t9abpa/asset.rar/file''

  • 2
    save target movement in a variable, and then move based on this variable. If you detect a key press, change target variable. – Mahrkeenerh Oct 07 '21 at 12:20

1 Answers1

0

I've made a snake game before and how I did it is by using a variable called direction.

Here is a little snippet from it.

for event in pygame.event.get():

    if event.type == pygame.QUIT:
        pygame.quit()
        quit()

    if event.type == pygame.KEYDOWN:
            
        if event.key == pygame.K_UP:
            direction = "up"
            
        if event.key == pygame.K_DOWN:
            direction = "down"

        if event.key == pygame.K_RIGHT:
            direction = "right"

        if event.key == pygame.K_LEFT:
            direction = "left"
        

Then I would check the current direction and change the position depending on the direction.

For example:

one would be

if direction == "up":
    player_y += 5
Yoffdan
  • 68
  • 7