20181209 2019-2020-2 《Python程序设计》实验4报告

 # 20181209 2019-2020-2 《Python程序设计》实验4报告

课程:《Python程序设计》
班级: 1812
姓名: 沙桐
学号:20181209
实验教师:王志强
实验日期:2020年6月8日
必修/选修: 公选课

## 1.实验内容
python综合实践 利用可视化、游戏等模块制作小游戏--打砖块
## 2. 实验过程及结果

 

 

实验分析

1.创建游戏窗口

2.创建球类

3.创建球拍

4.分数设计

5.游戏的开始 结束 胜利 失败

 

 

实验设计思路

操作在屏幕底端的球拍 通过反弹小球的方式

点击鼠标后开始小球的弹射 当小球碰到砖块 墙壁 球拍 会以相同的速度反弹 小球击打砖块 砖块消失,只需要打掉所有的砖块即可获的胜利。

小球掉落横板下方即失败。

实验过程

首先学习了以下 pygame 模块的使用

下载并安装该板块

 

从pygame模块导入常用的函数和常量:

  import pygame

  from pygame.locals import

       import sys, random, time, math

创建窗口类:

  

class GameWindow(object):


def __init__(self, *args, **kw):
self.window_length = 600
self.window_wide = 500
# 绘制游戏窗口,设置窗口尺寸
self.game_window = pygame.display.set_mode((self.window_length, self.window_wide))
# 设置游戏窗口标题
pygame.display.set_caption("CatchBallGame")
# 定义游戏窗口背景颜色参数
self.window_color = (135, 206, 250)

def backgroud(self):
# 绘制游戏窗口背景颜色
self.game_window.fill(self.window_color)

 创建球类:

  

class Ball(object):


def __init__(self, *args, **kw):
# 设置球的半径、颜色、移动速度参数
self.ball_color = (255, 215, 0)
self.move_x = 1
self.move_y = 1
self.radius = 10

def ballready(self):
# 设置球的初始位置、
self.ball_x = self.mouse_x
self.ball_y = self.window_wide - self.rect_wide - self.radius
# 绘制球,设置反弹触发条件
pygame.draw.circle(self.game_window, self.ball_color, (self.ball_x, self.ball_y), self.radius)

def ballmove(self):
# 绘制球,设置反弹触发条件
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
# 调用碰撞检测函数
self.ball_window()
self.ball_rect()
# 每接5次球球速增加一倍
if self.distance < self.radius:
self.frequency += 1
if self.frequency == 5:
self.frequency = 0
self.move_x += self.move_x
self.move_y += self.move_y
self.point += self.point
# 设置游戏失败条件
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 = 1

  

 

创建砖类:

  

class Brick(object):
def __init__(self, *args, **kw):
# 设置砖块颜色参数
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 in range(5):
for j in range(6):
self.brick_x = j * (self.brick_length + 24)
self.brick_y = i * (self.brick_wide + 20) + 40
if self.brick_list[i][j] == 1:
# 绘制砖块
pygame.draw.rect(self.game_window, self.brick_color,
(self.brick_x, self.brick_y, self.brick_length, self.brick_wide))
# 调用碰撞检测函数
self.ball_brick()
if self.distanceb < self.radius:
self.brick_list[i][j] = 0
self.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("You Win", False, (0, 0, 0))
self.game_window.blit(self.win, (100, 130))
self.win_sign = 1

 

 

 

 创建分数类:

  

class Score(object):
'''创建分数类'''

def __init__(self, *args, **kw):
# 设置初始分数
self.score = 0
# 设置分数字体
self.score_font = pygame.font.SysFont('arial', 20)
# 设置初始加分点数
self.point = 1
# 设置初始接球次数
self.frequency = 0

def countscore(self):
# 绘制玩家分数
my_score = self.score_font.render(str(self.score), False, (255, 255, 255))
self.game_window.blit(my_score, (555, 15))

 

 

 

 

创建游戏结束类:

  

class GameOver(object):


def __init__(self, *args, **kw):
# 设置Game Over字体
self.over_font = pygame.font.SysFont('arial', 80)
# 定义GameOver标识
self.over_sign = 0

 

 

 

 

 

 创建游戏胜利类:

  

class Win(object):


