m0_74057756 2022-10-18 23:14 采纳率: 66.7%
浏览 21
已结题

Python运行中磁盘空间变小

Python运行中磁盘空间变小

运行Python代码,运行后发现D盘空间变小,早上还有9G,晚上就只有1G了。但是关机再开机,D盘大小又恢复正常。Python装在D盘,代码分成三个文件,都在D盘。

代码如下(非专业人士,写的不好请见谅)
main.py(运行时执行的主程序)

"""保护符:Python之神保护以下代码不出BUG"""
# 导入需要的模块
import pygame as p
import os, sys
from pygame import time
from NaCl工具 import *
import Class

os.chdir(sys.path[0])  # 将工作目录改为当前目录,方便使用相对路径
p.init()  # 初始化pygame
screen = 窗口((900, 900), "飞机大战")
背景色 = (0, 255, 255)
# 精灵相关
总精灵组 = p.sprite.RenderUpdates()  # 存储所有当前需要的精灵的精灵组
自机 = Class.自机("./Data/歼击机.png", 尺寸=(128, 128), 旋转角度=90)
总精灵组.add(自机)
自机.rect.center = (450, 450)
screen.fill(背景色)
p.display.flip()
# 设置帧率,即FPS
clock = time.Clock()
FPS = 30
# 固定代码段,实现点击"X"号退出界面的功能,几乎所有的pygame都会使用该段代码
while True:
    clock.tick(FPS)
    # 循环获取事件,监听事件状态
    for event in p.event.get():
        screen.fill(背景色)
        总精灵组.update(event, screen,self)
        总精灵组.draw(screen)
        # 判断用户是否点了"X"关闭按钮,并执行if代码段
        if event.type == p.QUIT:
            # 卸载所有模块
            p.quit()
            # 终止程序,确保退出程序
            sys.exit()
    p.display.update()  # 更新屏幕内容

class.py(存放类)

# 导入需要的模块
import enum

import pygame as p
from pygame import sprite
from NaCl工具 import *

os.chdir(sys.path[0])  # 将工作目录改为当前目录,方便使用相对路径
p.init()  # 初始化pygame


class 自机(精灵):
    def __init__(self,
                 图片路径: str,
                 key: Tuple[int, int, int] = (0, 255, 0),
                 尺寸: Tuple[int, int] = (0, 0),
                 旋转角度: int = 0
                 ) -> None:
        super().__init__(图片路径, key, 尺寸, 旋转角度)

    def 跟随鼠标移动(self, event: e.Event) -> None:
        if event.type == p.MOUSEMOTION:
            self.rect.center = event.pos

    def update(self,
               event: e.Event,
               screen: p.Surface,
               group: sprite.Group,
               调试: bool = False
               ) -> None:
        """更新自机的位置和行为,并传入一个Rect表来制作脏矩形动画"""
        self.跟随鼠标移动(event)
        if 调试:
            self.调试(screen)
        if event.type == p.MOUSEBUTTONDOWN:
            自机子弹 = 自机子弹类('Data/子弹.png')
            group.add(自机子弹)


class 自机子弹类(精灵):
    def __init__(self,
                 图片路径: str,
                 key: Tuple[int, int, int] = (0, 255, 0),
                 尺寸: Tuple[int, int] = (0, 0),
                 旋转角度: int = 0
                 ) -> None:
        super().__init__(图片路径, key, 尺寸, 旋转角度)

    def update(self) -> None:
        self.前进(-10, 0)
# 导入需要的模块
import pygame as p
import os
import sys
from pygame import image, sprite, display, transform, Rect, math, draw
from pygame import event as e
from typing import Tuple, List, Union, Any
import abc

p.init()  # 初始化pygame


# 资源管理


def 窗口(尺寸: Tuple[int, int],
         title: str
         ) -> p.Surface:
    """生成窗口的函数"""
    screen: p.Surface = display.set_mode(尺寸)
    display.set_caption(title)
    return screen


def 图片(path: str,
         key: Tuple[int, int, int] = (0, 255, 0),
         尺寸: Tuple[int, int] = (0, 0),
         旋转角度: int = 0
         ) -> p.Surface:
    """加载图片的函数"""
    picture = image.load(path).convert()
    picture.set_colorkey(key)
    if 尺寸 != (0, 0):
        picture = transform.scale(picture, 尺寸)
    picture = transform.rotate(picture, 旋转角度)
    return picture


