Python implements the brick-and-mortar game

The examples in this article share the specific code of Python to realize the brick-and-mortar game for your reference. The specific content is as follows

# Import module
import pygame
from pygame.locals import*import sys,random,time,math
classGameWindow(object):'''Create game window class'''
def __init__(self,*args,**kw): 
self.window_length =600
self.window_wide =500
# Draw the game window, set the window size
self.game_window = pygame.display.set_mode((self.window_length,self.window_wide))
# Set the title of the game window
pygame.display.set_caption("CatchBallGame")
# Define the background color parameters of the game window
self.window_color =(135,206,250)
def backgroud(self):
# Draw the background color of the game window
self.game_window.fill(self.window_color)classBall(object):'''Create ball'''
def __init__(self,*args,**kw):
# Set the radius, color and moving speed parameters of the ball
self.ball_color =(255,215,0) 
self.move_x =1
self.move_y =1
self.radius =10
def ballready(self):
# Set the initial position of the ball,
self.ball_x = self.mouse_x
self.ball_y = self.window_wide-self.rect_wide-self.radius
# Draw the ball and set the bounce trigger condition
pygame.draw.circle(self.game_window,self.ball_color,(self.ball_x,self.ball_y),self.radius)
def ballmove(self):
# Draw the ball and set the bounce trigger condition
pygame.draw.circle(self.game_window,self.ball_color,(self.ball_x,self.ball_y),self.radius) 
self.ball_x += self.move_x
self.ball_y -= self.move_y
# Call the collision detection function
self.ball_window()
self.ball_rect()
# Ball speed doubles every 5 times the ball is received
if self.distance < self.radius:
self.frequency +=1if self.frequency ==5:
self.frequency =0
self.move_x += self.move_x
self.move_y += self.move_y
self.point += self.point
# Set game failure conditions
if self.ball_y   520:
self.gameover = self.over_font.render("Game Over",False,(0,0,0))
self.game_window.blit(self.gameover,(100,130))
self.over_sign =1classRect(object):'''Create a racket class'''
def __init__(self,*args,**kw):
# Set the racket color parameters
self.rect_color =(255,0,0)
self.rect_length =100
self.rect_wide =10
def rectmove(self):
# Get mouse position parameters
self.mouse_x,self.mouse_y = pygame.mouse.get_pos()
# Draw the racket and define the horizontal boundary
if self.mouse_x  = self.window_length-self.rect_length//2:
self.mouse_x = self.window_length-self.rect_length//2if self.mouse_x <= self.rect_length//2:
self.mouse_x = self.rect_length//2
pygame.draw.rect(self.game_window,self.rect_color,((self.mouse_x-self.rect_length//2),(self.window_wide-self.rect_wide),self.rect_length,self.rect_wide))classBrick(object):
def __init__(self,*args,**kw):
# Set brick color parameters
self.brick_color =(139,126,102)
self.brick_list =[[1,1,1,1,1,1],[1,1,1,1,1,1],[1,1,1,1,1,1],[1,1,1,1,1,1],[1,1,1,1,1,1]]
self.brick_length =80
self.brick_wide =20
def brickarrange(self):for i inrange(5):for j inrange(6):
self.brick_x = j*(self.brick_length+24)
self.brick_y = i*(self.brick_wide+20)+40if self.brick_list[i][j]==1:
# Draw bricks
pygame.draw.rect(self.game_window,self.brick_color,(self.brick_x,self.brick_y,self.brick_length,self.brick_wide)) 
# Call the collision detection function
self.ball_brick()if self.distanceb < self.radius:
self.brick_list[i][j]=0
self.score += self.point
# Set game victory conditions
if self.brick_list ==[[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0]]:
self.win = self.win_font.render("You Win",False,(0,0,0))
self.game_window.blit(self.win,(100,130))
self.win_sign =1classScore(object):'''Create a score class'''
def __init__(self,*args,**kw): 
# Set initial score
self.score =0
# Set fraction font
self.score_font = pygame.font.SysFont('arial',20)
# Set initial bonus points
self.point =1
# Set the initial number of catches
self.frequency =0
def countscore(self):
# Plot player scores
my_score = self.score_font.render(str(self.score),False,(255,255,255))
self.game_window.blit(my_score,(555,15))classGameOver(object):'''Create game over class'''
def __init__(self,*args,**kw):
# Set the Game Over font
self.over_font = pygame.font.SysFont('arial',80)
# Define GameOver logo
self.over_sign =0classWin(object):'''Create game victory class'''
def __init__(self,*args,**kw):
# Set You Win font
self.win_font = pygame.font.SysFont('arial',80)
# Define Win logo
self.win_sign =0classCollision(object):'''Collision detection class'''
# Collision detection between the ball and the window frame
def ball_window(self):if self.ball_x <= self.radius or self.ball_x  =(self.window_length-self.radius):
self.move_x =-self.move_x
if self.ball_y <= self.radius:
self.move_y =-self.move_y
# Ball and racket collision detection
def ball_rect(self):
# Define collision markers
self.collision_sign_x =0
self.collision_sign_y =0if self.ball_x <(self.mouse_x-self.rect_length//2):
self.closestpoint_x = self.mouse_x-self.rect_length//2
self.collision_sign_x =1
elif self.ball_x(self.mouse_x+self.rect_length//2):
self.closestpoint_x = self.mouse_x+self.rect_length//2
self.collision_sign_x =2else:
self.closestpoint_x = self.ball_x
self.collision_sign_x =3if self.ball_y <(self.window_wide-self.rect_wide):
self.closestpoint_y =(self.window_wide-self.rect_wide)
self.collision_sign_y =1
elif self.ball_y   self.window_wide:
self.closestpoint_y = self.window_wide
self.collision_sign_y =2else:
self.closestpoint_y = self.ball_y
self.collision_sign_y =3
# Define the distance between the closest point of the racket to the center of the circle and the center of the circle
self.distance = math.sqrt(math.pow(self.closestpoint_x-self.ball_x,2)+math.pow(self.closestpoint_y-self.ball_y,2))
# Collision detection of the ball on the racket in three situations: left, upper middle, upper right
if self.distance < self.radius and self.collision_sign_y ==1and(self.collision_sign_x ==1 or self.collision_sign_x ==2):if self.collision_sign_x ==1 and self.move_x   0:
self.move_x =- self.move_x
self.move_y =- self.move_y
if self.collision_sign_x ==1 and self.move_x <0:
self.move_y =- self.move_y
if self.collision_sign_x ==2 and self.move_x <0:
self.move_x =- self.move_x
self.move_y =- self.move_y
if self.collision_sign_x ==2 and self.move_x   0:
self.move_y =- self.move_y
if self.distance < self.radius and self.collision_sign_y ==1 and self.collision_sign_x ==3:
self.move_y =- self.move_y
# Collision detection of the ball in the middle of the left and right sides of the racket
if self.distance < self.radius and self.collision_sign_y ==3:
self.move_x =- self.move_x
# Ball and brick collision detection
def ball_brick(self):
# Define collision markers
self.collision_sign_bx =0
self.collision_sign_by =0if self.ball_x < self.brick_x:
self.closestpoint_bx = self.brick_x
self.collision_sign_bx =1
elif self.ball_x   self.brick_x+self.brick_length:
self.closestpoint_bx = self.brick_x+self.brick_length
self.collision_sign_bx =2else:
self.closestpoint_bx = self.ball_x
self.collision_sign_bx =3if self.ball_y < self.brick_y:
self.closestpoint_by = self.brick_y
self.collision_sign_by =1
elif self.ball_y   self.brick_y+self.brick_wide:
self.closestpoint_by = self.brick_y+self.brick_wide
self.collision_sign_by =2else:
self.closestpoint_by = self.ball_y
self.collision_sign_by =3
# Define the distance between the nearest point of the brick to the center of the circle and the center of the circle
self.distanceb = math.sqrt(math.pow(self.closestpoint_bx-self.ball_x,2)+math.pow(self.closestpoint_by-self.ball_y,2))
# Collision detection of the ball on the bricks in three situations: left, upper middle, upper right
if self.distanceb < self.radius and self.collision_sign_by ==1and(self.collision_sign_bx ==1 or self.collision_sign_bx ==2):if self.collision_sign_bx ==1 and self.move_x   0:
self.move_x =- self.move_x
self.move_y =- self.move_y
if self.collision_sign_bx ==1 and self.move_x <0:
self.move_y =- self.move_y
if self.collision_sign_bx ==2 and self.move_x <0:
self.move_x =- self.move_x
self.move_y =- self.move_y
if self.collision_sign_bx ==2 and self.move_x   0:
self.move_y =- self.move_y
if self.distanceb < self.radius and self.collision_sign_by ==1 and self.collision_sign_bx ==3:
self.move_y =- self.move_y
# Collision detection of the ball in the bottom left, bottom middle and bottom right of the brick
if self.distanceb < self.radius and self.collision_sign_by ==2and(self.collision_sign_bx ==1 or self.collision_sign_bx ==2):if self.collision_sign_bx ==1 and self.move_x   0:
self.move_x =- self.move_x
self.move_y =- self.move_y
if self.collision_sign_bx ==1 and self.move_x <0:
self.move_y =- self.move_y
if self.collision_sign_bx ==2 and self.move_x <0:
self.move_x =- self.move_x
self.move_y =- self.move_y
if self.collision_sign_bx ==2 and self.move_x   0:
self.move_y =- self.move_y
if self.distanceb < self.radius and self.collision_sign_by ==2 and self.collision_sign_bx ==3:
self.move_y =- self.move_y
# Collision detection of the ball in the middle of the left and right sides of the brick
if self.distanceb < self.radius and self.collision_sign_by ==3:
self.move_x =- self.move_x
classMain(GameWindow,Rect,Ball,Brick,Collision,Score,Win,GameOver):'''Create the main program class'''
def __init__(self,*args,**kw):super(Main,self).__init__(*args,**kw)super(GameWindow,self).__init__(*args,**kw)super(Rect,self).__init__(*args,**kw)super(Ball,self).__init__(*args,**kw)super(Brick,self).__init__(*args,**kw)super(Collision,self).__init__(*args,**kw)super(Score,self).__init__(*args,**kw)super(Win,self).__init__(*args,**kw)
# Define the game start flag
start_sign =0while True: 
self.backgroud()
self.rectmove()
self.countscore()if self.over_sign ==1 or self.win_sign ==1:break
# Get the state of the game window
for event in pygame.event.get():if event.type == pygame.QUIT:
sys.exit()if event.type == MOUSEBUTTONDOWN:
pressed_array = pygame.mouse.get_pressed()if pressed_array[0]:
start_sign =1if start_sign ==0:
self.ballready()else:
self.ballmove()
self.brickarrange()
# Update the game window
pygame.display.update()
# Control the refresh rate of the game window
time.sleep(0.010)if __name__ =='__main__':
pygame.init()
pygame.font.init()
catchball =Main()

The above is the whole content of this article, I hope it will be helpful to everyone's study.

Recommended Posts

Python implements the brick-and-mortar game
Python simply implements the snake game
Python implements tic-tac-toe game
Python implements guessing game
Python implements the source code of the snake game
python guess the word game
Python implements a guessing game
Python implements digital bomb game
Python realizes the guessing game
Python implements simple tic-tac-toe game
How Python implements the mail function
Python3 implements the singleton design pattern
Python implements the steepest descent method
Python implements digital bomb game program
Python implements the aircraft war project
Python solves the Tower of Hanoi game
Python implements the sum of fractional sequences
Python basic actual combat-guess the age game
python implements the gradient method python the fastest descent method
python3 simply implements the combined design pattern
Python implements the shuffling of the cards in Doudizhu
Python writes the game implementation of fishing master
Use python to realize the aircraft war game
Python implements Super Mario
Python implements man-machine gobang
Python implements image stitching
Python implements scanning tools
Python implements threshold regression
Python implements minesweeper games
Python implements electronic dictionary
Realize the tank battle game with Python | Dry Post
Realize the tank battle game with Python | Dry Post
Python implements simple tank battle
Python3 realizes airplane war game
Consolidate the foundation of Python(7)
Python implements udp chat window
Consolidate the foundation of Python(6)
Ubuntu install the latest Python 3.
Python implements parking management system
Python implements TCP file transfer
The difference between Python extensions
Python numpy implements rolling case
OpenCV Python implements puzzle games
Python implements password strength verification
Python implements car management system
Python implements code block folding
Python implements panoramic image stitching
Python implements SMTP mail sending
Python implements multi-dimensional array sorting
How Python implements FTP function
Python implements mean-shift clustering algorithm
Python implements verification code recognition
Python implements gradient descent method
Python implements text version minesweeper
Consolidate the foundation of Python (3)
Python implements image stitching function
The usage of wheel in python
Python implements student performance evaluation system
How does python change the environment
Python handles the 4 wheels of Chinese
Python implements exchange rate conversion operations