def __init__(self, *args, **kw):
# 设置You Win字体
self.win_font = pygame.font.SysFont('arial', 80)
# 定义Win标识
self.win_sign = 0

 

 

 创建碰撞检测类:

  

class Collision(object):


# 球与窗口边框的碰撞检测
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

# 球与球拍的碰撞检测
def ball_rect(self):
# 定义碰撞标识
self.collision_sign_x = 0
self.collision_sign_y = 0

if 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 = 2
else:
self.closestpoint_x = self.ball_x
self.collision_sign_x = 3

if 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 = 2
else:
self.closestpoint_y = self.ball_y
self.collision_sign_y = 3
# 定义球拍到圆心最近点与圆心的距离
self.distance = math.sqrt(
math.pow(self.closestpoint_x - self.ball_x, 2) + math.pow(self.closestpoint_y - self.ball_y, 2))
# 球在球拍上左、上中、上右3种情况的碰撞检测
if self.distance < self.radius and self.collision_sign_y == 1 and (
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
# 球在球拍左、右两侧中间的碰撞检测
if self.distance < self.radius and self.collision_sign_y == 3:
self.move_x = - self.move_x

# 球与砖块的碰撞检测
def ball_brick(self):
# 定义碰撞标识
self.collision_sign_bx = 0
self.collision_sign_by = 0

if 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 = 2
else:
self.closestpoint_bx = self.ball_x
self.collision_sign_bx = 3

if 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 = 2
else:
self.closestpoint_by = self.ball_y
self.collision_sign_by = 3
# 定义砖块到圆心最近点与圆心的距离
self.distanceb = math.sqrt(
math.pow(self.closestpoint_bx - self.ball_x, 2) + math.pow(self.closestpoint_by - self.ball_y, 2))
# 球在砖块上左、上中、上右3种情况的碰撞检测
if self.distanceb < self.radius and self.collision_sign_by == 1 and (
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
# 球在砖块下左、下中、下右3种情况的碰撞检测
if self.distanceb < self.radius and self.collision_sign_by == 2 and (
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
# 球在砖块左、右两侧中间的碰撞检测
if self.distanceb < self.radius and self.collision_sign_by == 3:
self.move_x = - self.move_x

 

 

 创建主函数类:

  

class Main(GameWindow, Rect, Ball, Brick, Collision, Score, Win, GameOver):


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)
# 定义游戏开始标识
start_sign = 0

while True:
self.backgroud()
self.rectmove()
self.countscore()

if self.over_sign == 1 or self.win_sign == 1:
break
# 获取游戏窗口状态
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 = 1
if start_sign == 0:
self.ballready()
else:
self.ballmove()

self.brickarrange()

# 更新游戏窗口
pygame.display.update()
# 控制游戏窗口刷新频率
time.sleep(0.010)


if __name__ == '__main__':
pygame.init()
pygame.font.init()
catchball = Main()

 

实验结果

  

 

 

 

 

 



## 3. 实验过程中遇到的问题和解决过程
- 问题1:对这个打砖块游戏的印象 只停留在儿时 没有什么系统的想法 不知从何处开始下手
- 问题1解决方案:在网上 查阅很多资料 看了很多视频有了才有了一个具体的思路
- 问题2:碰撞检测
- 问题2解决方案:在网上查询碰撞检测函数用法 分析所能出现的可能 把能想到的碰撞可能全部用if语句表出现
- ...


## 其他(感悟、思考等)
码云链接:https://gitee.com/tung_tree1/python_text-2020/blob/实验4/游戏设计.py

这个学习了python 觉得真的挺实用 比c语言好用很多 但是自己的水平还是很低 很多东西都需要在网上搜索 然后现学现用 我也会多多学习这门语言,因为python对于未来来说 他的使用价值很大 我觉得是像office一样 必备的技能

做了这个小游戏发现 自己的这个水平 还是很菜 也还有很大的提升空间 今后也要多多学习

还有我对这门课程有一点小建议,就像最开始的时候需要看视频去学习,感觉任务量很大,学多少 学不学 都是靠自己 但之后都是直播上课这个我很喜欢 可能是我自己的学习动力没有那么强,自己也还需要加强 我个人更喜欢那种直播上课

 

posted @ 2020-06-08 15:40  STong66  阅读(330)  评论(0编辑  收藏  举报