class 精灵(sprite.Sprite):
    """一个优化的精灵类"""

    def __init__(self,
                 图片路径: str,
                 key: Tuple[int, int, int] = (0, 255, 0),
                 尺寸: Tuple[int, int] = (0, 0),
                 旋转角度: int = 0
                 ) -> None:
        sprite.Sprite.__init__(self)
        self.原图 = self.image
        self.image: p.Surface = 图片(图片路径, key, 尺寸, 旋转角度)
        self.rect: Rect = self.image.get_rect()
        self.__子rect: Rect = self.rect
        self.方向: float = 0
        self.速度: int = 0

    def 旋转(self, 角度: float) -> None:
        self.image = transform.rotate(self.原图, 角度)
        self.rect = self.image.get_rect(center=self.rect.center)

    def 设置子rect(self, 宽: float, 高: float) -> None:
        子矩形left = self.rect.left + (self.rect.width - 宽) / 2
        子矩形top = self.rect.top + (self.rect.height - 高) / 2
        self.__子rect = Rect(子矩形left, 子矩形top, 宽, 高)

    def 子rect碰撞(self, other: "精灵") -> bool:
        return Rect.colliderect(self.__子rect, other.__子rect)

    def 方向变更(self, new: float) -> None:
        self.方向 = new

    def 前进(self, x: float, y: float) -> None:
        self.rect.move_ip(x, y)
        self.__子rect.move_ip(x, y)

    def 向量前进(self) -> None:
        前进向量 = math.Vector2(self.速度, 0)
        前进向量.rotate_ip(self.方向)
        x, y = 前进向量
        self.前进(x, y)

    def 调试(self, screen: p.Surface) -> None:
        draw.rect(screen, (0, 0, 0), self.rect, 1)
        draw.rect(screen, (255, 0, 0), self.__子rect, 1)

    @abc.abstractmethod
    def update(self, *args: Any, **kwargs: Any) -> None:
        pass

NaCl工具.py(自己写的工具集)

# 导入需要的模块
import pygame as p
import os
import sys
from pygame import image, sprite, display, transform, Rect, math, draw
from pygame import event as e
from typing import Tuple, List, Union, Any
import abc

p.init()  # 初始化pygame


# 资源管理


def 窗口(尺寸: Tuple[int, int],
         title: str
         ) -> p.Surface:
    """生成窗口的函数"""
    screen: p.Surface = display.set_mode(尺寸)
    display.set_caption(title)
    return screen


def 图片(path: str,
         key: Tuple[int, int, int] = (0, 255, 0),
         尺寸: Tuple[int, int] = (0, 0),
         旋转角度: int = 0
         ) -> p.Surface:
    """加载图片的函数"""
    picture = image.load(path).convert()
    picture.set_colorkey(key)
    if 尺寸 != (0, 0):
        picture = transform.scale(picture, 尺寸)
    picture = transform.rotate(picture, 旋转角度)
    return picture


class 精灵(sprite.Sprite):
    """一个优化的精灵类"""

    def __init__(self,
                 图片路径: str,
                 key: Tuple[int, int, int] = (0, 255, 0),
                 尺寸: Tuple[int, int] = (0, 0),
                 旋转角度: int = 0
                 ) -> None:
        sprite.Sprite.__init__(self)
        self.原图 = self.image
        self.image: p.Surface = 图片(图片路径, key, 尺寸, 旋转角度)
        self.rect: Rect = self.image.get_rect()
        self.__子rect: Rect = self.rect
        self.方向: float = 0
        self.速度: int = 0

    def 旋转(self, 角度: float) -> None:
        self.image = transform.rotate(self.原图, 角度)
        self.rect = self.image.get_rect(center=self.rect.center)

    def 设置子rect(self, 宽: float, 高: float) -> None:
        子矩形left = self.rect.left + (self.rect.width - 宽) / 2
        子矩形top = self.rect.top + (self.rect.height - 高) / 2
        self.__子rect = Rect(子矩形left, 子矩形top, 宽, 高)

    def 子rect碰撞(self, other: "精灵") -> bool:
        return Rect.colliderect(self.__子rect, other.__子rect)

    def 方向变更(self, new: float) -> None:
        self.方向 = new

    def 前进(self, x: float, y: float) -> None:
        self.rect.move_ip(x, y)
        self.__子rect.move_ip(x, y)

    def 向量前进(self) -> None:
        前进向量 = math.Vector2(self.速度, 0)
        前进向量.rotate_ip(self.方向)
        x, y = 前进向量
        self.前进(x, y)

    def 调试(self, screen: p.Surface) -> None:
        draw.rect(screen, (0, 0, 0), self.rect, 1)
        draw.rect(screen, (255, 0, 0), self.__子rect, 1)

    @abc.abstractmethod
    def update(self, *args: Any, **kwargs: Any) -> None:
        pass
  • 写回答

1条回答 默认 最新

  • 吕布辕门 后端领域新星创作者 2022-10-19 13:32
    关注

    不知道题主是不是用的pycharm,它运行完程序后会在安装盘产生大量缓存文件,如果是删除C:\Users\15839\AppData\Local\JetBrains\PyCharm2020.2\caches\content.dat.storageData就可以了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 10月27日
  • 已采纳回答 10月19日
  • 创建了问题 10月18日

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效