So I made a new game in pygame and after hours of debugging, I got to a stage where all of my objects get blitted onto the screen. Well, that's great but now I want movement for a particular object. I achieved that task easily enough, but I was wondering is there a way to make movement continuous while a key is pressed. For example, while the down key is pressed down, the character will smoothly glide 5 steps down instead of jerking repeatedly. This won't be good in a runner game where the player chooses when to start running and when to stop. I thought that this would work:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_DOWN:
heroY += 10
but unfortunately, this is the same as removing the second line and fixing the code indentation. I am very new to pygame and am making test projects to practice. Thanks in advance.
Full Code :
'''
Imports
'''
import pygame_textinput
import pygame
import sys
import random
import os
os.chdir(os.path.dirname(os.path.abspath(__file__)))
'''
Basic Stuff for Pygame
'''
pygame.init()
display_surface = pygame.display.set_mode((800, 640))
pygame.display.set_caption("AMOGUS")
gameIcon = pygame.image.load('icon_of_the_game.png')
pygame.display.set_icon(gameIcon)
'''
Variables
'''
player_changeable_name = pygame_textinput.TextInputVisualizer()
clock = pygame.time.Clock()
white = (255, 255, 255)
'''
Game State Variable
'''
menu = "menu"
game = "game"
ending_ani = "ending_ani"
'''
Functions
'''
def menu():
print("In Proggress")
def game():
print("In Proggress")
def ending_ani():
print("In Proggress")
'''
Images
'''
special_character = random.randint(1, 5)
if special_character == 1:
imposter = pygame.image.load('sus1.png')
sussie1 = pygame.image.load('sus2.png')
sussie2 = pygame.image.load('sus3.png')
sussie3 = pygame.image.load('sus4.png')
sussie4 = pygame.image.load('sus5.png')
elif special_character == 2:
imposter = pygame.image.load('sus2.png')
sussie1 = pygame.image.load('sus1.png')
sussie2 = pygame.image.load('sus3.png')
sussie3 = pygame.image.load('sus4.png')
sussie4 = pygame.image.load('sus5.png')
elif special_character == 3:
imposter = pygame.image.load('sus3.png')
sussie1 = pygame.image.load('sus1.png')
sussie2 = pygame.image.load('sus2.png')
sussie3 = pygame.image.load('sus4.png')
sussie4 = pygame.image.load('sus5.png')
elif special_character == 4:
imposter = pygame.image.load('sus4.png')
sussie1 = pygame.image.load('sus1.png')
sussie2 = pygame.image.load('sus2.png')
sussie3 = pygame.image.load('sus3.png')
sussie4 = pygame.image.load('sus5.png')
else:
imposter = pygame.image.load('sus5.png')
sussie1 = pygame.image.load('sus1.png')
sussie2 = pygame.image.load('sus2.png')
sussie3 = pygame.image.load('sus3.png')
sussie4 = pygame.image.load('sus4.png')
hero = pygame.image.load('hero.png')
'''
Hero
'''
heroX = 100
heroY = 100
'''
Imposter
'''
ImpX = 0
ImpY = 0
'''
Sussies
'''
Sussie1X = 0
Sussie1Y = 0
Sussie2X = 500
Sussie2Y = 500
Sussie3X = 250
Sussie3Y = 250
Sussie4X = 100
Sussie4Y = 500
'''
Main Script
'''
game_state = menu
while game_state == menu:
display_surface.fill(white)
display_surface.blit(imposter, (ImpX, ImpY))
display_surface.blit(hero, (heroX,heroY))
display_surface.blit(sussie1, (Sussie1X,Sussie1Y))
display_surface.blit(sussie2, (Sussie2X,Sussie2Y))
display_surface.blit(sussie3, (Sussie3X,Sussie3Y))
display_surface.blit(sussie4, (Sussie4X,Sussie4Y))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_DOWN:
heroY += 10
if event.type == pygame.KEYUP:
pass
pygame.display.update()
```
downButtonPressed = true
and when you get a key released event you storedownButtonPressed = false
and then in your game's main update loop you check the value ofdownButtonPressed
to decide whether to move the character down one frame's worth. Where did you run into trouble with this? \$\endgroup\$Down_Key_Pressed = True
. Then did the same with the key being released event, but this time it sets it to True. Then used if to move the hero. Unfortunately, no improvement. Could you edit the main loop and share it with me? \$\endgroup\$