将游戏分割成不同的文件后,Pygame 运行速度变慢

发布于 2025-01-19 14:04:08 字数 2446 浏览 1 评论 0原文

我用 pygame 编写了一个简单的游戏。它曾经全部在一个文件中,但我决定保持它的干净,所以我将代码分成不同的脚本。这样做之后,游戏的运行速度比以前慢了很多。我相信延迟是由每帧引用导入的变量和函数引起的,但仍然不知道如何解决问题。这是代码,希望对您有所帮助:

ma​​in.py

import pygame
import player
import window

def main():
    pygame.init()

    running = True

    clock = pygame.time.Clock()

    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False

        window.refresh()
        player.run()

        clock.tick(window.FPS)
        print(clock.get_fps())
        pygame.display.update()

    pygame.quit()

if __name__ == "__main__":
    main()

player.py

import pygame
from math import sqrt
from window import WIDTH, HEIGHT, WIN
from classes import *

player = Obj(50, 50, 50, 10)

def normalize(x, y):
    if x != 0 and y != 0:
        return (x / sqrt(2), y / sqrt(2))
    return (x, y)


def movement(obj: Obj):
    keys = pygame.key.get_pressed()

    startx = obj.x
    starty = obj.y

    x = 0
    y = 0

    if keys[pygame.K_w]:
        y -= obj.vel
    if keys[pygame.K_d]:
        x += obj.vel
    if keys[pygame.K_a]:
        x -= obj.vel
    if keys[pygame.K_s]:
        y += obj.vel

    pos = normalize(x, y)

    obj.x += pos[0]
    obj.y += pos[1]

    speed = abs(sqrt((obj.x - startx) ** 2 + (obj.y - starty) ** 2))
    # print(speed)


def on_track(obj: Obj):
    if obj.x >= WIDTH - obj.size:
        obj.x = WIDTH - obj.size
    if obj.x <= 0:
        obj.x = 0
    if obj.y >= HEIGHT - obj.size:
        obj.y = HEIGHT - obj.size
    if obj.y <= 0:
        obj.y = 0


def run():
    pygame.draw.rect(WIN, (Color.black), (player.x, player.y, player.size, player.size))
    movement(player)
    on_track(player)
    pygame.display.update()

window.py

from classes import *
import pygame

WIDTH, HEIGHT = 800, 600
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Game")
FPS = 60


def refresh():
    WIN.fill(Color.white)

classes.py

class Color(): 
    white = (255, 255, 255)
    black = (0, 0, 0)
    blue = (0, 0, 255)
    red = (255, 0, 0)
    green = (0, 255, 0)


class Obj():
    def __init__(self, x, y, size, vel):
        self.x = x
        self.y = y
        self.size = size
        self.vel = vel

谢谢你提前!

I've written a simple game in pygame. It used to be all in one file, but I decided to keep it clean, so I separated the code into different scripts. After doing so, the game runs much slower than before. I believe the lag is caused by refering to imported variables and functions every frame, but yet still don't know how to fix the issue. Here's the code, hope you can help:

main.py

import pygame
import player
import window

def main():
    pygame.init()

    running = True

    clock = pygame.time.Clock()

    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False

        window.refresh()
        player.run()

        clock.tick(window.FPS)
        print(clock.get_fps())
        pygame.display.update()

    pygame.quit()

if __name__ == "__main__":
    main()

player.py

import pygame
from math import sqrt
from window import WIDTH, HEIGHT, WIN
from classes import *

player = Obj(50, 50, 50, 10)

def normalize(x, y):
    if x != 0 and y != 0:
        return (x / sqrt(2), y / sqrt(2))
    return (x, y)


def movement(obj: Obj):
    keys = pygame.key.get_pressed()

    startx = obj.x
    starty = obj.y

    x = 0
    y = 0

    if keys[pygame.K_w]:
        y -= obj.vel
    if keys[pygame.K_d]:
        x += obj.vel
    if keys[pygame.K_a]:
        x -= obj.vel
    if keys[pygame.K_s]:
        y += obj.vel

    pos = normalize(x, y)

    obj.x += pos[0]
    obj.y += pos[1]

    speed = abs(sqrt((obj.x - startx) ** 2 + (obj.y - starty) ** 2))
    # print(speed)


def on_track(obj: Obj):
    if obj.x >= WIDTH - obj.size:
        obj.x = WIDTH - obj.size
    if obj.x <= 0:
        obj.x = 0
    if obj.y >= HEIGHT - obj.size:
        obj.y = HEIGHT - obj.size
    if obj.y <= 0:
        obj.y = 0


def run():
    pygame.draw.rect(WIN, (Color.black), (player.x, player.y, player.size, player.size))
    movement(player)
    on_track(player)
    pygame.display.update()

window.py

from classes import *
import pygame

WIDTH, HEIGHT = 800, 600
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Game")
FPS = 60


def refresh():
    WIN.fill(Color.white)

classes.py

class Color(): 
    white = (255, 255, 255)
    black = (0, 0, 0)
    blue = (0, 0, 255)
    red = (255, 0, 0)
    green = (0, 255, 0)


class Obj():
    def __init__(self, x, y, size, vel):
        self.x = x
        self.y = y
        self.size = size
        self.vel = vel

Thank you in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╄→承喏 2025-01-26 14:04:08

您调用pygame.display.update两次(在player.runmain.main中)。
player.run 中删除它可以解决问题。

You call pygame.display.update twice (in player.run and main.main).
Removing it from player.run solves the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文