20232411 董琰祥 《Python程序设计》实验四报告

20232411董琰祥 2024-05-29 23:56:00

20232411 董琰祥 《Python程序设计》实验四报告

课程:《Python程序设计》
班级: 2324
姓名: 董琰祥
学号:20232411
实验教师:王志强
实验日期:2022年4月17日
必修/选修: 公选课

1. 实验内容

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

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

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

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

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

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

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

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

2.实验结果呈现

完整代码:

飞机大战.py 11.37K

运行视频:

3.实验过程

第一步:下载并导入pygame等函数库,设置屏幕大小并初始化

飞机大战这个小游戏的核心就在于大量飞机、子弹间的碰撞检测,而pygame在此方面卓有建树,于是我下载并导入了它。随机函数库的导入则是为了方便后续敌人的生成,sys函数库导入则是为了退出游戏。导入pygame后就利用它完成窗口的创建,以及一些常量的初始化。

# -*- coding = utf-8 -*-

import pygame
from pygame.locals import *
from sys import exit
from random import randint

SCREEN_WIDTH = 480    #窗口大小
SCREEN_HEIGHT = 640
......
score = 0     #分数初始化
......
FRAME_RATE = 60       #最高帧率
ANIMATE_CYCLE = 30     #动画频率

ticks = 0     #时频计数
ticks_enemy = 0

clock = pygame.time.Clock()    #帧率对象生成

offset = {pygame.K_LEFT:0, pygame.K_RIGHT:0, pygame.K_UP:0, pygame.K_DOWN:0}    #按键初始化

fps_enemy1 = 30    #三种敌人的生成频率
fps_enemy2 = 300
fps_enemy3 = 1799

......

pygame.init()     #窗口生成
screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT])
pygame.display.set_caption('planes\'war')

第二步:编写玩家,子弹,敌人的类

借助网上收集的图片资源进行玩家,子弹,敌人的类的编写。(图片资源在导入图像部分再详说)

class Bullet1(pygame.sprite.Sprite):     #玩家子弹类
    def __init__(self, bullet1_surface, bullet1_init_pos):
        pygame.sprite.Sprite.__init__(self)
        self.image = bullet1_surface
        self.rect = self.image.get_rect()
        self.rect.topleft = bullet1_init_pos
        self.speed = 8
    def update(self):
        self.rect.top -= self.speed
        if self.rect.bottom < 0:
            self.kill()

class Bullet2(pygame.sprite.Sprite):    #敌人子弹类
    def __init__(self, bullet2_surface, bullet2_init_pos):
        pygame.sprite.Sprite.__init__(self)
        self.image = bullet2_surface
        self.rect = self.image.get_rect()
        self.rect.topleft = bullet2_init_pos
        self.speed = 8
    def update(self):
        self.rect.top += self.speed
        if self.rect.top > SCREEN_HEIGHT:
            self.kill()

class Hero(pygame.sprite.Sprite):   #玩家类
    def __init__(self, hero_surface, hero_init_pos):
        pygame.sprite.Sprite.__init__(self)
        self.image = hero_surface
        self.rect = self.image.get_rect()
        self.rect.topleft = hero_init_pos
        self.speed = 6
        self.is_hit = 0
        self.bullets_1 = pygame.sprite.Group()
    def single_shoot(self, bullet1_surface):      #发射子弹
        bullet_1 = Bullet1(bullet1_surface, self.rect.midtop)
        self.bullets_1.add(bullet_1)
    def move(self, offset):      #移动
        x = self.rect.left + offset[pygame.K_RIGHT] - offset[pygame.K_LEFT]
        y = self.rect.top + offset[pygame.K_DOWN] - offset[pygame.K_UP]
        if x < 0:
            self.rect.left = 0
        elif x > SCREEN_WIDTH - self.rect.width:
            self.rect.left = SCREEN_WIDTH - self.rect.width
        else:
            self.rect.left = x

        if y < 0:
            self.rect.top = 0
        elif y > SCREEN_HEIGHT - self.rect.height:
            self.rect.top = SCREEN_HEIGHT - self.rect.height
        else:
            self.rect.top = y

