how to make a player move in pygame

Solutions on MaxInterview for how to make a player move in pygame by the best coders in the world

showing results for - "how to make a player move in pygame"
Francesca
08 Feb 2019
1so let's move the palyer horizontally left right
2
3# IF YOU HAVE MADE THE SCRIPT FOR MAKING SCREEN THE JUST COPY  
4# THE SYNTAX OF THE MOVE SCRIPT
5# BUT MAKE SURE YOU SEE THE DEF PLAYER
6
7
8import pygame
9# initialize pygame
10pygame.init()
11
12screen = pygame.display.set_mode((1370, 710))
13# Player
14playerIMG = pygame.image.load('ninja.png')
15playerX = 650
16playerY = 600
17playerX_change = 0
18playerY_change = 0
19#You shall have a image of 64*64 pixels You can download it from flaticon.com
20def player(x, y):
21    screen.blit(playerIMG, (x, y))
22    
23	
24            
25#YOU NEED TO MAKE A INFINITE GAME LOOP 
26# Game Loop
27running = True
28while running:
29    # Making Background color (R=Red,G=Green,B=Blue)
30    screen.fill((192, 192, 192))
31    for event in pygame.event.get():
32        if event.type == pygame.QUIT:
33            running = False
34            
35     # THE MOVE SCRIPT!!!!
36    if event.type == pygame.KEYDOWN:
37        if event.key == pygame.K_LEFT:
38            playerX_change = -1
39            
40        if event.key == pygame.K_RIGHT:
41            playerX_change = 1
42            
43        if event.key == pygame.K_UP:
44            playerY_change == 10
45            
46    if event.type == pygame.KEYUP:
47        if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
48            playerX_change = 0
49
50#IN THERE YOU NEED TO WRITE THE FUNCTION
51	player(playerX, playerY)
52# THERE YOU GO THATS ALL YOU NEED TO MOVE THE PLAYER (LEFT AND RIGHT)