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