Pygame application runs slower on Mac than on PC

2024/9/27 7:26:17

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.

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.

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.

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 osos.environ['SDL_VIDEO_CENTERED'] = '1'class Wombat:def __init__(self, screen_rect, image, starting_loc):self.screen_rect = screen_rectself.image = imageself.width = 192self.height = 96self.starting_loc = starting_locself.rect = self.image.get_rect(bottomleft=starting_loc)self.speed = 5self.grav = .5self.jumping = Falseself.y_vel = 0def 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.speedself.rect.y += y * self.speeddef jump_update(self):if self.jumping:self.y_vel += self.gravself.rect.y += self.y_velif self.is_touching_ground():self.jumping = Falsedef is_touching_ground(self):return self.rect.y >= self.screen_rect.height - self.height - 50def jump(self):if not self.jumping:self.y_vel = -12self.jumping = Trueclass 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 = 60self.quit = Falseself.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 = Trueelif 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()
Answer

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. Run your pygame program
  2. In the dock you will see a snake with controllers in his mouth. Right click him.
  3. Go to Options and click "Show in Finder"
  4. Finder will open and you will see a the python application.(Mine was in the shape of rocket with the idle symbol on it.)
  5. Right click the python application and click "Get Info".
  6. Check the box "Open in Low Resolution" and it should now run at around 60fps.
https://en.xdnf.cn/q/71476.html

Related Q&A

How to extract feature vector from single image in Pytorch?

I am attempting to understand more about computer vision models, and Im trying to do some exploring of how they work. In an attempt to understand how to interpret feature vectors more Im trying to use …

Which language should I use for Artificial intelligence on web projects

I have to do one project for my thesis involving Artificial intelligence, collaborative filtering and machine learning methods.I only know PHP/mysq/JS, and there is not much AI stuff examples in PHP.Th…

Scrapy with selenium, webdriver failing to instantiate

I am trying to use selenium/phantomjs with scrapy and Im riddled with errors. For example, take the following code snippet:def parse(self, resposne):while True:try:driver = webdriver.PhantomJS()# do so…

How do I enable TLS on an already connected Python asyncio stream?

I have a Python asyncio server written using the high-level Streams API. I want to enable TLS on an already established connection, as in STARTTLS in the SMTP and IMAP protocols. The asyncio event loop…

Validate with three xml schemas as one combined schema in lxml?

I am generating an XML document for which different XSDs have been provided for different parts (which is to say, definitions for some elements are in certain files, definitions for others are in other…

An unusual Python syntax element frequently used in Matplotlib

One proviso: The syntax element at the heart of my Question is in the Python language; however, this element appears frequently in the Matplotlib library, which is the only context i have seen it. So w…

Control the power of a usb port in Python

I was wondering if it could be possible to control the power of usb ports in Python, using vendor ids and product ids. It should be controlling powers instead of just enabling and disabling the ports. …

Threads and local proxy in Werkzeug. Usage

At first I want to make sure that I understand assignment of the feature correct. The local proxy functionality assigned to share a variables (objects) through modules (packages) within a thread. Am I …

Unable to use google-cloud in a GAE app

The following line in my Google App Engine app (webapp.py) fails to import the Google Cloud library:from google.cloud import storageWith the following error:ImportError: No module named google.cloud.st…

Multiple thermocouples on raspberry pi

I am pretty new to the GPIO part of the raspberry Pi. When I need pins I normally just use Arduino. However I would really like this project to be consolidated to one platform if possible, I would li…