how to make a player move in pygame code example
Example: how to make a player move in pygame
so let's move the palyer horizontally left right
# IF YOU HAVE MADE THE SCRIPT FOR MAKING SCREEN THE JUST COPY
# THE SYNTAX OF THE MOVE SCRIPT
# BUT MAKE SURE YOU SEE THE DEF PLAYER
import pygame
# initialize pygame
pygame.init()
screen = pygame.display.set_mode((1370, 710))
# Player
playerIMG = pygame.image.load('ninja.png')
playerX = 650
playerY = 600
playerX_change = 0
playerY_change = 0
#You shall have a image of 64*64 pixels You can download it from flaticon.com
def player(x, y):
screen.blit(playerIMG, (x, y))
#YOU NEED TO MAKE A INFINITE GAME LOOP
# Game Loop
running = True
while running:
# Making Background color (R=Red,G=Green,B=Blue)
screen.fill((192, 192, 192))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# THE MOVE SCRIPT!!!!
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX_change = -1
if event.key == pygame.K_RIGHT:
playerX_change = 1
if event.key == pygame.K_UP:
playerY_change == 10
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 0
#IN THERE YOU NEED TO WRITE THE FUNCTION
player(playerX, playerY)
# THERE YOU GO THATS ALL YOU NEED TO MOVE THE PLAYER (LEFT AND RIGHT)