class Enemy1(pygame.sprite.Sprite):   #第一类敌人
    def __init__(self, enemy1_surface, enemy1_init_pos):
        pygame.sprite.Sprite.__init__(self)
        self.image = enemy1_surface
        self.rect = self.image.get_rect()
        self.rect.topleft = enemy1_init_pos
        self.speed = 2
        self.down_index = 0
    def update(self):
        self.rect.top += self.speed
        if self.rect.top > SCREEN_HEIGHT:
            self.kill()

class Enemy2(pygame.sprite.Sprite):     #第二类敌人
    def __init__(self, enemy2_surface, enemy2_init_pos):
        pygame.sprite.Sprite.__init__(self)
        self.image = enemy2_surface
        self.rect = self.image.get_rect()
        self.rect.topleft = enemy2_init_pos
        self.rect.midbottom = [self.rect.left+self.rect.width/2,self.rect.top+self.rect.height]
        self.speed = 2
        self.is_hit = 0
        self.down_index = 0
        self.bullets = pygame.sprite.Group()
    def update(self):
        self.rect.top += self.speed
        if self.rect.top > SCREEN_HEIGHT:
            self.kill()
    def single_shoot(self, bullet2_surface):
        bullet = Bullet2(bullet2_surface, self.rect.midbottom)
        self.bullets.add(bullet)

class Enemy3(pygame.sprite.Sprite):       #第三类敌人
    def __init__(self, enemy3_surface, enemy3_init_pos):
        pygame.sprite.Sprite.__init__(self)
        self.image = enemy3_surface
        self.rect = self.image.get_rect()
        self.rect.topleft = enemy3_init_pos
        self.is_hit = 0
        self.down_index = 0
......
hero_down_index = 1       #玩家坠落动画标识初始化

第三步:导入图像数据

图像数据都是从网上找的,源自源自Kill-Console的技术博客Pygame制作微信打飞机游戏PC版。图片资源以附件形式上传。

resources.zip 581.25K

background = pygame.image.load('resources/image/background.png')
gameover = pygame.image.load('resources/image/gameover.png')
shoot_img = pygame.image.load('resources/image/shoot.png')


hero_surface = []
hero_surface.append(shoot_img.subsurface(pygame.Rect(0, 99, 102, 126)))
hero_surface.append(shoot_img.subsurface(pygame.Rect(165, 360, 102, 126)))
hero_surface.append(shoot_img.subsurface(pygame.Rect(165, 234, 102, 126)))
hero_surface.append(shoot_img.subsurface(pygame.Rect(330, 624, 102, 126)))
hero_surface.append(shoot_img.subsurface(pygame.Rect(330, 498, 102, 126)))
hero_surface.append(shoot_img.subsurface(pygame.Rect(432, 624, 102, 126)))
hero_pos = [200, 500]


bullet1_surface = shoot_img.subsurface(pygame.Rect(1004, 987, 9, 21))
bullet2_surface = shoot_img.subsurface(pygame.Rect(69, 78, 9, 21))

enemy1_surface = shoot_img.subsurface(pygame.Rect(534, 612, 57, 43))
enemy1_down_surface = []
enemy1_down_surface.append(shoot_img.subsurface(pygame.Rect(267, 347, 57, 43)))
enemy1_down_surface.append(shoot_img.subsurface(pygame.Rect(873, 697, 57, 43)))
enemy1_down_surface.append(shoot_img.subsurface(pygame.Rect(267, 296, 57, 43)))
enemy1_down_surface.append(shoot_img.subsurface(pygame.Rect(930, 697, 57, 43)))

