Hi everyone ! In this post, let us see how to move objects on the game screen. In the previous post, we learnt how to draw objects and fill colors. Now we will see how to move those objects from one place to another.
To move objects in a game, we need some event to occur. Let us make the object move on pressing the arrow buttons.
import pygame
pygame.init()
white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
display=pygame.display.set_mode((500,500))
game_loop=False
rect_x = 100
rect_y = 100
while not game_loop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_loop = True
if event.type == pygame.KEYDOWN :
if event.key == pygame.K_LEFT :
rect_x -= 10
if event.key == pygame.K_RIGHT :
rect_x += 10
if event.key == pygame.K_UP :
rect_y -= 10
if event.key == pygame.K_DOWN :
rect_y +=10
display.fill(blue)
display.fill(black,rect=[rect_x,rect_y,50,50])
pygame.display.update()
pygame.quit()
quit()
The pygame.event.get() is the events loop where we can write functions to control all the events.
pygame.KEYDOWN is the event type that occurs when a key is pressed down. When it is a keydown event, then we check if what key is pressed. If the user presses the left arrow key, we have to move the rectangle to the left. In the program, we have to reduce the value of x co-ordinate of the rectangle by some unit( 10 here ).pygame.K_LEFT event handles the event when left arrow key is pressed.
Similarly, code is written to handle the keydown events of all the arrow keys.
So when the program is executed, the rectangle moves in the direction corresponding to the arrow key presses by some distance.
The disadvantage of this code is that, when the user presses the key, the rectangle moves by 10 and we have to press the arrow key again to make the rectangle move. The rectangle stops moving, the moment user takes his/her hand off the key, i.e only one keydown event happens even when the user continues to press the key without taking his/her hand off.
In order to handle this, let us introduce a variable x_change and y_change.
import pygame
pygame.init()
white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
display=pygame.display.set_mode((500,500))
game_loop=False
rect_x = 100
rect_y = 100
x_change = 0
y_change = 0
while not game_loop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_loop = True
if event.type == pygame.KEYDOWN :
if event.key == pygame.K_LEFT :
y_change = 0
x_change = -10
if event.key == pygame.K_RIGHT :
y_change = 0
x_change = +10
if event.key == pygame.K_UP :
x_change = 0
y_change = -10
if event.key == pygame.K_DOWN :
x_change = 0
y_change = +10
display.fill(blue)
rect_x += x_change
rect_y += y_change
display.fill(black,rect=[rect_x,rect_y,50,50])
pygame.display.update()
pygame.quit()
quit()
To move objects in a game, we need some event to occur. Let us make the object move on pressing the arrow buttons.
import pygame
pygame.init()
white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
display=pygame.display.set_mode((500,500))
game_loop=False
rect_x = 100
rect_y = 100
while not game_loop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_loop = True
if event.type == pygame.KEYDOWN :
if event.key == pygame.K_LEFT :
rect_x -= 10
if event.key == pygame.K_RIGHT :
rect_x += 10
if event.key == pygame.K_UP :
rect_y -= 10
if event.key == pygame.K_DOWN :
rect_y +=10
display.fill(blue)
display.fill(black,rect=[rect_x,rect_y,50,50])
pygame.display.update()
pygame.quit()
quit()
The pygame.event.get() is the events loop where we can write functions to control all the events.
pygame.KEYDOWN is the event type that occurs when a key is pressed down. When it is a keydown event, then we check if what key is pressed. If the user presses the left arrow key, we have to move the rectangle to the left. In the program, we have to reduce the value of x co-ordinate of the rectangle by some unit( 10 here ).pygame.K_LEFT event handles the event when left arrow key is pressed.
Similarly, code is written to handle the keydown events of all the arrow keys.
So when the program is executed, the rectangle moves in the direction corresponding to the arrow key presses by some distance.
The disadvantage of this code is that, when the user presses the key, the rectangle moves by 10 and we have to press the arrow key again to make the rectangle move. The rectangle stops moving, the moment user takes his/her hand off the key, i.e only one keydown event happens even when the user continues to press the key without taking his/her hand off.
In order to handle this, let us introduce a variable x_change and y_change.
import pygame
pygame.init()
white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
display=pygame.display.set_mode((500,500))
game_loop=False
rect_x = 100
rect_y = 100
x_change = 0
y_change = 0
while not game_loop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_loop = True
if event.type == pygame.KEYDOWN :
if event.key == pygame.K_LEFT :
y_change = 0
x_change = -10
if event.key == pygame.K_RIGHT :
y_change = 0
x_change = +10
if event.key == pygame.K_UP :
x_change = 0
y_change = -10
if event.key == pygame.K_DOWN :
x_change = 0
y_change = +10
display.fill(blue)
rect_x += x_change
rect_y += y_change
display.fill(black,rect=[rect_x,rect_y,50,50])
pygame.display.update()
pygame.quit()
quit()
We also need to add code that controls the frames per second. The game loop runs nearly 1000 times per second, so we need to control the number of frames per second in order to make the movement visible to human eyes. pygame.time.Clock() function is used for this functionality.
So the final code is :
import pygame
pygame.init()
clock = pygame.time.Clock()
white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
display=pygame.display.set_mode((500,500))
game_loop=False
rect_x = 100
rect_y = 100
x_change = 0
y_change = 0
while not game_loop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_loop = True
if event.type == pygame.KEYDOWN :
if event.key == pygame.K_LEFT :
y_change = 0
x_change = -10
if event.key == pygame.K_RIGHT :
y_change = 0
x_change = +10
if event.key == pygame.K_UP :
x_change = 0
y_change = -10
if event.key == pygame.K_DOWN :
x_change = 0
y_change = +10
display.fill(blue)
rect_x += x_change
rect_y += y_change
display.fill(black,rect=[rect_x,rect_y,50,50])
clock.tick(15)
pygame.display.update()
pygame.quit()
quit()
So we have successfully written a program to move a rectangle on the screen. In the next post, let us see how we can start designing a simple game with all the functionalities.
Comments
Post a Comment