This article has participated in the activity of “New person creation Ceremony”, and started the road of digging gold creation together.
# PyGame
This game is implemented using the PyGame module from Pycharm, which can also be run in Python.
Introduce the module
Minimum development framework
Pygame minimum development framework in detail
Pygame and SYS modules
import pygame # Modules to use when making a game
import sys The python standard library initializes the creation of system modules
Copy the code
The random module
You need to randomly generate balls on the screen
from random import randint
Copy the code
See this article: python-random module for details
Related functions
Window size change
Adjust the size of the game screen
Change the window size
elif event.type == pygame.VIDEORESIZE:
size = w,h = event.w,event.h
screen = pygame.display.set_mode(size,pygame.RESIZABLE)
Copy the code
VIDEORESIZE PyGame.videoreSize This is a window resize event. After the event occurs, return the event. Size tuple containing the width and height of the new window. Size [0] height, which can also be represented by event.w. Size [1] width, which can also be represented by event.h. The return parameter is useful only when an event occurs
Keyboard control baffle
# Keyboard control baffle
elif event.type == pygame.KEYDOWN: # Keyboard press event detection
if event.key == pygame.K_LEFT: Check whether the baffle moves to the left
if board_rect.left > 0 and board_rect.left <= w - 186:
board_rect.left -= board_x
elif board_rect.left <= 0: Check whether the left coordinate of the baffle is less than 0
board_rect.left = 0
board_rect.top -= board_y
elif event.key == pygame.K_RIGHT: Check whether the baffle moves right
if board_rect.right >= 186 and board_rect.right < w:
board_rect.right += board_x
elif board_rect.right >= w: Board_rect. right = w
board_rect.bottom += board_y
Copy the code
The mouse control
# Mouse control baffle
elif event.type == pygame.MOUSEMOTION:
# Left mouse button down and follow the mouse movement
if event.buttons[0] = =1:
if event.pos[0] > =0 and event.pos[0] < w - 186:# Determine mouse position
board_rect.left = event.pos[0] # Place the mouse's x coordinate to the left of the Rect object
elif event.pos[0] >= w - 186 and event.pos[0] <= w:
board_rect.left = w - 186
# board_rect.top = H-17 # baffle position at bottom
elif event.type == pygame.MOUSEBUTTONDOWN: # Mouse button down
# Move the current mouse position to the baffle
if event.button == 1:
if event.pos[0] > =0 and event.pos[0] < w - 186:# Determine mouse position
board_rect.left = event.pos[0] # Place the mouse's x coordinate to the left of the Rect object
if event.pos[0] >= w - 186 and event.pos[0] <= w:
board_rect.left = w - 186
# board_rect.top = h - 17
Copy the code
The backstop catches the ball and scores
# The lower baffle is connected to the ball
if ball_y >= h - 37 and (ball_x >= board_rect.left - 20 and ball_x <= board_rect.left + 206):
move_y = - move_y The velocity in the y direction is reversed
score += points # score
count += 1 Increase the number of times by 1
if count == 5: # Difficulty and points per catch increase with every 5 hits
count = 0 # The number of catches scored is cleared
points += points
The velocity in the x direction is increasing
if move_x > 0:
move_x += 1
else:
move_x -= 1
move_y -= 1
Copy the code
The ball missed the ball
# The lower baffle did not catch the ball
if ball_y > h - 27 and (ball_x < board_rect.left - 20 or ball_x > board_rect.left + 206) :# Game over
ball_y = 200 # The position of the ball
break
Copy the code
A ball moving
# Move the ball
ball_x += move_x
ball_y += move_y
if ball_x <= 20 or ball_x >= w - 20: # Touch left and right walls
move_x = - move_x # reverse velocity in the x direction
if ball_y <= 20: # Touch the wall above
move_y = - move_y The velocity in the y direction is reversed
Copy the code
According to score
my_score = font.render(str(score), False, (255.255.0)) Create text object (text, whether smooth, text color)
screen.blit(my_score, (w - 100.30)) # Add text to the window
Copy the code
The complete code
import sys
from random import randint
import pygame
pygame.init() # initialization
size = w, h = (600.500) # Screen display area, height and width
screen = pygame.display.set_mode(size,pygame.RESIZABLE)
pygame.display.set_caption("Catch the ball") # Screen title
fpsClock = pygame.time.Clock() The higher the window refresh speed, the faster it will run
board = pygame.image.load(R "D:\pycharm\WorkTime (soph) \ fend.jpg")
board_rect = board.get_rect() Create recT with surface
color = pygame.Color(255.255.255) # Screen (window) color: white
Green = pygame.Color('green') # Ball color: green
# Randomly generate the ball's X and y coordinates (integers, including both ends)
ball_x = randint(20.580)
ball_y = randint(20.200)
# Change in the x and y coordinates of the ball
move_x = 1
move_y = 1
# Change of x and y coordinates of baffle plate
board_x = 46
board_y = 0
score=0 # score
font=pygame.font.Font(R'd: font Library Calligraphy and New Font Library Microsoft Yahe.ttf '.60) Set the font (the former is the font path) and font size
points=1 # Bonus points for a catch
count=0 # Number of catches scored
# size1 = board.get_size(
# print(size1)
while True:
board_rect.top = h - 17
for event in pygame.event.get(): # pygame.event.get() fetches the event from the event queue and removes the event from the queue
if event.type == pygame.QUIT:
sys.exit()
Change the window size
elif event.type == pygame.VIDEORESIZE:
size = w,h = event.w,event.h
screen = pygame.display.set_mode(size,pygame.RESIZABLE)
# Keyboard control baffle
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT: # Move the baffle left
if board_rect.left > 0 and board_rect.left <= w - 186:
board_rect.left -= board_x
elif board_rect.left <= 0: Check whether the left coordinate of the baffle is less than 0
board_rect.left = 0
board_rect.top -= board_y
elif event.key == pygame.K_RIGHT: # Move the baffle right
if board_rect.right >= 186 and board_rect.right < w:
board_rect.right += board_x
elif board_rect.right >= w: Board_rect. right = w
board_rect.bottom += board_y
# Mouse control baffle
elif event.type == pygame.MOUSEMOTION:
# Left mouse button down and follow the mouse movement
if event.buttons[0] = =1:
if event.pos[0] > =0 and event.pos[0] < w - 186:# Determine mouse position
board_rect.left = event.pos[0] # Place the mouse's x coordinate to the left of the Rect object
elif event.pos[0] >= w - 186 and event.pos[0] <= w:
board_rect.left = w - 186
# board_rect.top = H-17 # baffle position at bottom
elif event.type == pygame.MOUSEBUTTONDOWN: # Mouse button down
# Move the current mouse position to the baffle
if event.button == 1:
if event.pos[0] > =0 and event.pos[0] < w - 186:# Determine mouse position
board_rect.left = event.pos[0] # Place the mouse's x coordinate to the left of the Rect object
if event.pos[0] >= w - 186 and event.pos[0] <= w:
board_rect.left = w - 186
# board_rect.top = h - 17
# The lower baffle is connected to the ball
if ball_y >= h - 37 and (ball_x >= board_rect.left - 20 and ball_x <= board_rect.left + 206):
move_y = - move_y The velocity in the y direction is reversed
score += points
count += 1
if count == 5: # Difficulty and points per catch increase with every 5 hits
count = 0 # The number of catches scored is cleared
points += points
The velocity in the x direction is increasing
if move_x > 0:
move_x += 1
else:
move_x -= 1
move_y -= 1
# The lower baffle did not catch the ball
if ball_y > h - 27 and (ball_x < board_rect.left - 20 or ball_x > board_rect.left + 206) :# Game over
ball_y = 200
break
# Move the ball
ball_x += move_x
ball_y += move_y
if ball_x <= 20 or ball_x >= w - 20: # Touch left and right walls
move_x = - move_x # reverse velocity in the x direction
if ball_y <= 20: # Touch the wall above
move_y = - move_y The velocity in the y direction is reversed
fpsClock.tick(200)
screen.fill(color)
# display score
my_score = font.render(str(score), False, (255.255.0)) Create text object (text, whether smooth, text color)
screen.blit(my_score, (w - 100.30)) # Add text to the window
screen.blit(board,board_rect) Overlay the surface object over the moved rect object
pygame.draw.circle(screen, Green, (ball_x, ball_y), 20) # Draw the ball
pygame.display.update() # Update the display window, redraw the default window
Copy the code