打砖块小游戏 Python3实现打砖块小游戏ok

编辑:
发布时间: 2020-12-11 16:10:24
分享:

用Python实现的是一个接球打砖块的小游戏,需要导入pygame模块,有以下两条经验总结:

1.多父类的继承2.碰撞检测的数学模型

知识点稍后再说,我们先看看游戏的效果和实现:

一、游戏效果

二、游戏代码

#导入模块import pygamefrom pygame.locals import *import sys,random,time,mathclass GameWindow:"""创建游戏窗口类"""def __init__:self.window_length = 600self.window_wide = 500#绘制游戏窗口,设置窗口尺寸self.game_window = pygame.display.set_mode)#设置游戏窗口标题pygame.display.set_caption#定义游戏窗口背景颜色参数self.window_color = def backgroud:#绘制游戏窗口背景颜色self.game_window.fillclass Ball:"""创建球类"""def __init__:#设置球的半径、颜色、移动速度参数self.ball_color = self.move_x = 1self.move_y = 1self.radius = 10def ballready:#设置球的初始位置、self.ball_x = self.mouse_xself.ball_y = self.window_wide-self.rect_wide-self.radius#绘制球,设置反弹触发条件pygame.draw.circle,self.radius)def ballmove:#绘制球,设置反弹触发条件pygame.draw.circle,self.radius)self.ball_x += self.move_xself.ball_y -= self.move_y#调用碰撞检测函数self.ball_windowself.ball_rect#每接5次球球速增加一倍if self.distance < self.radius:self.frequency += 1if self.frequency == 5:self.frequency = 0self.move_x += self.move_xself.move_y += self.move_yself.point += self.point#设置游戏失败条件if self.ball_y > 520:self.gameover = self.over_font.render)self.game_window.blit)self.over_sign = 1class Rect:"""创建球拍类"""def __init__:#设置球拍颜色参数self.rect_color = self.rect_length = 100self.rect_wide = 10def rectmove:#获取鼠标位置参数self.mouse_x,self.mouse_y = pygame.mouse.get_pos#绘制球拍,限定横向边界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//2pygame.draw.rect,,self.rect_length,self.rect_wide))class Brick:def __init__:#设置砖块颜色参数self.brick_color = 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 = 80self.brick_wide = 20def brickarrange:for i in range:for j in range:self.brick_x = j*self.brick_y = i*+40if self.brick_list[i][j] == 1:#绘制砖块pygame.draw.rect)#调用碰撞检测函数self.ball_brickif self.distanceb < self.radius:self.brick_list[i][j] = 0self.score += self.point#设置游戏胜利条件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)self.game_window.blit)self.win_sign = 1class Score:"""创建分数类"""def __init__:#设置初始分数self.score = 0#设置分数字体self.score_font = pygame.font.SysFont#设置初始加分点数self.point = 1#设置初始接球次数self.frequency = 0def countscore:#绘制玩家分数my_score = self.score_font.render,False,)self.game_window.blit)class GameOver:"""创建游戏结束类"""def __init__:#设置Game Over字体self.over_font = pygame.font.SysFont#定义GameOver标识self.over_sign = 0class Win:"""创建游戏胜利类"""def __init__:#设置You Win字体self.win_font = pygame.font.SysFont#定义Win标识self.win_sign = 0class Collision:"""碰撞检测类"""#球与窗口边框的碰撞检测def ball_window:if self.ball_x <= self.radius or self.ball_x >= :self.move_x = -self.move_xif self.ball_y <= self.radius:self.move_y = -self.move_y#球与球拍的碰撞检测def ball_rect:#定义碰撞标识self.collision_sign_x = 0self.collision_sign_y = 0if self.ball_x < :self.closestpoint_x = self.mouse_x-self.rect_length//2self.collision_sign_x = 1elif self.ball_x > :self.closestpoint_x = self.mouse_x+self.rect_length//2self.collision_sign_x = 2else:self.closestpoint_x = self.ball_xself.collision_sign_x = 3if self.ball_y < :self.closestpoint_y = self.collision_sign_y = 1elif self.ball_y > self.window_wide:self.closestpoint_y = self.window_wideself.collision_sign_y = 2else:self.closestpoint_y = self.ball_yself.collision_sign_y = 3#定义球拍到圆心最近点与圆心的距离self.distance = math.sqrt+math.pow)#球在球拍上左、上中、上右3种情况的碰撞检测if self.distance < self.radius and self.collision_sign_y == 1 and :if self.collision_sign_x == 1 and self.move_x > 0:self.move_x = - self.move_xself.move_y = - self.move_yif self.collision_sign_x == 1 and self.move_x < 0:self.move_y = - self.move_yif self.collision_sign_x == 2 and self.move_x < 0:self.move_x = - self.move_xself.move_y = - self.move_yif self.collision_sign_x == 2 and self.move_x > 0:self.move_y = - self.move_yif self.distance < self.radius and self.collision_sign_y == 1 and self.collision_sign_x == 3:self.move_y = - self.move_y#球在球拍左、右两侧中间的碰撞检测if self.distance < self.radius and self.collision_sign_y == 3:self.move_x = - self.move_x#球与砖块的碰撞检测def ball_brick:#定义碰撞标识self.collision_sign_bx = 0self.collision_sign_by = 0if self.ball_x < self.brick_x:self.closestpoint_bx = self.brick_xself.collision_sign_bx = 1elif self.ball_x > self.brick_x+self.brick_length:self.closestpoint_bx = self.brick_x+self.brick_lengthself.collision_sign_bx = 2else:self.closestpoint_bx = self.ball_xself.collision_sign_bx = 3if self.ball_y < self.brick_y:self.closestpoint_by = self.brick_yself.collision_sign_by = 1elif self.ball_y > self.brick_y+self.brick_wide:self.closestpoint_by = self.brick_y+self.brick_wideself.collision_sign_by = 2else:self.closestpoint_by = self.ball_yself.collision_sign_by = 3#定义砖块到圆心最近点与圆心的距离self.distanceb = math.sqrt+math.pow)#球在砖块上左、上中、上右3种情况的碰撞检测if self.distanceb < self.radius and self.collision_sign_by == 1 and :if self.collision_sign_bx == 1 and self.move_x > 0:self.move_x = - self.move_xself.move_y = - self.move_yif self.collision_sign_bx == 1 and self.move_x < 0:self.move_y = - self.move_yif self.collision_sign_bx == 2 and self.move_x < 0:self.move_x = - self.move_xself.move_y = - self.move_yif self.collision_sign_bx == 2 and self.move_x > 0:self.move_y = - self.move_yif self.distanceb < self.radius and self.collision_sign_by == 1 and self.collision_sign_bx == 3:self.move_y = - self.move_y#球在砖块下左、下中、下右3种情况的碰撞检测if self.distanceb < self.radius and self.collision_sign_by == 2 and :if self.collision_sign_bx == 1 and self.move_x > 0:self.move_x = - self.move_xself.move_y = - self.move_yif self.collision_sign_bx == 1 and self.move_x < 0:self.move_y = - self.move_yif self.collision_sign_bx == 2 and self.move_x < 0:self.move_x = - self.move_xself.move_y = - self.move_yif self.collision_sign_bx == 2 and self.move_x > 0:self.move_y = - self.move_yif self.distanceb < self.radius and self.collision_sign_by == 2 and self.collision_sign_bx == 3:self.move_y = - self.move_y#球在砖块左、右两侧中间的碰撞检测if self.distanceb < self.radius and self.collision_sign_by == 3:self.move_x = - self.move_xclass Main:"""创建主程序类"""def __init__:super.__init__super.__init__super.__init__super.__init__super.__init__super.__init__super.__init__super.__init__#定义游戏开始标识start_sign = 0while True:self.backgroudself.rectmoveself.countscoreif self.over_sign == 1 or self.win_sign == 1:break#获取游戏窗口状态for event in pygame.event.get:if event.type == pygame.QUIT:sys.exitif event.type == MOUSEBUTTONDOWN:pressed_array = pygame.mouse.get_pressedif pressed_array[0]:start_sign = 1if start_sign == 0:self.ballreadyelse:self.ballmoveself.brickarrange#更新游戏窗口pygame.display.update#控制游戏窗口刷新频率time.sleepif __name__ == "__main__":pygame.initpygame.font.initcatchball = Main三、知识点1.多父类的继承

