将游戏分割成不同的文件后,Pygame 运行速度变慢
我用 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
谢谢你提前!
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您调用
pygame.display.update
两次(在player.run
和main.main
中)。从
player.run
中删除它可以解决问题。You call
pygame.display.update
twice (inplayer.run
andmain.main
).Removing it from
player.run
solves the problem.