110
社区成员




课程:《Python程序设计》
班级: 2324
姓名: 陈鹏宇
学号: 20232424
实验教师:王志强
实验日期:2024年5月15日
必修/选修: 公选课
设计两个个简单小游戏:
a).炫彩小球
b).勇闯地下一百层
一、炫彩小球
1.设计步骤:
a).导入库:
import pgzrun # 导入游戏库
import random # 导入随机库
b).设置窗口大小:
WIDTH = 800 # 设置窗口的宽度
HEIGHT = 600 # 设置窗口的高度
c).初始化小球列表:
balls = [] # 存储所有小球的信息,初始为空列表
d).绘制函数draw():
def draw(): # 绘制模块,每帧重复执行
screen.fill('white') # 白色背景
for ball in balls: # 绘制所有的圆
screen.draw.filled_circle((ball[0], ball[1]), ball[4], (ball[5], ball[6], ball[7]))
for x in range(1, ball[4], 3): # 用同心圆填充
screen.draw.filled_circle((ball[0], ball[1]), ball[4]-x, (random.randint(
ball[5], 255), random.randint(ball[6], 255), random.randint(ball[7], 255)))
e).更新函数 update():
def update(): # 更新模块,每帧重复操作
for ball in balls:
ball[0] = ball[0] + ball[2] # 利用x方向速度更新x坐标
ball[1] = ball[1] + ball[3] # 利用y方向速度更新y坐标
if ball[0] > WIDTH-ball[4] or ball[0] < ball[4]: # 当小球碰到左右边界时,x方向速度反转
ball[2] = -ball[2]
if ball[1] > HEIGHT-ball[4] or ball[1] < ball[4]: # 当小球碰到上下边界时,y方向速度反转
ball[3] = -ball[3]
f).鼠标移动事件处理函数 on_mouse_move():
def on_mouse_move(pos, rel, buttons): # 当鼠标移动时
if mouse.LEFT in buttons: # 当鼠标左键按下时
x = pos[0] # 鼠标的x坐标,设为小球的x坐标
y = pos[1] # 鼠标的y坐标,设为小球的y坐标
speed_x = random.randint(1, 5) # 小球x方向的速度
speed_y = random.randint(1, 5) # 小球y方向的速度
r = random.randint(5, 50) # 小球的半径
colorR = random.randint(10, 255) # 小球的三个颜色分量
colorG = random.randint(10, 255)
colorB = random.randint(10, 255)
# 存储小球所有信息的列表
ball = [x, y, speed_x, speed_y, r, colorR, colorG, colorB]
balls.append(ball) # 把第i号小球的信息添加到balls中
g).开始游戏 pgzrun.go():
pgzrun.go() # 开始执行游戏
2.代码运行结果:
3.一点修改:
将小球速度改为0,半径改为固定值10,则该程序可变为画笔工具(炫)
4.源代码:
a).小球:
import pgzrun # 导入游戏库
import random # 导入随机库
WIDTH = 800 # 设置窗口的宽度
HEIGHT = 600 # 设置窗口的高度
balls = [] # 存储所有小球的信息,初始为空列表
def draw(): # 绘制模块,每帧重复执行
screen.fill('white') # 白色背景
for ball in balls: # 绘制所有的圆
screen.draw.filled_circle((ball[0], ball[1]), ball[4], (ball[5], ball[6], ball[7]))
for x in range(1, ball[4], 3): # 用同心圆填充
screen.draw.filled_circle((ball[0], ball[1]), ball[4]-x, (random.randint(
ball[5], 255), random.randint(ball[6], 255), random.randint(ball[7], 255)))
def update(): # 更新模块,每帧重复操作
for ball in balls:
ball[0] = ball[0] + ball[2] # 利用x方向速度更新x坐标
ball[1] = ball[1] + ball[3] # 利用y方向速度更新y坐标
if ball[0] > WIDTH-ball[4] or ball[0] < ball[4]: # 当小球碰到左右边界时,x方向速度反转
ball[2] = -ball[2]
if ball[1] > HEIGHT-ball[4] or ball[1] < ball[4]: # 当小球碰到上下边界时,y方向速度反转
ball[3] = -ball[3]
def on_mouse_move(pos, rel, buttons): # 当鼠标移动时
if mouse.LEFT in buttons: # 当鼠标左键按下时
x = pos[0] # 鼠标的x坐标,设为小球的x坐标
y = pos[1] # 鼠标的y坐标,设为小球的y坐标
speed_x = random.randint(1, 5) # 小球x方向的速度
speed_y = random.randint(1, 5) # 小球y方向的速度
r = random.randint(5, 50) # 小球的半径
colorR = random.randint(10, 255) # 小球的三个颜色分量
colorG = random.randint(10, 255)
colorB = random.randint(10, 255)
# 存储小球所有信息的列表
ball = [x, y, speed_x, speed_y, r, colorR, colorG, colorB]
balls.append(ball) # 把第i号小球的信息添加到balls中
pgzrun.go() # 开始执行游戏
b).画笔:
import pgzrun # 导入游戏库
import random # 导入随机库
WIDTH = 800 # 设置窗口的宽度
HEIGHT = 600 # 设置窗口的高度
balls = [] # 存储所有小球的信息,初始为空列表
def draw(): # 绘制模块,每帧重复执行
screen.fill('white') # 白色背景
for ball in balls: # 绘制所有的圆
screen.draw.filled_circle((ball[0], ball[1]), ball[4], (ball[5], ball[6],
ball[7]))
for x in range(1,ball[4],3):
screen.draw.filled_circle((ball[0],ball[1]),ball[4]-x,(random.randint(ball[5],255),random.randint(ball[6],255),random.randint(ball[7],255)))
def update(): # 更新模块,每帧重复操作
for ball in balls:
ball[0] = ball[0] + ball[2] # 利用x方向速度更新x坐标
ball[1] = ball[1] + ball[3] # 利用y方向速度更新y坐标
# 当小球碰到左右边界时,x方向速度反向
if ball[0] > WIDTH-ball[4] or ball[0] < ball[4]:
ball[2] = -ball[2]
# 当小球碰到上下边界时,y方向速度反向
if ball[1] > HEIGHT-ball[4] or ball[1] < ball[4]:
ball[3] = -ball[3]
def on_mouse_move(pos, rel, buttons): # 当鼠标移动时
if mouse.LEFT in buttons: # 当按下鼠标左键时
x = pos[0] # 鼠标的x坐标,设为小球的x坐标
y = pos[1] # 鼠标的y坐标,设为小球的y坐标
speed_x = 0 # 小球x方向的速度
speed_y = 0 # 小球y方向的速度
r = 10 # 小球的半径
colorR = random.randint(10, 255) # 小球的3个颜色分量
colorG = random.randint(10, 255)
colorB = random.randint(10, 255)
# 存储小球所有信息的列表
ball = [x, y, speed_x, speed_y, r, colorR, colorG, colorB]
balls.append(ball) # 把该小球的信息添加到balls中
pgzrun.go() # 开始执行游戏
二、勇闯地下一百层
1.设计步骤:
a).导入库:
import pgzrun # 导入游戏库
import random # 导入随机库
b).常量:
WIDTH = 600 # 设置窗口的宽度
HEIGHT = 800 # 设置窗口的高度
playerSpeed = 5 # 玩家水平移动速度
brickSpeed = 2 # 砖块自动上移速度
isLoose = False # 游戏是否失败
score = 0 # 游戏得分
c).初始化游戏元素:
alien = Actor('alien') # 导入玩家图片
alien.x = WIDTH/2 # 设置玩家的x坐标
alien.y = HEIGHT/5 # 设置玩家的y坐标
lastAlienY = alien.y # 记录角色上一帧的y坐标
bricks = [] # 存储所有砖块的列表,开始为空
for i in range(5):
brick = Actor('brick') # 导入砖块图片
brick.pos = 100*(i+1), 150*(i+1) # 设置砖块的(x,y)坐标
bricks.append(brick) # 把当前砖块加入列表中
d).绘制函数draw():
def draw(): # 绘制模块,每帧重复执行
screen.clear() # 清空游戏画面
alien.draw() # 绘制玩家
for brick in bricks: # 绘制列表中每个砖块
brick.draw() # 绘制砖块
screen.draw.text("你已坚持了 "+str(score)+"层!", (400, 20), fontsize=25,
fontname='s', color='white')
if isLoose: # 如果游戏失败,输出相关信息
screen.draw.text("游戏失败!", (80, HEIGHT/2-100),
fontsize=100, fontname='s', color='red')
e).更新函数 update():
def update(): # 更新模块,每帧重复操作
global isLoose, playerSpeed, brickSpeed, score, lastAlienY
isPlayerOnGround = False # 开始假设角色没有站在砖块上
for brick in bricks: # 对列表中所有砖块遍历
# 玩家正好站在砖块上面,在方块左右之间,可以左右移动
if abs(alien.bottom-brick.top) < 5 \
and brick.left - alien.left < alien.width*2/3 \
and alien.right - brick.right < alien.width*2/3:
alien.image = 'alien' # 设为在砖块上站立的图片
isPlayerOnGround = True # 玩家在一块砖上
alien.bottom = brick.top # 玩家跟着砖块一直向上移动
if lastAlienY < alien.y:
score += 1 # 之前还不在砖上,表示现在刚开始落到砖上,分数+1
if keyboard.left: # 如果按下键盘左键
alien.x = alien.x - playerSpeed # 玩家左移
if keyboard.right: # 如果按下键盘右键
alien.x = alien.x + playerSpeed # 玩家右移
lastAlienY = alien.y # 对所有砖块遍历后,更新lastAlienY的值
if not isPlayerOnGround:
alien.image = 'alien_falling' # 设为在空中的玩家图片
alien.y += 5 # 玩家不在任何一块砖上,就下落
for brick in bricks: # 所有砖块缓慢上移
brick.y -= brickSpeed
if bricks[0].top < 10: # 最上面的一个砖块达到画面顶部时
del bricks[0] # 删除最上面的砖块
brick = Actor('brick') # 新增一个砖块
brick.x = random.randint(100, 500) # 水平位置随机
brick.y = HEIGHT # 从最下部出现
bricks.append(brick) # 将新砖块添加到列表中
if alien.top < 0 or alien.bottom > HEIGHT: # 角色超出上下范围,游戏失败
playerSpeed = 0 # 玩家水平移动速度设为0
brickSpeed = 0 # 砖块自动上移速度设为0
isLoose = True # 游戏失败
f).开始游戏 pgzrun.go():
pgzrun.go() # 开始执行游戏
2.代码运行结果:
3.源代码:
import pgzrun # 导入游戏库
import random # 导入随机库
WIDTH = 600 # 设置窗口的宽度
HEIGHT = 800 # 设置窗口的高度
playerSpeed = 5 # 玩家水平移动速度
brickSpeed = 2 # 砖块自动上移速度
isLoose = False # 游戏是否失败
score = 0 # 游戏得分
alien = Actor('alien') # 导入玩家图片
alien.x = WIDTH/2 # 设置玩家的x坐标
alien.y = HEIGHT/5 # 设置玩家的y坐标
lastAlienY = alien.y # 记录角色上一帧的y坐标
bricks = [] # 存储所有砖块的列表,开始为空
for i in range(5):
brick = Actor('brick') # 导入砖块图片
brick.pos = 100*(i+1), 150*(i+1) # 设置砖块的(x,y)坐标
bricks.append(brick) # 把当前砖块加入列表中
def draw(): # 绘制模块,每帧重复执行
screen.clear() # 清空游戏画面
alien.draw() # 绘制玩家
for brick in bricks: # 绘制列表中每个砖块
brick.draw() # 绘制砖块
screen.draw.text("你已坚持了 "+str(score)+"层!", (400, 20), fontsize=25,
fontname='s', color='white')
if isLoose: # 如果游戏失败,输出相关信息
screen.draw.text("游戏失败!", (80, HEIGHT/2-100),
fontsize=100, fontname='s', color='red')
def update(): # 更新模块,每帧重复操作
global isLoose, playerSpeed, brickSpeed, score, lastAlienY
isPlayerOnGround = False # 开始假设角色没有站在砖块上
for brick in bricks: # 对列表中所有砖块遍历
# 玩家正好站在砖块上面,在方块左右之间,可以左右移动
if abs(alien.bottom-brick.top) < 5 \
and brick.left - alien.left < alien.width*2/3 \
and alien.right - brick.right < alien.width*2/3:
alien.image = 'alien' # 设为在砖块上站立的图片
isPlayerOnGround = True # 玩家在一块砖上
alien.bottom = brick.top # 玩家跟着砖块一直向上移动
if lastAlienY < alien.y:
score += 1 # 之前还不在砖上,表示现在刚开始落到砖上,分数+1
if keyboard.left: # 如果按下键盘左键
alien.x = alien.x - playerSpeed # 玩家左移
if keyboard.right: # 如果按下键盘右键
alien.x = alien.x + playerSpeed # 玩家右移
lastAlienY = alien.y # 对所有砖块遍历后,更新lastAlienY的值
if not isPlayerOnGround:
alien.image = 'alien_falling' # 设为在空中的玩家图片
alien.y += 5 # 玩家不在任何一块砖上,就下落
for brick in bricks: # 所有砖块缓慢上移
brick.y -= brickSpeed
if bricks[0].top < 10: # 最上面的一个砖块达到画面顶部时
del bricks[0] # 删除最上面的砖块
brick = Actor('brick') # 新增一个砖块
brick.x = random.randint(100, 500) # 水平位置随机
brick.y = HEIGHT # 从最下部出现
bricks.append(brick) # 将新砖块添加到列表中
if alien.top < 0 or alien.bottom > HEIGHT: # 角色超出上下范围,游戏失败
playerSpeed = 0 # 玩家水平移动速度设为0
brickSpeed = 0 # 砖块自动上移速度设为0
isLoose = True # 游戏失败
pgzrun.go() # 开始执行游戏
首先,Python的强大和灵活性让我印象深刻。在学习的过程中,我逐渐了解到Python的简洁明了和易于上手的特点,其语法清晰,代码易于阅读和维护,这使得我在编写程序时能够更加高效和准确。其次,Python的学习过程也让我认识到了编程思维的重要性。编程不仅仅是一种技能,更是一种解决问题的思维方式。通过Python的学习,我学会了如何将问题分解成更小的部分,并使用逻辑和算法来解决它们。这种思维方式不仅对我的学习有帮助,也对我的日常生活产生了积极的影响。最后,王老师的课堂活泼有趣,不同于平日的其他科目的课堂。课上王老师深入浅出的讲解带我领略到了python真正的魅力,如自己编写计算器程序、使用socket与舍友通过程序进行对话等,不同于高中时期简单的输入输出语句那样机械,此外,英语打卡也让人印象深刻,甚至在英语听写课堂中救我一命(doge)。总之,人生苦短,我用Python!