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术语;
  • 做了四次实验;

    体会

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

参考资料

...全文
586 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复
内容概要:本文围绕“阶梯碳下考虑P2G-CCS与供需灵活响应的IES优化调度”展开,基于Matlab平台构建综合能源系统(IES)在阶梯式碳交易机制下的优化调度模型。研究深度融合电制气(P2G)与碳捕集、利用与封存(CCS)技术,结合需求侧灵活响应机制,旨在提升系统的低碳运行能力与经济性。通过建立多能流耦合的优化模型,协调电力、天然气、热力等多种能源形式的协同调度,有效降低系统碳排放强度,并借助YALIMIP工具包调用求解器进行高效求解。文档提供了完整的代码实现、模型构建流程与结果分析方法,涵盖从问题建模到仿真实现的全过程,具备较强的可复现性与科研参考价值。; 适合人群:具备电力系统、能源系统或优化建模相关背景的研究生、高校教师及工程技术人员,尤其适合从事综合能源系统、碳减排策略、P2G与CCS技术集成研究的专业人员,需熟练掌握Matlab编程与基本的数学规划知识。; 使用场景及目标:①用于研究阶梯式碳交易政策下综合能源系统的低碳经济调度策略;②支撑P2G-CCS技术与需求响应机制在IES中的仿真集成与性能评估;③作为撰写高水平学术论文(如EI/SCI收录)的技术基础与复现资源,推动碳中和背景下能源系统优化方向的创新研究。; 阅读建议:建议结合百度网盘提供的完整代码与资料包,按照模块逐步调试程序,重点理解目标函数的设计逻辑、碳交易成本的建模方式、约束条件的数学表达及求解器的配置方法,同时关注多能耦合设备的建模细节,配合公众号“荔枝科研社”获取持续的技术支持与案例拓展。
内容概要:本文系统研究了基于卷积神经网络(CNN)与支持向量机(SVM)融合的CNN-SVM混合模型在数据分类预测中的应用,尤其聚焦于工业故障识别领域。通过Matlab平台实现,该方法首先利用CNN强大的多层次特征提取能力对原始输入数据进行深度特征学习,自动捕获关键局部模式与空间结构信息,随后将提取的高层特征作为输入传递至SVM分类器,借助SVM在高维空间中小样本条件下卓越的分类性能与泛化能力完成最终判别任务。文中详尽阐述了模型的整体架构设计、网络参数配置、训练优化流程及特征迁移机制,充分结合了深度学习在特征表达上的优势与传统机器学习在分类决策上的稳健性。实验部分通过实际故障数据集验证了该混合模型相较于单一CNN或SVM模型在分类准确率、鲁棒性和抗过拟合能力方面的显著提升,证明了其在复杂故障诊断任务中的有效性与先进性; 适合人群:具备一定机器学习与深度学习理论基础,熟悉Matlab编程环境,从事故障诊断、模式识别、智能制造、电力系统监控或工业数据分析等相关领域的研究生、科研人员及工程技术开发者; 使用场景及目标:① 应用于旋转机械、电力设备、航空航天等领域的多类别故障识别与状态监测;② 掌握深度特征提取与传统分类器融合的技术路径,提升小样本、高噪声环境下数据分类的精度与可靠性;③ 为撰写高水平学术论文、开展科研项目或工程实践提供可复现的算法框架与完整代码支持; 阅读建议:读者应深入理解CNN与SVM的协同工作机制,重点分析特征提取层与分类层之间的接口设计,建议动手运行并调试所提供的Matlab代码,尝试在不同数据集上进行迁移实验与参数调优,以全面掌握该混合模型的应用技巧与优化策略。

144

社区成员

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

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