enemy2_surface = shoot_img.subsurface(pygame.Rect(0, 0, 69, 99))
enemy2_down_surface = []
enemy2_down_surface.append(shoot_img.subsurface(pygame.Rect(534, 655, 69, 95)))
enemy2_down_surface.append(shoot_img.subsurface(pygame.Rect(603, 655, 69, 95)))
enemy2_down_surface.append(shoot_img.subsurface(pygame.Rect(672, 653, 69, 95)))
enemy2_down_surface.append(shoot_img.subsurface(pygame.Rect(741, 653, 69, 95)))

enemy3_surface = shoot_img.subsurface(pygame.Rect(335, 750, 169, 258))
enemy3_down_surface = []
enemy3_down_surface.append(shoot_img.subsurface(pygame.Rect(0, 486, 165, 261)))
enemy3_down_surface.append(shoot_img.subsurface(pygame.Rect(0, 225, 165, 261)))
enemy3_down_surface.append(shoot_img.subsurface(pygame.Rect(839, 748, 165, 261)))
enemy3_down_surface.append(shoot_img.subsurface(pygame.Rect(165, 486, 165, 261)))
enemy3_down_surface.append(shoot_img.subsurface(pygame.Rect(673, 748, 165, 261)))
enemy3_down_surface.append(shoot_img.subsurface(pygame.Rect(0, 747, 165, 261)))
enemy3_pos = [150, 0]

hero = Hero(hero_surface[0], hero_pos)

enemy1_group = pygame.sprite.Group()
enemy1_down_group = pygame.sprite.Group()

enemy2_group = pygame.sprite.Group()
enemy2_down_group = pygame.sprite.Group()

enemy3_group = pygame.sprite.Group()
enemy3_down_group = pygame.sprite.Group()

第四步:编写主函数中玩家及敌人生成,时频判断和碰撞检测的部分

这是游戏的精髓,pygame的价值也体现在这里:通过精灵组实现同类对象的统一管理
我会在这具体介绍我的设计:对于玩家,每三分之一秒发射一枚子弹,共五滴血;
第一类敌人作为小兵,不发射子弹,一滴血,0.5秒生成一个,50分
第二类敌人作为中坚,发射子弹(子弹1.5秒发射一个,其他参数与玩家子弹相同),三滴血,5秒生成一个,500分
第三类敌人作为boss,不发射子弹但会改变其他敌人生成速率(第一类敌人0.05秒生成一个,第二类敌人2秒生成一个),十滴血,30秒生成一个,5000分
此外,只有用子弹击败敌人才得分
玩家能直接撞毁前两类敌人,但会被第三类敌人撞毁

