
PyGame is a well-liked library for constructing tasks with Python and offers a strong toolset for recreation improvement. On this article, you’ll discover ways to create a begin menu and recreation over display for a easy recreation utilizing PyGame.
Making a Easy Recreation
Earlier than we create the beginning menu and recreation over display, let’s make a easy recreation. On this recreation, you’ll management a personality with the arrow keys and attempt to keep away from obstacles. Attempt to not use any photos to maintain issues easy.
To get began, you’ll want to import the mandatory modules. you’ll be utilizing pygame Module for accessing PyGame’s features. To put in the module you’ll be able to: Use the pip package manager:
pip set up pygame
Now that our module is able to use, let’s create a easy recreation with a participant and an impediment that may transfer left or proper utilizing the arrow keys. In the event you collide with the impediment, the sport will probably be over. Right here is similar code:
import pygame
pygame.init()screen_width = 750
screen_height = 450
display = pygame.show.set_mode((screen_width, screen_height))
obstacle_x = 400
obstacle_y = 400
obstacle_width = 40
obstacle_height = 40
player_x = 200
player_y = 400
player_width = 20
player_height = 20
whereas True:
for occasion in pygame.occasion.get():
if occasion.sort == pygame.QUIT:
pygame.give up()
give up()
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
player_x -= 5
if keys[pygame.K_RIGHT]:
player_x += 5
if player_x + player_width > obstacle_x and player_x < obstacle_x + obstacle_width and player_y + player_height > obstacle_y and player_y < obstacle_y + obstacle_height:
game_over = True
display.fill((0, 0, 0))
pygame.draw.rect(display, (255, 0, 0), (obstacle_x, obstacle_y, obstacle_width, obstacle_height))
pygame.draw.rect(display, (0, 255, 0), (player_x, player_y, player_width, player_height))
pygame.show.replace()
Within the code above you’ve got arrange the sport window and declared the sport variables. You have additionally logged in and drawn objects onto the display.
Now that you’ve a base recreation, let’s create a begin menu. To do that you have to to declare a worldwide variable for the sport state. This variant will preserve monitor of the present state of the sport, whether or not the beginning menu is energetic or the sport is working. You’ll want to add this variable originally of your recreation code:
game_state = "start_menu"
Subsequent, you will add a perform to attract the beginning menu onto the display. You should utilize PyGame features to attract textual content, buttons, and different objects on the display.
def draw_start_menu():
display.fill((0, 0, 0))
font = pygame.font.SysFont('arial', 40)
title = font.render('My Recreation', True, (255, 255, 255))
start_button = font.render('Begin', True, (255, 255, 255))
display.blit(title, (screen_width/2 - title.get_width()/2, screen_height/2 - title.get_height()/2))
display.blit(start_button, (screen_width/2 - start_button.get_width()/2, screen_height/2 + start_button.get_height()/2))
pygame.show.replace()
you’ll be able to add later draw_start_menu() perform to the principle recreation loop.
whereas True: for occasion in pygame.occasion.get():
if occasion.sort == pygame.QUIT:
pygame.give up()
give up()
if game_state == "start_menu":
draw_start_menu()
if game_state == "recreation":
keys = pygame.key.get_pressed()
Now, the beginning menu will probably be drawn to the display. The ultimate step is the processing of person enter. You are able to do add an if statement go to the principle recreation loop to verify if the person has pressed the beginning button.
if game_state == "start_menu":
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
player_x = 200
player_y = 400
game_state = "recreation"
game_over = False
With this code, the sport will begin as quickly because the person presses the beginning button.
Implementing the Recreation Over Display
Now that you’ve the beginning menu, let’s create the sport over display. This perform ought to show the ultimate rating and a recreation over message.
def draw_game_over_screen():
display.fill((0, 0, 0))
font = pygame.font.SysFont('arial', 40)
title = font.render('Recreation Over', True, (255, 255, 255))
restart_button = font.render('R - Restart', True, (255, 255, 255))
quit_button = font.render('Q - Stop', True, (255, 255, 255))
display.blit(title, (screen_width/2 - title.get_width()/2, screen_height/2 - title.get_height()/3))
display.blit(restart_button, (screen_width/2 - restart_button.get_width()/2, screen_height/1.9 + restart_button.get_height()))
display.blit(quit_button, (screen_width/2 - quit_button.get_width()/2, screen_height/2 + quit_button.get_height()/2))
pygame.show.replace()
You’ll be able to then add this performance to the principle recreation loop.
if game_state == "start_menu":
draw_start_menu()
if game_state == "game_over":
draw_game_over_screen()
if game_state == "recreation":
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
player_x -= 5
if keys[pygame.K_RIGHT]:
player_x += 5
if player_x + player_width > obstacle_x and player_x < obstacle_x + obstacle_width and player_y + player_height > obstacle_y and player_y < obstacle_y + obstacle_height:
game_over = True
game_state = "game_over"
Lastly, you’ll want to course of person enter to make the person’s choice. You’ll be able to add an if assertion to the principle recreation loop to verify if the person presses the restart or exit button.
if game_state == "start_menu":
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
game_state = "recreation"
player_x = 200
player_y = 400
game_state = "recreation"
game_over = Falseif game_state == "game_over":
keys = pygame.key.get_pressed()
if keys[pygame.K_r]:
game_state = "start_menu"
if keys[pygame.K_q]:
pygame.give up()
give up()
With this code, the sport will restart when the person presses the ‘R’ key and when the person presses the ‘Q’ key on the keyboard, the sport will exit.
The total code is under:
import pygamepygame.init()
screen_width = 750
screen_height = 450
display = pygame.show.set_mode((screen_width, screen_height))
obstacle_x = 400
obstacle_y = 400
obstacle_width = 40
obstacle_height = 40
player_x = 200
player_y = 400
player_width = 20
player_height = 20
game_state = "start_menu"
def draw_start_menu():
display.fill((0, 0, 0))
font = pygame.font.SysFont('arial', 40)
title = font.render('My Recreation', True, (255, 255, 255))
start_button = font.render('Begin', True, (255, 255, 255))
display.blit(title, (screen_width/2 - title.get_width()/2, screen_height/2 - title.get_height()/2))
display.blit(start_button, (screen_width/2 - start_button.get_width()/2, screen_height/2 + start_button.get_height()/2))
pygame.show.replace()
def draw_game_over_screen():
display.fill((0, 0, 0))
font = pygame.font.SysFont('arial', 40)
title = font.render('Recreation Over', True, (255, 255, 255))
restart_button = font.render('R - Restart', True, (255, 255, 255))
quit_button = font.render('Q - Stop', True, (255, 255, 255))
display.blit(title, (screen_width/2 - title.get_width()/2, screen_height/2 - title.get_height()/3))
display.blit(restart_button, (screen_width/2 - restart_button.get_width()/2, screen_height/1.9 + restart_button.get_height()))
display.blit(quit_button, (screen_width/2 - quit_button.get_width()/2, screen_height/2 + quit_button.get_height()/2))
pygame.show.replace()
whereas True:
for occasion in pygame.occasion.get():
if occasion.sort == pygame.QUIT:
pygame.give up()
give up()
if game_state == "start_menu":
draw_start_menu()
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
player_x = 200
player_y = 400
game_state = "recreation"
game_over = False
elif game_state == "game_over":
draw_game_over_screen()
keys = pygame.key.get_pressed()
if keys[pygame.K_r]:
game_state = "start_menu"
if keys[pygame.K_q]:
pygame.give up()
give up()
elif game_state == "recreation":
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
player_x -= 5
if keys[pygame.K_RIGHT]:
player_x += 5
if player_x + player_width > obstacle_x and player_x < obstacle_x + obstacle_width and player_y + player_height > obstacle_y and player_y < obstacle_y + obstacle_height:
game_over = True
game_state = "game_over"
display.fill((0, 0, 0))
pygame.draw.rect(display, (255, 0, 0), (obstacle_x, obstacle_y, obstacle_width, obstacle_height))
pygame.draw.rect(display, (0, 255, 0), (player_x, player_y, player_width, player_height))
pygame.show.replace()
elif game_over:
game_state = "game_over"
game_over = False
The code begins by importing pygame module and initialization. It then creates a recreation window and studies the required recreation variables, together with the place, measurement and recreation state of the participant and impediment.
The code defines two features, draw_start_menu() and draw_game_over_screen(), to attract the beginning menu and recreation display. These features use PyGame features to attract textual content and buttons on the display.
The primary recreation loop begins by dealing with occasions and checking the sport state. If the sport state begin menu, the beginning menu is drawn on the display. If the sport state recreation over, the sport over display is drawn on the display. If the sport state is one thing else, the sport updates and pulls the participant and impediment onto the display.
The sport is up to date by urgent the keys and checking if there’s a collision between the participant and the impediment. If a collision happens, the sport recreation over Mark TRUE and set the sport state like this: recreation over.
After the sport is up to date, the participant and the impediment are drawn on the display. Lastly, the code, recreation over the flag is ready and whether it is, it resets the flag and units the sport state like this: recreation over.
This course of is repeated constantly till the sport window is closed.
Enhance Consumer Interplay with a Visually Enticing Consumer Interface
Utilizing a well-designed person interface (UI) can drastically enhance person engagement in a PyGame recreation. A transparent and easy-to-read font, visually interesting graphics, simple navigation, and person suggestions are essential parts to think about when designing the person interface. By implementing these parts, you’ll be able to create a person interface that helps preserve the participant engaged and immersed within the recreation.
Remember to check your UI and get suggestions from gamers to ensure it is efficient at growing engagement. Total, taking the time to create a strong UI can drastically improve the participant’s gaming expertise and delight from the sport.
#Create #Begin #Menu #Recreation #Display #PyGame