20204106 实验四 《Python程序设计》实验报告

课程:《Python程序设计》
班级: 2041
姓名: 刘芷铭
学号:20204106
实验教师:王志强
实验日期:2021年5月30日
必修/选修: 公选课

(一)实验内容

Python综合应用:爬虫、数据处理、可视化、机器学习、神经网络、游戏、网络安全等。

鉴于课程最后学习的网络爬虫技术,以及最近表情包资源稀缺,想到可用python实现网络爬虫对于图片的抓取。

(二)实验过程及结果

1.实验目的

(1)根据本学期学习的Python基础内容,进行综合实践

(2)编写贪吃蛇游戏的代码

(3)程序代码托管到码云。

2.实验思路

(1)翻阅教材与网络寻找案例;

(2)根据案例创作全新的贪吃蛇游戏;

(3)程序代码托管到码云。

3.实验过程

根据教材中提供的贪吃蛇游戏的案例,对游戏背景、游戏参数、角色颜色和操作方法进行设定

 

 详细代码见码云网站,

码云出现问题,现将代码发在报告中

'''
实验四:Python综合实践
学号:20204106
姓名:刘芷铭
'''
import pygame, sys, time, random
import zt

pygame.init()
font = pygame.font.SysFont("", 50)
font2 = pygame.font.SysFont("", 20)
screen = pygame.display.set_mode((600, 600))
t1 = font.render('gameover', True, (0, 0, 0))
t2 = font2.render(str(zt.fs), True, (0, 0, 0))
pygame.display.set_caption("我的作品")
isit = False
ti = 0.5
f = 0


# pop
class rect():
def __init__(self, x, y, color):
self.width = 20
self.height = 20
self.x = x
self.y = y
self.color = color

def draw(self):
pygame.draw.rect(screen, self.color, (self.x, self.y, self.width, self.height), 0)
pygame.draw.rect(screen, (0, 0, 0), (self.x, self.y, self.width, self.height), 1)


class snake():
global speed, isit, f

def __init__(self):
self.width = 20
self.height = 20
self.x = 300
self.y = 300

self.s = [rect(self.x, self.y, (255, 0, 0))]

def drawsnake(self):
for s in self.s:
s.draw()

def move(self):
self.x += speed[0]
self.y += speed[1]
self.s.insert(0, rect(self.x, self.y, (255, 0, 0)))
if not isit:
self.s.pop()

def p(self):
if self.s[0].x < 20 or self.s[0].x > 550:
zt.l = False
screen.blit(t1, (290, 290))
if self.s[0].y < 20 or self.s[0].y > 550:
zt.l = False
screen.blit(t1, (290, 290))


class food():
def __init__(self):
self.x = random.randint(2, 27) * 20
self.y = random.randint(2, 27) * 20
self.food = rect(self.x, self.y, (0, 0, 255))

def draw(self):
self.food.draw()


s = snake()
speed = [20, 0]
last = 0
d = food()


def iseat():
global d, isit
if (d.x == s.s[0].x) and (d.y == s.s[0].y):
d = food()
isit = True
zt.fs = zt.fs + 1
else:
isit = False


t = True

while zt.l:
now = time.time()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP and t:
speed = [0, -20]
t = False
if event.key == pygame.K_DOWN and t:
speed = [0, 20]
t = False
if event.key == pygame.K_LEFT and not t:
speed = [-20, 0]
t = True
if event.key == pygame.K_RIGHT and not t:
speed = [20, 0]
t = True
if now - last >= ti / (zt.fs + 0.5):
screen.fill((255, 255, 255))
pygame.draw.rect(screen, (0, 0, 0), (20, 20, 560, 550), 1)
t2 = font2.render(str(zt.fs), True, (0, 0, 0))
screen.blit(t2, (580, 580))
s.move()
s.drawsnake()
last = now
d.draw()
iseat()
s.p()
pygame.display.update()

受截图影响难以展示运行结果,

详情请见视频。

4.实验体会

通过本学期的学期,我深刻体会到了Python作为一门简单易懂的编程语言所具有的独特魅力,让我感受到了计算机科学的独特魅力,虽然受限于个人水平,不能完全理解老师在课堂中讲述的一些知识,但仍然让我对Python这门学科有了一定的了解,计算机科学不仅是科学,更是人类在不断地探索中形成的一门诞生于实践、植根于实践、生长于实践中的人类艺术,感谢志强老师一学期以来的倾情讲解,让我们受益匪浅,我会更加努力地学习科学文化知识,取长补短,将课堂中学到的知识灵活运用在实践中,为事业的发展献出力量,感谢王志强老师,希望有机会还能在课堂上聆听您的教诲!

posted @ 2021-07-01 00:03  刘芷铭LN  阅读(258)  评论(0编辑  收藏  举报