20192221 2019-2020-2 《Python程序设计》实验四报告
课程:《Python程序设计》
班级:1922班
姓名:叶蕊馨
学号:20192221
实验教师:王志强老师
实验日期:2020年6月9日
必修/选修: 公选课
实验内容选择
Python综合应用:爬虫、数据处理、可视化、机器学习、神经网络、游戏、网络安全等。
1.实验内容
python利用pygame进行简单的游戏开发(打砖块)
2. 实验过程及结果
1.功能划分
1)显现一个屏幕
2)画一个小球在屏幕上移动
3)碰到边缘要能够反弹
4)挡板绘制和移动
5)砖块的绘制
6)游戏流程和控制逻辑
完整代码
import pygame,random
import sys
import time
from pygame.locals import *
#界面长度宽度定义
width = 640
hight = 480
#界面颜色定义
backcolor = (230,230,230)
#游戏状态、属性常量定义
state_init = 0
stata_level = 1
state_run = 2
state_gameover = 3
state_shutdown = 4
state_exit = 5
fps = 25
#游戏状态
game_state = state_init
blocks = []
life_left = 1
game_over = False
blocks_hit = 0
#挡板和位置颜色
paddle = {'rect':pygame.Rect(0,0,32,8),
'color':(128,64,64)}
#挡板的运动控制
paddle_move_left = False
paddle_move_right = False
#小球的位置和速度
ball_x = 0
ball_y = 0
ball_dx = 0
ball_dy = 0
#界面初始化
pygame.init()
mainClock = pygame.time.Clock()
surface = pygame.display.set_mode((width, hight), 0, 32)
pygame.display.set_caption('打砖块')
#初始化砖块数组
def InitBlocks():
blocks = [[1] * 8] * 6
return blocks
#检测小球与挡板是否碰撞
def ProcessBall(blocks, ball_x, ball_y, paddle):
if (ball_y > hight//2):
if (ball_x+4 >= paddle['rect'].left and \
ball_x-4 <= paddle['rect'].left+32 and \
ball_y+4 >= paddle['rect'].top and \
ball_y-4 <= paddle['rect'].top+8):
return None
#显示文字
def DrawText(text, font, surface, x, y):
text_obj = font.render(text, 1, (255, 255, 255))
text_rect = text_obj.get_rect()
text_rect.topleft = (x, y)
surface.blit(text_obj, text_rect)
#退出游戏
def Terminate():
pygame.quit()
sys.exit()
#等待用户输入
def WaitForPlayerToPressKey():
while True:
for event in pygame.event.get():
if event.type == QUIT:
Terminate()
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
Terminate()
return
game_start_font = pygame.font.SysFont(None, 48)
game_over_font = pygame.font.SysFont(None, 48)
text_font = pygame.font.SysFont(None, 20)
pygame.display.update()
WaitForPlayerToPressKey()
while True:
#界面设置
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
exit()
#事件监听
if event.key == K_LEFT:
paddle_move_left = True
if event.key == K_RIGHT:
paddle_move_right = True
if event.type == KEYUP:
if event.key == K_LEFT:
paddle_move_left = False
if event.key == K_RIGHT:
paddle_move_right = False
#游戏控制流程
if game_state == state_init:
# 初始化游戏
ball_x = random.randint(8, width - 8)
ball_y = (hight//2)
ball_dx = random.randint(-3, 4)
ball_dy = random.randint(5, 8)
paddle['rect'].left = (width/2 - 16)
paddle['rect'].top = (hight - 32)
paddle_move_left = False
paddle_move_right = False
#life_left = TOTAL_LIFE
game_over = False
blocks_hit = 0
level = 1
game_state = stata_level
#新的一关
elif game_state == stata_level:
blocks = InitBlocks()
game_state = state_run
elif game_state == state_run:
# 游戏运行
# 球的运动
ball_x += ball_dx;
ball_y += ball_dy;
if ball_x > (width - 4) or ball_x < 4:
ball_dx = -ball_dx
ball_x += ball_dx;
elif ball_y < 4:
ball_dy = -ball_dy
ball_y += ball_dy
elif ball_y > hight - 4:
if life_left == 0:
game_state = state_gameover
else:
life_left -= 1
# 初始化游戏
ball_x = paddle['rect'].left + 32 // 2
ball_y = (hight//2)
ball_dx = random.randint(-4, 5)
ball_dy = random.randint(6, 9)
#检测球与挡板是否碰撞
if ball_y > hight // 2:
if (ball_x + 4 >= paddle['rect'].left and \
ball_x - 4 <= paddle['rect'].left + 32 and \
ball_y + 4 >= paddle['rect'].top and \
ball_y - 4 <= paddle['rect'].top + 8):
ball_dy = - ball_dy
ball_y += ball_dy
if paddle_move_left:
ball_dx -= random.randint(0, 3)
elif paddle_move_right:
ball_dx += random.randint(0, 3)
else:
ball_dx += random.randint(-1, 2)
#检测球与砖块是否碰撞
cur_x = 8
cur_y = 8
for row in range(6):
cur_x = 8
for col in range(8):
if blocks[row][col] != 0:
if (ball_x + 4 >= cur_x and \
ball_x - 4 <= cur_x + 64 and \
ball_y + 4 >= cur_y and \
ball_y - 4 <= cur_y + 16):
blocks[row][col] = 0
blocks_hit += 1
ball_dy = -ball_dy
ball_dx += random.randint(-1, 2)
#score += 5 * (level + abs(ball_dx))
cur_x += 80
cur_y += 32
if blocks_hit == 6 * 8:
level += 1
blocks_hit = 0
game_state = stata_level
#挡板的运动
if paddle_move_left:
paddle['rect'].left -= 8
if paddle['rect'].left < 0:
paddle['rect'].left = 0
if paddle_move_right:
paddle['rect'].left += 8
if paddle['rect'].left > width - 32:
paddle['rect'].left = width - 32
#绘制背景
surface.fill(backcolor)
#绘制挡板
pygame.draw.rect(surface, paddle['color'], paddle['rect'])
#绘制小球
pygame.draw.circle(surface,(0,0,255), (ball_x, ball_y), 4, 0)
#绘制砖块
cur_x = 8
cur_y = 8
for row in range(6):
cur_x = 8
for col in range(8):
if blocks[row][col] != 0:
pygame.draw.rect(surface, (255,128,0),(cur_x, cur_y, 64, 16))
cur_x += 80
cur_y += 32
#文字信息绘制
message = ' Life: ' + str(life_left)
DrawText(message, text_font, surface, 8, (hight - 16))
elif game_state == state_gameover:
DrawText('GAME OVER', game_over_font, surface,
(width / 3), (hight / 3))
DrawText('Press any key to play again.', game_over_font, surface,
(width / 3) - 80, (hight / 3) + 150)
pygame.display.update()
pygame.mixer.music.stop()
WaitForPlayerToPressKey()
game_state = state_init
elif game_state == state_shutdown:
game_state = state_exit
pygame.display.update()
mainClock.tick(30)
2.运行
其他(感悟、思考等)
*1.课程总结
这次的python学习包含序列、文件操作、网络编程、GUI、模块、爬虫以及其它基础知识点,对我c语言的学习也有很大的帮助,通过python的学习,了解到一些编程语言的思想,尤其是因为这学期的特殊情况,这门课比c语言先开一周,于是先一步了解编程方面的知识,对我后续c语言的学习先行掀开一角,会更加轻松。王志强老师也尽心尽责,出现问题先自己尝试解决的方法也让我获益匪浅。(要善用搜索引擎)
- 2.课程感想体会
经过这一特殊学期的学习,掌握了不少python编程技巧,从最开始对编程一窍不通(因为初高中完全没有接触过这一类的学习),到现在的综合实践都能成功写出一个简单的小游戏
,都是这一学期积累的知识点,从hello world一步步的学习中,踩入计算机的大门;从不知道怎么解决问题,到善于自己网上查找答案。我相信这对于我后续的学习也会有很大的帮助。刚选课的时候学长学姐们都很推荐王志强老师的课,选上后也觉得不虚此行。而对于我这个计算机专业的来说,多学一门语言相信以后对我的帮助会很大。 - 3.意见和建议
因为是网上授课的缘故最开始老师采用的方式是自己找时间看视频,但后来直接上直播课对我来说可能效果会更好。
建议:
希望如果有多余时间可以课上拓宽一下,讲一些比较复杂一点的例子。