144
社区成员




20221218 2022-2023-2 《Python程序设计》实验四报告
课程:《Python程序设计》
班级: 2212
姓名:党万方
学号:20221218
实验教师:王志强
实验日期:2022年5月11日
必修/选修: 公选课
Python综合应用:爬虫、数据处理、可视化、机器学习、神经网络、游戏、网络安全等;
本人选择的为编写贪吃蛇小游戏,不同的小球具有不同的分值,当分值累积时蛇速度增加,难度提高。
1.首先导入库
- import random
- import sys
- import time
- import pygame
- from pygame.locals import *
- from collections import deque
2.基础设置
- Screen_Height=480
- Screen_Width=600
- Size=20#小方格大小
- Line_Width=1
- #游戏区域的坐标范围,0是左边界,1是右边界
- Area_x=(0,Screen_Width//Size-1)
- Area_y=(2,Screen_Height//Size-1)
- #食物的分值+颜色
- Food_Style_List=[(10,(255,100,100)),(20,(100,255,100)),(30,(100,100,255))]
- #整体颜色设置
- Light=(100,100,100)
- Dark=(200,200,200)
- Black=(0,0,0)
- Red=(200,30,30)
- Back_Ground=(40,40,60)
3.文本输出设置
- def Print_Txt(screen,font,x,y,text,fcolor=(255,255,255)):
- Text=font.render(text,True,fcolor)
- screen.blit(Text,(x,y))
4.蛇的初始化
- def init_snake():
- snake=deque()
- snake.append((2,Area_y[0]))
- snake.append((1,Area_y[0]))
- snake.append((0,Area_y[0]))
- return snake
5.食物设置
- def Creat_Food(snake):
-
- food_x=random.randint(Area_x[0],Area_x[1])
- food_y=random.randint(Area_y[0],Area_y[1])
- while(food_x,food_y)in snake:#如果食物出现在蛇上,重来;
- food_x = random.randint(Area_x[0], Area_x[1])
- food_y = random.randint(Area_y[[0], Area_y[1]])
- return food_x,food_y
6.定义主函数
- def main():
- pygame.init()
- screen=pygame.display.set_mode((Screen_Width,Screen_Height)) #初始化一个准备显示的窗口或屏幕
- pygame.display.set_caption('贪吃蛇')
- font1=pygame.font.SysFont('SimHei',24)
- font2 = pygame.font.SysFont(None, 72)
- fwidth, fheight = font2.size('GAME OVER')
- b=True
- #蛇
- snake=init_snake()
- #食物
- food=Creat_Food(snake)
- food_style=Food_Style()
- #方向控制
- pos=(1,0) ###
- #启动游戏相关变量初始化
- game_over=True #结束标志 # 是否开始,当start = True,game_over = True 时,才显示 GAME OVER
- game_start=False #开始标志
- score=0 #得分
- orispeed=0.2 #蛇初始速度
- speed=orispeed #蛇速度
- last_move_time=None
- pause=False #暂停
- while True:
- for event in pygame.event.get():
- if event.type==QUIT:
- sys.exit()
- elif event.type==KEYDOWN:
- if event.key==K_RETURN:
- if game_over:
- game_start=True
- game_over=False
- b=True
- snake=init_snake()
- food=Creat_Food(snake)
- food_style=Food_Style()
- pos=(1,0)
- #得分
- score=0
- last_move_time=time.time()
- elif event.key==K_SPACE:
- if not game_over:
- pause=not pause
- #以下为防止蛇在向右移动时按向左键,导致GameOver
- elif event.key in (K_UP,K_w):
- if b and not pos[1]: ###
- pos=(0,-1)
- b=False
- elif event.key in (K_DOWN,K_s):
- if b and not pos[1]:
- pos = (0, 1)
- b = False
- elif event.key in (K_LEFT,K_a):
- if b and not pos[0]:
- pos = (-1, 0)
- b = False
- elif event.key in (K_RIGHT,K_d):
- if b and not pos[0]:
- pos = (1, 0)
- b = False
- #填充背景色
- screen.fill(Back_Ground)
- ###
- #画网格线、竖线
- for x in range(Size, Screen_Width, Size):
- pygame.draw.line(screen, Black, (x, Area_y[0] * Size), (x, Screen_Height), Line_Width)
- #画网格线、横线
- for y in range(Area_y[0] * Size, Screen_Height, Size):
- pygame.draw.line(screen, Black, (0, y), (Screen_Width, y), Line_Width)
- #蛇的爬行过程
- if not game_over:
- curTime=time.time()
- if curTime-last_move_time>speed: ###
- if not pause:
- b=True
- last_move_time=curTime
- next_s = (snake[0][0] + pos[0], snake[0][1] + pos[1])
- #如果吃到了食物
- if next_s==food:
- snake.appendleft(next_s)
- score+=food_style[0]
- speed = orispeed - 0.03 * (score // 100)
- food = Creat_Food(snake)
- food_style = Food_Style()
- else:
- #在区域内
- if Area_x[0]<=next_s[0]<=Area_x[1] and Area_y[0]<=next_s[1]<=Area_y[1] and next_s not in snake:
- snake.appendleft(next_s)
- snake.pop()
- else :
- game_over=True
- #画食物
- if not game_over:
- pygame.draw.rect(screen, food_style[1], (food[0] * Size, food[1] * Size, Size, Size), 0)
- #画蛇
- for s in snake:
- pygame.draw.rect(screen, Dark, (s[0] * Size + Line_Width, s[1] * Size + Line_Width,
- Size - Line_Width * 2, Size - Line_Width * 2), 0)
- Print_Txt(screen, font1, 30, 7, f'速度: {score // 100}')
- Print_Txt(screen, font1, 450, 7, f'得分: {score}')
- #画GameOver
- if game_over:
-
- if game_start:
- #print('GameOver')
- Print_Txt(screen, font2, (Screen_Width - fwidth) // 2, (Screen_Height - fheight) // 2, 'GAME OVER',Red)
- pygame.display.update()
源代码如下:
- import random
- import sys
- import time
- import pygame
- from pygame.locals import *
- from collections import deque
- #基础设置
- Screen_Height=480
- Screen_Width=600
- Size=20#小方格大小
- Line_Width=1
- #游戏区域的坐标范围,0是左边界,1是右边界
- Area_x=(0,Screen_Width//Size-1)
- Area_y=(2,Screen_Height//Size-1)
- #食物的分值+颜色
- Food_Style_List=[(10,(255,100,100)),(20,(100,255,100)),(30,(100,100,255))]
- #整体颜色设置
- Light=(100,100,100)
- Dark=(200,200,200)
- Black=(0,0,0)
- Red=(200,30,30)
- Back_Ground=(40,40,60)
- #文本输出格式设置
- def Print_Txt(screen,font,x,y,text,fcolor=(255,255,255)):
- Text=font.render(text,True,fcolor)
- screen.blit(Text,(x,y))
- #初始化蛇
- def init_snake():
- snake=deque()
- snake.append((2,Area_y[0]))
- snake.append((1,Area_y[0]))
- snake.append((0,Area_y[0]))
- return snake
- #食物设置
- #注意需要对食物出现在蛇身上的情况进行判断
- def Creat_Food(snake):
-
- food_x=random.randint(Area_x[0],Area_x[1])
- food_y=random.randint(Area_y[0],Area_y[1])
- while(food_x,food_y)in snake:#如果食物出现在蛇上,重来;
- food_x = random.randint(Area_x[0], Area_x[1])
- food_y = random.randint(Area_y[[0], Area_y[1]])
- return food_x,food_y
- #食物风格
- def Food_Style():
- return Food_Style_List[random.randint(0,2)] #返回随机的分值和颜色
- def main():
- pygame.init()
- screen=pygame.display.set_mode((Screen_Width,Screen_Height)) #初始化一个准备显示的窗口或屏幕
- pygame.display.set_caption('贪吃蛇')
- font1=pygame.font.SysFont('SimHei',24)
- font2 = pygame.font.SysFont(None, 72)
- fwidth, fheight = font2.size('GAME OVER')
- b=True
- #蛇
- snake=init_snake()
- #食物
- food=Creat_Food(snake)
- food_style=Food_Style()
- #方向控制
- pos=(1,0) ###
- #启动游戏相关变量初始化
- game_over=True #结束标志 # 是否开始,当start = True,game_over = True 时,才显示 GAME OVER
- game_start=False #开始标志
- score=0 #得分
- orispeed=0.2 #蛇初始速度
- speed=orispeed #蛇速度
- last_move_time=None
- pause=False #暂停
- while True:
- for event in pygame.event.get():
- if event.type==QUIT:
- sys.exit()
- elif event.type==KEYDOWN:
- if event.key==K_RETURN:
- if game_over:
- game_start=True
- game_over=False
- b=True
- snake=init_snake()
- food=Creat_Food(snake)
- food_style=Food_Style()
- pos=(1,0)
- #得分
- score=0
- last_move_time=time.time()
- elif event.key==K_SPACE:
- if not game_over:
- pause=not pause
- #以下为防止蛇在向右移动时按向左键,导致GameOver
- elif event.key in (K_UP,K_w):
- if b and not pos[1]: ###
- pos=(0,-1)
- b=False
- elif event.key in (K_DOWN,K_s):
- if b and not pos[1]:
- pos = (0, 1)
- b = False
- elif event.key in (K_LEFT,K_a):
- if b and not pos[0]:
- pos = (-1, 0)
- b = False
- elif event.key in (K_RIGHT,K_d):
- if b and not pos[0]:
- pos = (1, 0)
- b = False
- #填充背景色
- screen.fill(Back_Ground)
- ###
- #画网格线、竖线
- for x in range(Size, Screen_Width, Size):
- pygame.draw.line(screen, Black, (x, Area_y[0] * Size), (x, Screen_Height), Line_Width)
- #画网格线、横线
- for y in range(Area_y[0] * Size, Screen_Height, Size):
- pygame.draw.line(screen, Black, (0, y), (Screen_Width, y), Line_Width)
- #蛇的爬行过程
- if not game_over:
- curTime=time.time()
- if curTime-last_move_time>speed: ###
- if not pause:
- b=True
- last_move_time=curTime
- next_s = (snake[0][0] + pos[0], snake[0][1] + pos[1])
- #如果吃到了食物
- if next_s==food:
- snake.appendleft(next_s)
- score+=food_style[0]
- speed = orispeed - 0.03 * (score // 100)
- food = Creat_Food(snake)
- food_style = Food_Style()
- else:
- #在区域内
- if Area_x[0]<=next_s[0]<=Area_x[1] and Area_y[0]<=next_s[1]<=Area_y[1] and next_s not in snake:
- snake.appendleft(next_s)
- snake.pop()
- else :
- game_over=True
- #画食物
- if not game_over:
-
- pygame.draw.rect(screen, food_style[1], (food[0] * Size, food[1] * Size, Size, Size), 0)
- #画蛇
- for s in snake:
- pygame.draw.rect(screen, Dark, (s[0] * Size + Line_Width, s[1] * Size + Line_Width,
- Size - Line_Width * 2, Size - Line_Width * 2), 0)
- Print_Txt(screen, font1, 30, 7, f'速度: {score // 100}')
- Print_Txt(screen, font1, 450, 7, f'得分: {score}')
- #画GameOver
- if game_over:
-
- if game_start:
- #print('GameOver')
- Print_Txt(screen, font2, (Screen_Width - fwidth) // 2, (Screen_Height - fheight) // 2, 'GAME OVER',Red)
- pygame.display.update()
- if __name__=='__main__':
- main()
-
运行如下:
托管至gitee:
1.在外部安装几个版本的pygame失败,后通过查询可在pycharm内部直接安装合适的pygame。
2.对于如何运用pygame导入窗口和相关应用不熟悉,通过查询资料解决。
3.一些设置调用的函数需要通过导入库,通过查询资料解决。
第一次用python写这种大程序,说实话好多地方都不知道怎么处理,让我自己写恐怕很难完成,许多基础设置也是去网上查阅资料才找到合适的处理方法。资料的有些函数从来没有见过,看了好多博客才大致了解其运行的原理和作用。总得来说,这次实验让我进步了很多,学习了pygame以及许多从来没见过的函数。
在python程序设计选修课上,我们从初识python了解到python语言的简洁性(通过与C语言比对,如不需要定义类型是不需要int、char等),到学习各种基础语句,再到元组、字典等序列、面向对象编程三要素、socket编程技术、网络爬虫技术......让我了解到了python的独特魅力。虽然本人对python这门语言仍然比较陌生,但我会在接下来的学习过程中更加深入地了解。
课程上,王志强老师会带着我会解读各种代码并现场编程调试,带领我们理解代码背后的运行原理。当遇到运行不成功的时候,王志强老师也会与我们一起讨论问题所在,让我们能更加深入地思考,从而起到锻炼我们编程思维的效果。
说实话,序列后跨到异常处理、socket技术跟爬虫、web的时候,我有点没跟上,可能是因为我的基础并不够,直到现在有很多地方我都还不能完全明白。但是由于课程课时安排以及教纲教导内容的原因,注定了有些地方讲的会比较快。我的一点点想法是,能不能减少知识大点的跨度,将每个知识大点的知识小点讲的更加详细。希望python程序设计课能越来越好!