Python的继承方式分为深度优先和广度优先,Python2分经典类的深度优先搜索继承方式、 新式类的广度优先搜索继承方式2种,Python3经典类与新式类的继承方式与python2的新式类继承方式一致,都为广度优先的继承方式。

经典类的深度优先搜索继承方式:如图所示class Bclass Cclass D

若D类有构造函数,则重写所有父类的继承若D类没有构造函数,B类有构造函数,则D类会继承B类的构造函数若D类没有构造函数,B类也没有构造函数,则D类会继承 A类的构造函数,而不是C类的构造函数若D类没有构造函数,B类也没有构造函数,A类也没有构造函数,则D类才会继承C类的构造函数

新式类的广度优先搜索继承方式:如图所示class Bclass Cclass D

若D类有构造函数,则重写所有父类的继承若D类没有构造函数,B类有构造函数,则D类会继承B类的构造函数若D类没有构造函数,B类也没有构造函数,则D类会继承 C类的构造函数,而不是A类的构造函数若D类没有构造函数,B类也没有构造函数,C类也没有构造函数,则D类才会继承A类的构造函数

通过上面的分析,大家应该清楚了Python中类的继承顺序,那么问题来了,如果我不想重写父类的构造函数,要子类和父类的构造函数都生效怎么办?解决办法需要用到super关键字,对直接父类对象的引用,可以通过super来访问父类中被子类覆盖的方法或属性。

class A:def __init__class B:def __init__super.__init__class C:def __init__super.__init__class D:def __init__super.__init__super.__init__2.碰撞检测的数学模型

其实,编程问题到最后就是数学问题,这个游戏涉及到2D圆形与矩形的碰撞检测问题:

碰撞检测原理:通过找出矩形上离圆心最近的点,然后通过判断该点与圆心的距离是否小于圆的半径,若小于则为碰撞。

那如何找出矩形上离圆心最近的点呢?下面我们从 x 轴、y 轴两个方向分别进行寻找。为了方便描述,我们先约定以下变量:

矩形上离圆心最近的点为变量:closestpoint = [x, y]矩形 rect = [x, y, l, w] 左上角与长宽 length,wide圆形 circle = [x, y, r] 圆心与半径

首先是 x 轴:如果圆心在矩形的左侧,那么 closestpoint_x = rect_x。如果圆心在矩形的右侧,那么 closestpoint_x = rect_x + rect_l。如果圆心在矩形的正上下方,那么 closestpoint_x = circle_x。

同理,对于 y 轴:如果圆心在矩形的上方,那么 closestpoint_y = rect_y。如果圆心在矩形的下方,那么 closestpoint_y = rect_y + rect_w。圆形圆心在矩形的正左右两侧,那么 closestpoint_y = circle_y。

因此,通过上述方法即可找出矩形上离圆心最近的点了,然后通过“两点之间的距离公式”得出“最近点”与“圆心”的距离,最后将其与圆的半径相比,即可判断是否发生碰撞。distance=math.sqrt+math.pow)

if distance < circle.r :return True – 发生碰撞else :return False – 未发生碰撞

相关阅读
热门精选
孩子 皮肤