Pygame is a set of cross-platform Python modules designed specifically for writing video games. It includes computer graphics and sound libraries designed for use with the Python programming language. You can use PyGame to create different types of games, including arcade games, platformers, and more.
Images used:
You can control the player’s movement. To do this, first create a display object using PyGame’s display.set_mode() method and add the player’s Sprite using PyGame’s image.load() method. The set_mode() function is used to initialize the display surface or window. The size argument is a pair of numbers representing the width and height. The FLAGS argument is a collection of additional options. The depth parameter represents the number of bits used for the color.
Grammar:
set_mode(size=(0, 0), flags=0, depth=0, display=0, vsync=0)
Create a variable to store the player’s speed. Set the player’s initial coordinates. Now, change the x and Y coordinates of the player based on keyboard events that occur when the key state changes.
The blit(surface,surfacerect) function is used to draw images on the screen.
Grammar:
blit(surface, surfacerect)
To collect all the events from the queue, use the get() function of the event module, and then we iterate over all the events using the for loop.
Grammar:
get(eventtype=None)
Update the screen with the update() function of the display module. Finally, if your time is not very tight, and want to quickly improve, the most important thing is not afraid of hardship, I suggest you can price @762459510, that is really very good, many people progress quickly, need you not afraid of hardship oh! You can go to add a look at ~
Grammar:
update(rectangle=None)
Here is the implementation
Example: player movement application
# Import pyGame module
import pygame
from pygame.locals import *
Start PyGame and grant permission to use PyGame functionality
pygame.init()
Create a display surface object of a specific size
window = pygame.display.set_mode((600.600))
# Add a title to the window
pygame.display.set_caption('Player Movement')
# Add player sprites
image = pygame.image.load(r'haiyong.png')
Store the player's initial coordinates in two variables, x and Y
x = 100
y = 100
Create a variable to store how fast the player moves
velocity = 12
Create an infinite loop
run = True
while run:
Fill the background with white
window.fill((255.255.255))
# Display player sprites at x and y coordinates
window.blit(image, (x, y))
# Iterate over the list of event objects returned by the pygame.event.get() method.
for event in pygame.event.get():
# If the event type is QUIT, close the window and program
if event.type == pygame.QUIT:
run = False
pygame.quit()
quit()
Check the event key if the event type is KEYDOWN, i.e. the keyboard button is pressed
if event.type == pygame.KEYDOWN:
If the left arrow key is pressed, decrease the x coordinate
if event.key == pygame.K_LEFT:
x -= velocity
If the right arrow key is pressed, add the x coordinate
if event.key == pygame.K_RIGHT:
x += velocity
If the up arrow key is pressed, decrease the y coordinate
if event.key == pygame.K_UP:
y -= velocity
If the down arrow key is pressed, increase the y coordinate
if event.key == pygame.K_DOWN:
y += velocity
Draw the surface object to the screen
pygame.display.update()
Copy the code
Output:
The player can also move continuously. To this end, everything remains the same except for a few changes. Here, we create a new clock object to use clock() to control the frame rate of the game. Finally, if your time is not very tight, and want to quickly improve, the most important thing is not afraid of hardship, I suggest you can price @762459510, that is really very good, many people progress quickly, need you not afraid of hardship oh! You can go to add a look at ~
grammar
Clock()
Create a new variable (named key_pressed_is) to store the key pressed by the user. To do this, we use the get_pressed() function of the key module.