Pygame 将游戏拆分成不同的文件后运行速度变慢
Pygame runs slower after splitting the game into different files
我在 pygame 中编写了一个简单的游戏。它曾经全部在一个文件中,但我决定保持干净,所以我将代码分成不同的脚本。这样做之后,游戏运行速度比以前慢了很多。我相信延迟是由每帧引用导入的变量和函数引起的,但仍然不知道如何解决这个问题。代码如下,希望对你有所帮助:
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
提前致谢!
您调用 pygame.display.update
两次(在 player.run
和 main.main
中)。
将其从 player.run
中删除即可解决问题。
我在 pygame 中编写了一个简单的游戏。它曾经全部在一个文件中,但我决定保持干净,所以我将代码分成不同的脚本。这样做之后,游戏运行速度比以前慢了很多。我相信延迟是由每帧引用导入的变量和函数引起的,但仍然不知道如何解决这个问题。代码如下,希望对你有所帮助:
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
提前致谢!
您调用 pygame.display.update
两次(在 player.run
和 main.main
中)。
将其从 player.run
中删除即可解决问题。