So, I've used another answer to help me figure out an easy way into moving my player sprite in pygame. The only issue is that the buttons for movement have to be spammed in order for the sprite to move more than the five pixels specified every time the button is pressed.
Here's the code in full. The areas in question are the Player() class and the " player." areas in the loop:
import pygame
player_path = "downChara.png" #specifies image path
class Player(object): #representitive of the player's overworld sprite
def __init__(self):
self.image = pygame.image.load(player_path).convert_alpha() #creates image, the player_path variable allowing it to be updated
self.X = 284; # x co-ord of player
self.Y = 184; # y co-ord of player
def handle_keys(self): #handling the keys/inputs
key = pygame.key.get_pressed()
dist = 5 #distance travelled in one frame of the program
if key[pygame.K_DOWN]: #if down
self.Y += dist #move down the length of dist
player_path = "downChara.png" #change image to down
self.image = pygame.image.load(player_path).convert_alpha()
elif key[pygame.K_UP]: #if up
self.Y -= dist #move up the length of dist
player_path = "upChara.png" #change to up
self.image = pygame.image.load(player_path).convert_alpha()
if key[pygame.K_RIGHT]: #etc.
self.X += dist
player_path = "rightChara.png"
self.image = pygame.image.load(player_path).convert_alpha()
elif key[pygame.K_LEFT]:
self.X -= dist
player_path = "leftChara.png"
self.image = pygame.image.load(player_path).convert_alpha()
def draw(self, surface): #draw to the surface/screen
surface.blit(self.image, (self.X, self.Y))
pygame.init() (width, height) = (600, 400) #specify window resolution bg_colour = (100,20,156) #specify bg colour
screen = pygame.display.set_mode((width, height)) #create window pygame.display.set_caption('EduGame') #specify window name
player = Player() clock = pygame.time.Clock()
pygame.display.flip() #paints screen gameRun = True #allow game events to loop/be carried out more than once
while gameRun: #while game is running:
for event in pygame.event.get(): #all the events (movement, etc) to be here.
if event.type == pygame.QUIT: #if the "x" is pressed
pygame.quit() #quit game
gameRun = False #break the loop.
quit()
player.handle_keys() #handle keys
screen.fill(bg_colour) #draw background colour
player.draw(screen) #draws player
pygame.display.update()
clock.tick(40)
here are the specific areas that I mentioned:
class Player(object): #representitive of the player's overworld sprite
def __init__(self):
self.image = pygame.image.load(player_path).convert_alpha() #creates image, the player_path variable allowing it to be updated
self.X = 284; # x co-ord of player
self.Y = 184; # y co-ord of player
def handle_keys(self): #handling the keys/inputs
key = pygame.key.get_pressed()
dist = 5 #distance travelled in one frame of the program
if key[pygame.K_DOWN]: #if down
self.Y += dist #move down the length of dist
player_path = "downChara.png" #change image to down
self.image = pygame.image.load(player_path).convert_alpha()
elif key[pygame.K_UP]: #if up
self.Y -= dist #move up the length of dist
player_path = "upChara.png" #change to up
self.image = pygame.image.load(player_path).convert_alpha()
if key[pygame.K_RIGHT]: #etc.
self.X += dist
player_path = "rightChara.png"
self.image = pygame.image.load(player_path).convert_alpha()
elif key[pygame.K_LEFT]:
self.X -= dist
player_path = "leftChara.png"
self.image = pygame.image.load(player_path).convert_alpha()
def draw(self, surface): #draw to the surface/screen
surface.blit(self.image, (self.X, self.Y))
and:
player.handle_keys() #handle keys
screen.fill(bg_colour) #draw background colour
player.draw(screen) #draws player
the "player." isn't in any event.type if/elif loop, however when i try putting it under "event.type == pygame.KEYDOWN" it doesn't seem to have any effect.
This isn't necessarily a crippling error as I could try and adapt my game around it, however I think the game would run and look smoother if the player was able to just hold down the respective key(s) instead of spam-tapping them for a decade to try and navigate.
(as a bonus feel free to try and find a better way of updating the sprites path/location than what I did under the "def handle_keys(self)" function)
cheers