while True:
    clock.tick(FRAME_RATE)
    screen.blit(background, (0, 0))

    if ticks >= ANIMATE_CYCLE:
        ticks = 0

    if hero.is_hit >= 5:   #玩家坠落判定
        hero.kill()
        if ticks%(ANIMATE_CYCLE//2) == 0:
            hero_down_index += 1
        hero.image = hero_surface[hero_down_index]
        if hero_down_index == 5:
            break
    else:
        hero.image = hero_surface[ticks//(ANIMATE_CYCLE//2)]

    if ticks % 20 == 0:
        hero.single_shoot(bullet1_surface)
    hero.bullets_1.update()
    hero.bullets_1.draw(screen)


    if ticks_enemy % fps_enemy1 == 0:      #敌人生成
        enemy1 = Enemy1(enemy1_surface, [randint(0, SCREEN_WIDTH - enemy1_surface.get_width()), -enemy1_surface.get_height()])
        enemy1_group.add(enemy1)
    enemy1_group.update()
    enemy1_group.draw(screen)

    if ticks_enemy % fps_enemy2 == 0:
        enemy2 = Enemy2(enemy2_surface, [randint(0, SCREEN_WIDTH - enemy1_surface.get_width()), -enemy1_surface.get_height()])
        enemy2_group.add(enemy2)
    enemy2_group.update()
    enemy2_group.draw(screen)
    for enemy2 in enemy2_group:
        if ticks_enemy % 90 == 0:
            enemy2.single_shoot(bullet2_surface)
        enemy2.bullets.update()
        enemy2.bullets.draw(screen)

    if ticks_enemy // fps_enemy3 == 1:
        enemy3 = Enemy3(enemy3_surface, enemy3_pos)
        enemy3_group.add(enemy3)
        fps_enemy1 = 15
        fps_enemy2 = 120
        ticks_enemy = 0
    enemy3_group.update()
    enemy3_group.draw(screen)

    if enemy1_down_group.add(pygame.sprite.groupcollide(enemy1_group, hero.bullets_1, True, True)):         #玩家子弹与敌人的碰撞检测
        score += 50

    pygame.sprite.groupcollide(enemy2.bullets, hero.bullets_1, True, True)      #玩家子弹与敌人子弹相遇抵消

    for enemy2 in enemy2_group:
        if enemy2.is_hit < 2:
            if pygame.sprite.groupcollide(enemy2_group, hero.bullets_1, False, True):
                enemy2.is_hit += 1
        else:
            enemy2_down_group.add(pygame.sprite.groupcollide(enemy2_group, hero.bullets_1, True, True))
            score += 500

    for enemy3 in enemy3_group:
        if enemy3.is_hit < 9:
            if pygame.sprite.groupcollide(enemy3_group, hero.bullets_1, False, True):
                enemy3.is_hit += 1
        else:
            enemy3_down_group.add(pygame.sprite.groupcollide(enemy3_group, hero.bullets_1, True, True))
            fps_enemy1 = 30
            fps_enemy3 = 300
            score += 5000

    for enemy1_down in enemy1_down_group:       #敌人坠落动画判定
        screen.blit(enemy1_down_surface[enemy1_down.down_index], enemy1_down.rect)
        if ticks % (ANIMATE_CYCLE//2) == 0:
            if enemy1_down.down_index < 3:
                enemy1_down.down_index += 1
            else:
                enemy1_down_group.remove(enemy1_down)

    for enemy2_down in enemy2_down_group:
        screen.blit(enemy2_down_surface[enemy2_down.down_index], enemy2_down.rect)
        if ticks % (ANIMATE_CYCLE//2) == 0:
            if enemy2_down.down_index < 3:
                enemy2_down.down_index += 1
            else:
                enemy2_down_group.remove(enemy2_down)

    for enemy3_down in enemy3_down_group:
        screen.blit(enemy3_down_surface[enemy3_down.down_index], enemy3_down.rect)
        if ticks % (ANIMATE_CYCLE//2) == 0:
            if enemy3_down.down_index < 5:
                enemy3_down.down_index += 1
            else:
                enemy3_down_group.remove(enemy3_down)


    enemy1_down_list = pygame.sprite.spritecollide(hero, enemy1_group, True)     #敌人,玩家碰撞检测
    if len(enemy1_down_list) > 0:
        enemy1_down_group.add(enemy1_down_list)
        hero.is_hit += 1


    enemy2_down_list = pygame.sprite.spritecollide(hero, enemy2_group, True)
    if len(enemy2_down_list) > 0:
        enemy2_down_group.add(enemy2_down_list)
        hero.is_hit += 1


    if pygame.sprite.spritecollide(hero, enemy3_group, False):
        hero.is_hit += 5

    if pygame.sprite.spritecollide(hero, enemy2.bullets, True):      #玩家被击中的判定
        hero.is_hit += 1

第五步:完成主函数中的事件判定

主要是对退出游戏的判定和对玩家移动的补充

    for event in pygame.event.get():    #退出游戏
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()

        if event.type == pygame.KEYDOWN:     #移动判定
            if event.key in offset:
                offset[event.key] = hero.speed
        elif event.type == pygame.KEYUP:
            if event.key in offset:
                offset[event.key] = 0


    hero.move(offset)

第六步:完成游戏结束后的画面呈现和退出判定

分数呈现和结束画面

font = pygame.font.SysFont("", 48)      #分数呈现
text = font.render("Score: " + str(score), True, (255, 0, 0))
text_rect = text.get_rect()
text_rect.centerx = screen.get_rect().centerx
text_rect.centery = screen.get_rect().centery - 50

screen.blit(gameover, (0, 0))      #结束画面
screen.blit(text, text_rect)

while True:       #退出游戏
    pygame.display.update()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()

4.实验过程中遇到的问题和解决方案

问题一:游戏结束后无法正常退出游戏:点叉号无反应
解决方案:原退出判定只在游戏中起效,为了解决问题,就在游戏结束后再次进行退出判定
问题二:设置对象移动方向时总是与设想的不一致
解决方案:通过对pygame的深入学习,我发现pygame的坐标轴与我们日常常见的坐标轴不同,y坐标是相反的,因此将涉及y坐标的代码都重新审查以解决问题

5.实验感想

本次实验对我来说颇具挑战性,完成这个游戏的过程也是十分曲折。但真正完成了这个实验后,我对python又有了更深入的了解,也进一步体会到了python的魅力。完成实验的过程虽然曲折,但这是必不可少的,只有真正在实践中,我才能真正成长。事实上,这个游戏还很简陋,有诸多不足,甚至可能还有我没发觉的bug。但这是我亲手写的,是我的成果,我对此感到骄傲。同时,我也会继续努力,完善我的游戏,让它真正出彩。

6.课程总结

很荣幸也很幸运我选了王志强老师的“python程序与设计”,在此之前我接触地更多的是C语言,python只有偶尔我上网查询ctf资料时,会在插件中遇到。对当时的我来说python显得很神秘,也很强大,我也因此产生了兴趣。同时,由于我本身对ctf的学习要求,需要接触学习python,于是我开始了一定程度的自学。当时,我学习的时候不得要领,东一榔头西一棒槌,最后连什么是面向对象的语言都没搞清。也因此,这学期我选修了这门课程,系统学习了python后,我才彻底地感受到了它的简洁但又功能多样,令我难以自拔。所以说,我很高兴能选修这门课,领略python的魅力。“人生苦短,我用python。”

...全文
423 回复 打赏 收藏 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复
数据集可视化效果见下方。 【数据集概况】 · 检测类别(中文):[火(fire)] · 训练集:2565 张 · 验证集:244 张 · 测试集:122 张 · 总计:2931 张 该数据集针对夜间道路环境中的火情检测需求,精准捕捉火源特征,为道路安全监控系统提供可靠的数据支持,具有显著的安全预警价值和实际应用意义。该数据集的训练集、验证集和测试集分别包含2565张、244张和122张图像,分布比例合理,能够有效支持模型的训练、验证和测试过程,确保模型性能的稳定性和可靠性。该数据集的标注工作严谨细致,火目标的边界框标注精准,与实际火源位置高度吻合,标注规范符合行业标准,为模型... 【训练曲线与评估图】 【模型训练配置】 参数 | 值 模型 | yolo26n 训练轮数 | 100 epochs 输入尺寸 | 640x640 批次大小 | 24 优化器 | auto 初始学习率 | 0.01 训练设备 【关键指标汇总】 训练了 58 个 epoch,最终轮指标: 指标 | 数值 mAP50 | **0.9537** mAP50-95 | 0.6455 Precision | 0.9156 Recall | 0.9168 train/box_loss | 1.0237 train/cls_loss | 0.5109 val/box_loss | 1.1818 val/cls_loss | 0.5480 【训练过程分析】 58 轮训练后 mAP50 达到 0.9537,模型收敛良好。Loss 曲线前段快速下降,后段趋于平稳,val_loss 无反弹,没有明显过拟合。但 mAP50-95 为 0.6455,和 mAP50 差距 0.31,定位精度仍有优化空间。 【模型性能评估】 Precision 0.9156、Recall 0.9168,精召双高,模型对火的检测能力强。 ...
可参考下方数据集可视化效果。 【数据集概况】 · 检测类别(中文):[底盘铭牌(Chassis)] · 训练集:3258 张 · 验证集:136 张 · 测试集:0 张 · 总计:3394 张 该数据集聚焦于工业制造与车辆检测场景中对汽车底盘铭牌的精准识别,涵盖多种品牌车辆(如通用、现代、日产、马自达等)在不同光照和污损条件下的铭牌图像。通过真实生产环境中的多样化样本采集,数据集有效支持了车辆身份验证、质量追溯及自动化检测系统的开发,具有显著的工程应用价值。... 【训练曲线与评估图】 【模型训练配置】 参数 | 值 模型 | yolo26n 训练轮数 | 100 epochs 输入尺寸 | 640x640 批次大小 | 24 优化器 | auto 初始学习率 | 0.01 训练设备 【关键指标汇总】 训练了 34 个 epoch,最终轮指标: 指标 | 数值 mAP50 | **0.9649** mAP50-95 | 0.5292 Precision | 0.9019 Recall | 0.8783 train/box_loss | 1.2850 train/cls_loss | 0.7588 val/box_loss | 1.4612 val/cls_loss | 0.6408 【训练过程分析】 34 轮训练后 mAP50 达到 0.9649,模型收敛良好。Loss 曲线前段快速下降,后段趋于平稳,val_loss 无反弹,没有明显过拟合。但 mAP50-95 为 0.5292,和 mAP50 差距 0.44,定位精度仍有优化空间。 【模型性能评估】 Precision 0.9019、Recall 0.8783,精度高于召回,存在一定漏检。 【预测效果展示】 验证集预测效果较好,检测框基本准确覆盖底盘铭牌,置信度整体偏高。 【改进建议】 1. 增强难例挖掘:在大规...
代码下载链接: https://pan.quark.cn/s/a4b39357ea24 远程桌面管理工具 基于 MSTSC 连接 Windows 远程桌面,并对其进行封装实现管理多个远程桌面配置的小工具 关键字: 效果图如下: 效果-主界面 效果-打开远程连接 1、什么是 MSTSC (Microsoft terminal services client) ? 创建与终端服务器或其他远程计算机的连接,适用于 Windows XP 及以上的 Windows 操作系统 MSTSC 还有一种说法,Microsoft Telnet Screen Control ,即“微软远程桌面控制”。 PS. 相信对本项目感兴趣的朋友肯定熟悉下面微软的官方工具,本项目也是基于其 ActiveX 控件进行的封装,实现对远程桌面的管理与使用 MSTSC 2、赶紧来添加第一个远程桌面连接 ? 点击左下角的 来,开启 选项,起个 并 来添加您的一个父级(分组)节点,譬如 样例-添加父节点 再次点击点击左下角的 来新建您的第一个连接,假定您已熟悉 MSTSC 相关设置,那么下面这个配置也难不住您了 样例-添加子节点 样例-显示新增的配置 单击此方块即可连接服务器,请确保网络无阻哦,一般来说 MSTSC 能连上,本工具同样可以 样例-正在连接 样例-连接成功 好了,基本上您已经掌握如何使用此工具了 3、还有其他功能应用? 支持拖动页签头来改变其位置 (同时连接了多个远程可能会有些用) 当你不想打开远程桌面,而只想打开远程服务器上的某个程序时,请在 时填写需要启动程序的路径,如 ,如果该目录已配置到环境变量的 中,如本示例使用程序名 亦同 右击页面头会有菜单选项,比如可执行 操作,右击连接方块或分组...

110

社区成员

发帖
与我相关
我的任务
社区描述
人生苦短,我用Python!
python3.11 高校
社区管理员
  • blackwall0321
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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