20221328 实验四《python程序设计》实验报告

liudaotongming 2023-05-25 22:50:02

20221328 2022-2023-2 《Python程序设计》实验一报告

课程:《Python程序设计》
班级: 2213
姓名: 马申明
学号:1328
实验教师:王志强
实验日期:2023年5月4日
必修/选修: 公选课

1.实验内容

Python综合应用:爬虫、数据处理、可视化、机器学习、神经网络、游戏、网络安全等。
课代表和各小组负责人收集作业(源代码、视频、综合实践报告)

例如:编写从社交网络爬取数据,实现可视化舆情监控或者情感分析。

例如:利用公开数据集,开展图像分类、恶意软件检测等

例如:利用Python库,基于OCR技术实现自动化提取图片中数据,并填入excel中。

例如:爬取天气数据,实现自动化微信提醒

例如:利用爬虫,实现自动化下载网站视频、文件等。

例如:编写小游戏:坦克大战、贪吃蛇、扫雷等等

注:在Windows/Linux系统上使用VIM、PDB、IDLE、Pycharm等工具编程实现。

2. 实验过程及结果

Flappy Bird代码:

import pygame
import sys
import random
class Bird(object):
    def __init__(self):
        self.birdRect = pygame.Rect(65,50,50,50)
        self.birdStatus = [pygame.image.load("bird2.png"),
                           pygame.image.load("bird0.png"),
                           pygame.image.load("birddead1.png")]
        self.status = 0
        self.birdX = 120
        self.birdY = 300
        self.jump = False
        self.jumpSpeed = 10
        self.gravity = 5
        self.dead = False
        self.strong = False
    def birdUpdate(self):
        if self.jump:
            self.jumpSpeed -= 1
            self.birdY -= self.jumpSpeed
        else:
            self.gravity += 0.2
            self.birdY += self.gravity
        self.birdRect[1] = self.birdY
class Pipeline(object):
    def __init__(self):
        self.wallx = 1024
        self.wallx2 = 1424
        self.wallx3 = 1824
        self.wally = 0
        self.wally2 = -80
        self.wally3 = -50
        self.pineUp = pygame.image.load("管道上.jpg")
        self.pineDown = pygame.image.load("管道下.jpg")
    def updatePipeline(self):
        global score
        if score <= 30:
            self.wallx -= 5
            self.wallx2 -= 5
            self.wallx3 -= 5
        elif score <= 60:
            self.wallx -= 7
            self.wallx2 -= 7
            self.wallx3 -= 7
        else:
            self.wallx -= 9
            self.wallx2 -= 9
            self.wallx3 -= 9
        if self.wallx < -133:
            score += 1
            self.wallx = 1024
            self.wally = random.randrange(-100,0)
        elif self.wallx2 < -133:
            score += 1
            self.wallx2 = 1024
            self.wally2 = random.randrange(-100,0)
        elif self.wallx3 < -133:
            score += 1
            self.wallx3 = 1024
            self.wally3 = random.randrange(-100,0)
#class shield(object):
#   def __init__(self):
#      self.Sx = random.randrange(1168, 1381)
 #     self.Sy = random.randrange(0, 1000)
  #    self.img = pygame.image.load("shield_副本.png")
   #def Supdate (self):
   #   if score <= 30:
    #      self.Sx -= 5
     # elif score <= 60:
      #    self.Sx -= 7
      #else:
       #   self.Sx -= 9
      #if self.Sx < -30:
       #   self.Sx = random.randrange(1168, 1381)
        #  self.Sy = random.randrange(0, 599)
def createMap():
    screen.fill((255,255,255))
    screen.blit(background,(0,0))
    screen.blit(Pipeline.pineUp,(Pipeline.wallx,Pipeline.wally))
    screen.blit(Pipeline.pineUp,(Pipeline.wallx2,Pipeline.wally2))
    screen.blit(Pipeline.pineUp, (Pipeline.wallx3, Pipeline.wally3))
    if score < 90:
        d = 550
    else:
        d = 500
    screen.blit(Pipeline.pineDown,(Pipeline.wallx,Pipeline.wally + d))
    screen.blit(Pipeline.pineDown, (Pipeline.wallx2, Pipeline.wally2 + d))
    screen.blit(Pipeline.pineDown, (Pipeline.wallx3, Pipeline.wally3 + d))
    Pipeline.updatePipeline()
    #screen.blit(shield.img,(shield.Sx,shield.Sy))
    #shield.Supdate()
    if Bird.dead:
        Bird.status = 2
    elif Bird.jump:
        Bird.status = 1
    screen.blit(Bird.birdStatus[Bird.status],(Bird.birdX,Bird.birdY))
    Bird.birdUpdate()
    screen.blit(font.render('Score:' + str(score), -1, (253, 177, 6)), (100, 50))
    pygame.display.update()
