Introduction
Pygame is a multimedia toolkit for making games and other multimedia applications based on Python. In this lesson, we'll use the pygame module to draw various forms on the screen while considering their height, width, and location within the pygame window.
SDL stands for Simple DirectMedia Layer, and Pygame is a Python wrapper for it. SDL allows you to utilize any platform to access your system's multimedia hardware, such as sound, video, mouse, keyboard, and joystick. The pygame project was created to replace the now-defunct PySDL project. Because SDL and pygame are cross-platform, you can create games and rich multimedia Python programs on any platform that supports them!
Pygame
Pygame is a Python library that focuses on game development and design. Pygame only works with 2D games that use a variety of sprites. Pygame isn't suitable for game development because it's challenging to use and doesn't have a graphical user interface like Unity, but it does give logic for more complex applications.
Pygame is a collection of cross-platform Python modules for game development. It includes sound and graphics libraries for use with the Python programming language. Now it's up to the developer's imagination or requirement to decide what kind of game to make using this toolbox.
In this post, we'll look at how to construct a design in PyGame using keys such that the design, or marker, travels horizontally when you press the right or left arrow keys on the keyboard and vertically when you use the up or down arrow keys. We can do this by placing a spot (marker) on the appropriate coordinates, which may be adjusted using keys.
Change in Coordinates of marker for respective keys pressed :
Left arrow key: Decrement in x coordinate
Right arrow key: Increment in x coordinate
Up arrow key: Decrement in the y coordinate
Down arrow key: Increment in the y coordinate
Below is the implementation
# importing the pygame module in the program
import pygame
# activates the pygame library .
# initiating pygame and gave permission
# to use the pygame functionality.
pygame.init()
# creating the display surface object
# of specific dimension..e(500, 500).
wn = pygame.display.set_mode((500, 500))
# setting the pygame window name
pygame.display.set_caption("Moving rectangle")
# marker current coordinates
x_new = 200
y_new = 200
# dimensions of the marker
wdth = 10
hght = 10
# velocity / speed of movement
velocity = 10
# Indicates pygame is running
rn = True
# infinite loop
while rn:
# creates time delay of 10ms
pygame.time.delay(10)
# iterate over the list of Event objects
# that was returned by pygame.event.get() method.
for vnt in pygame.event.get():
# if event object type is QUIT
# then quitting the pygame
# and program both.
if vnt.type == pygame.QUIT:
# it will make exit the while loop
rn = False
# stores keys pressed
kys = pygame.key.get_pressed()
# if left arrow key is pressed
if kys[pygame.K_LEFT] and x_new > 0:
# decrement in x co-ordinate
x_new -= velocity
# if left arrow key is pressed
if kys[pygame.K_RIGHT] and x_new < 500 - width:
# increment in x co-ordinate
x_new += velocity
# if left arrow key is pressed
if kys[pygame.K_UP] and y_new > 0:
# decrement in y co-ordinate
y_new -= velocity
# if left arrow key is pressed
if kys[pygame.K_DOWN] and y_new < 500 - height:
# increment in y co-ordinate
y += velocity
# drawing spot on screen which is rectangle here
pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))
# it refreshes the window
pygame.display.update()
# closes the pygame window
pygame.quit()