Pygame 应用程序在 Mac 上的运行速度比在 PC 上慢 [英] Pygame application runs slower on Mac than on PC

查看:68
本文介绍了Pygame 应用程序在 Mac 上的运行速度比在 PC 上慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我和一个朋友正在使用 Python (2.7) 和 Pygame 模块制作游戏.到目前为止,我主要完成了游戏的美术工作,而他主要完成了编码工作,但最终我计划在大部分美术工作完成后帮助他编写代码.我使用的是 Mac(最新版本的 OS X),而我的朋友使用的是 PC.

A friend and I are making a game in Python (2.7) with the Pygame module. I have mostly done the art for the game so far and he has mostly done the coding but eventually I plan to help code with him once most of the art is done. I am on a Mac (latest version of OS X) and my friend is using a PC.

他一直在他的 PC 上构建和运行游戏,截至目前,它在他的 PC 上按计划运行(完美 60fps).然而,每当我从 GitHub 中提取代码(我肯定有我们代码的最新版本)并尝试运行游戏时,游戏运行速度只有一半.

He has been building and running the game from his PC and as of right now it has been working as planned in his PC (perfect 60fps). However, whenever I pull the code from GitHub (I definitely have the most updated version of our code) and I try to run the game, the game runs like half as fast.

我们尝试在代码中将 fps 加倍到 120,然后它在 PC 上的运行速度是原来的两倍,但是当我在 Mac 上提取该代码时,它似乎仍然被限制在 30fps 左右.

We have tried doubling the fps to 120 in the code and it then runs twice as fast on the PC but when I pull that code on my Mac it still seemed like I was capped around 30fps.

我们在其他任何地方都没有真正找到任何令人信服的答案,但是我们对 Pygame 和 Python 都很陌生,所以我们可能会遗漏一些非常明显的东西.

We haven't really found any convincing answers to this problem anywhere else, however we are both pretty new to Pygame and Python so we may be missing something very obvious.

import pygame as pg
import os

os.environ['SDL_VIDEO_CENTERED'] = '1'

class Wombat:
    def __init__(self, screen_rect, image, starting_loc):
        self.screen_rect = screen_rect
        self.image = image
        self.width = 192
        self.height = 96
        self.starting_loc = starting_loc
        self.rect = self.image.get_rect(bottomleft=starting_loc)
        self.speed = 5
        self.grav = .5

        self.jumping = False
        self.y_vel = 0

    def update(self):
        self.rect.clamp_ip(self.screen_rect)
        self.jump_update()

    def render(self, screen):
        screen.blit(self.image, self.rect)

    def move(self, x, y):
        self.rect.x += x * self.speed
        self.rect.y += y * self.speed

    def jump_update(self):
        if self.jumping:
            self.y_vel += self.grav
            self.rect.y += self.y_vel
            if self.is_touching_ground():
                self.jumping = False

    def is_touching_ground(self):
        return self.rect.y >= self.screen_rect.height - self.height - 50

    def jump(self):
        if not self.jumping:
            self.y_vel = -12
            self.jumping = True


class Control:
    def __init__(self):
        self.screensize = (1000,500)
        self.screen = pg.display.set_mode(self.screensize, pg.DOUBLEBUF)
        self.screen_rect = self.screen.get_rect()
        try:
            self.bg = pg.image.load("res\\bg.png")
            self.wb11 = pg.image.load("res\BlueWombat\BlueStay.png")
            self.wb1 = pg.image.load("res\BlueWombat\BlueWalk.gif").convert_alpha()
            self.wb2 = pg.image.load("res\GreenWombat\GreenStay.png")
            self.wb21 = pg.image.load("res\GreenWombat\GreenWalk.gif")
        except:
            self.bg = pg.image.load("res/bg.png")
            self.wb1 = pg.image.load("res/BlueWombat/BlueStay.png")
            self.wb11 = pg.image.load("res/BlueWombat/BlueWalk.gif")
            self.wb2 = pg.image.load("res/GreenWombat/GreenStay.png")
            self.wb21 = pg.image.load("res/GreenWombat/GreenWalk.gif")
        self.wb2 = pg.transform.flip(self.wb2, True, False)
        self.clock = pg.time.Clock()
        self.fps = 60
        self.quit = False
        self.keys = pg.key.get_pressed()

        self.wombat_one = Wombat(self.screen_rect, self.wb1, (0,450))
        self.wombat_two = Wombat(self.screen_rect, self.wb2, (1000-192,450))

    def run(self):
        while not self.quit:
            now = pg.time.get_ticks()
            self.held_keys(self.keys)
            self.event_loop()
            self.update()
            self.render()
            pg.display.update()

            self.clock.tick(self.fps)

    def event_loop(self):
        for event in pg.event.get():
            if event.type == pg.QUIT:
                self.quit = True
            elif event.type in (pg.KEYDOWN, pg.KEYUP):
                self.keys = pg.key.get_pressed()
                if event.type == pg.KEYDOWN:
                    if event.key == pg.K_w:
                        self.wombat_one.jump()
                    if event.key == pg.K_UP:
                        self.wombat_two.jump()

    def held_keys(self, keys):
        if keys[pg.K_a]:
            self.wombat_one.move(-1, 0)
        if keys[pg.K_d]:
            self.wombat_one.move(1, 0)
        if keys[pg.K_LEFT]:
            self.wombat_two.move(-1, 0)
        if keys[pg.K_RIGHT]:
            self.wombat_two.move(1, 0)

    def render(self):
        self.screen.blit(self.bg, (0,0))
        self.wombat_one.render(self.screen)
        self.wombat_two.render(self.screen)

    def update(self):
        self.wombat_one.update()
        self.wombat_two.update()


app = Control()
app.run()

推荐答案

嘿,嗯,我遇到了同样的问题,但现在我的 pygame 代码以 60 fps 运行,这很好.我将 Idle 与 Python 3.6.3 和相应的 pygame 一起使用.这是我修复它的方法:

Hey umm I had the same problem but now my pygame code runs at 60 fps which is good. I am using Idle with Python 3.6.3 and the appropriate pygame for it. Here is how I fixed it:

  1. 运行你的 pygame 程序
  2. 在码头中,您会看到一条嘴里叼着控制器的蛇.右键点击他.
  3. 转到选项"并单击在 Finder 中显示"
  4. Finder 将打开,您将看到一个 Python 应用程序.(我的是火箭形状,上面有空闲符号.)
  5. 右键单击 Python 应用程序,然后单击获取信息".
  6. 选中以低分辨率打开"框,它现在应该以 60fps 左右的速度运行.

这篇关于Pygame 应用程序在 Mac 上的运行速度比在 PC 上慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