def checkDead(score):
    upRect = pygame.Rect(Pipeline.wallx,Pipeline.wally,
                         Pipeline.pineUp.get_width() - 10,
                         Pipeline.pineUp.get_height())
    downRect = pygame.Rect(Pipeline.wallx,Pipeline.wally + 550,
                           Pipeline.pineDown.get_width() - 10,
                           Pipeline.pineDown.get_height())
    if upRect.colliderect(Bird.birdRect) or downRect.colliderect(Bird.birdRect):
        Bird.dead = True
        if Bird.strong == True:
            score = score + 1
            Bird.dead = False
            Bird.strong = False
            return False
    upRect = pygame.Rect(Pipeline.wallx2, Pipeline.wally2,
                         Pipeline.pineUp.get_width() - 10,
                         Pipeline.pineUp.get_height())
    downRect = pygame.Rect(Pipeline.wallx2, Pipeline.wally2 + 550,
                           Pipeline.pineDown.get_width() - 10,
                           Pipeline.pineDown.get_height())
    if upRect.colliderect(Bird.birdRect) or downRect.colliderect(Bird.birdRect):
        Bird.dead = True
        if Bird.strong == True:
            score = score + 1
            Bird.dead = False
            Bird.strong = False
            return False
    upRect = pygame.Rect(Pipeline.wallx3, Pipeline.wally3,
                         Pipeline.pineUp.get_width() - 10,
                         Pipeline.pineUp.get_height())
    downRect = pygame.Rect(Pipeline.wallx3, Pipeline.wally3 + 550,
                           Pipeline.pineDown.get_width() - 10,
                           Pipeline.pineDown.get_height())
    if upRect.colliderect(Bird.birdRect) or downRect.colliderect(Bird.birdRect):
        Bird.dead = True
        if Bird.strong == True:
            score = score + 1
            Bird.dead = False
            Bird.strong = False
            return False
    if not 0 < Bird.birdRect[1] < height:
        Bird.dead = True
        if Bird.strong == True:
            score = score + 1
            Bird.dead = False
            Bird.strong = False
            return False
        return True
    else:
        return False
def strong(score):
    Rect = pygame.Rect(shield.Sx, shield.Sy,
                       shield.img.get_width(),
                       shield.img.get_height())
    if Rect.colliderect(Bird.birdRect):
        score = score + 1
        Bird.strong = True
def getResutl():
    final_text1 = "Game Over"
    final_text2 = "Your final score is:  " + str(score)
    ft1_font = pygame.font.SysFont("Arial", 70)                                      # 设置第一行文字字体
    ft1_surf = font.render(final_text1, 1, (242, 3, 36))                             # 设置第一行文字颜色
    ft2_font = pygame.font.SysFont("Arial", 50)                                      # 设置第二行文字字体
    ft2_surf = font.render(final_text2, 1, (253, 177, 6))                            # 设置第二行文字颜色
    screen.blit(ft1_surf, [screen.get_width() / 2 - ft1_surf.get_width() / 2, 100])  # 设置第一行文字显示位置
    screen.blit(ft2_surf, [screen.get_width() / 2 - ft2_surf.get_width() / 2, 200])  # 设置第二行文字显示位置
    pygame.display.flip()
if __name__ == '__main__':
    pygame.init()
    pygame.font.init()
    font = pygame.font.SysFont(None,50)
    size = width , height = 1024,650
    screen = pygame.display.set_mode(size)
    clock = pygame.time.Clock()
    Pipeline = Pipeline()
    Bird = Bird()
    #shield = shield()
    score = 0
    while True:
        clock.tick(60)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            if (event.type == pygame.KEYDOWN or event.type == pygame.MOUSEBUTTONDOWN)and not Bird.dead:
                Bird.jump = True
                Bird.gravity = 5
                Bird.jumpSpeed = 10
        background = pygame.image.load("blue sky.jpg")
        #pygame.mixer.music.load(r"Flappy Bird.MP3")
        #pygame.mixer.music.play(loops=-1, start=0.0)
        if checkDead(score):
            pygame.mixer.music.fadeout(3000)
            getResutl()
        else:
            createMap()
    pygame.quit()
    sys.exit()

实验过程:

    1. 抄书;
    1. 改代码,改变了柱子出现的高度,增加了随机数;
      增加了柱子数量;
      增加了分数;

遇到的问题及感想

  • 问题一:上下柱子图片重叠;
  • 解决方案:改坐标参数成功解决;
  • 问题二:无法添加音乐;
  • 解决方案:将所需模块复制到win64,未能解决;
  • 问题三:小道具功能失效;

感想

想法未能成功转变成实际,难受;

总结

  • 学会了基本的python语法;
  • 了解了一些python术语;
  • 做了四次实验;

    体会

  • 课程紧了点
  • 实操少了点
  • 复习资料无

参考资料

...全文
282 回复 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

144

社区成员

发帖
与我相关
我的任务
社区描述
开展Python教学和技术交流
python 高校 北京·丰台区
社区管理员
  • blackwall0321
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