So, I am making a game engine in python using pygame. I am trying to get and display fps using pygame.time.Clock().get_fps(), but it is only saying 0.0 . Here is the basic code:
import pygame
from pygame.locals import *
screen = pygame.display.set_mode()
while 1:
print(pygame.time.Clock().get_fps())
pygame.time.Clock().tick(60)
I just need anything that exactly shows current fps.
See get_fps()
Compute your game's framerate (in frames per second). It is computed by averaging the last ten calls to Clock.tick().
So there are 2 reasons why your code does not work. You create a new instance of pygame.time.Clock() every time you call it, and the FPS cannot be averaged. But an even bigger problem is that you call tick from another instance.
You must create one Clock object before the application loop, but not in the application loop, and call tick and get_fps from that object. Also handle the events in the application loop (PyGame window not responding after a few seconds):
import pygame
from pygame.locals import *
screen = pygame.display.set_mode()
clock = pygame.time.Clock()
while 1:
pygame.event.pump()
print(clock.get_fps())
clock.tick(60)
A better way would be to create your clock at the top of your code. Then within the game loop at the end you can enact the .tick function.
import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode()
clock = pygame.time.Clock()
while True:
pygame.display.update()
clock.tick(60)
Related
I have a simple Pygame program:
#!/usr/bin/env python
import pygame
from pygame.locals import *
pygame.init()
win = pygame.display.set_mode((400,400))
pygame.display.set_caption("My first game")
But every time I try to run it, I get this:
pygame 2.0.0 (SDL 2.0.12, python 3.8.3)
Hello from the pygame community. https://www.pygame.org/contribute.html
And then nothing happens.
Why I can't run this program?
Your application works well. However, you haven't implemented an application loop:
import pygame
from pygame.locals import *
pygame.init()
win = pygame.display.set_mode((400,400))
pygame.display.set_caption("My first game")
clock = pygame.time.Clock()
run = True
while run:
# handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
# update game objects
# [...]
# clear display
win.fill((0, 0, 0))
# draw game objects
# [...]
# update display
pygame.display.flip()
# limit frames per second
clock.tick(60)
pygame.quit()
The typical PyGame application loop has to:
handle the events by calling either pygame.event.pump() or pygame.event.get().
update the game states and positions of objects dependent on the input events and time (respectively frames)
clear the entire display or draw the background
draw the entire scene (blit all the objects)
update the display by calling either pygame.display.update() or pygame.display.flip()
limit frames per second to limit CPU usage with pygame.time.Clock.tick
repl.it/#Rabbid76/PyGame-MinimalApplicationLoop See also Event and application loop
This is interesting. Computer read your program line by line[python]. When all the line are interpreted, the program closed. To solve this problem you need to add a while loop to make sure the program will continue until you close the program.
import pygame,sys
from pygame.locals import *
pygame.init()
pygame.display.set_caption("My first game")
win = pygame.display.set_mode((400,400))
#game loop keeps the game running until you exit the game.
game_running=True
while game_running:
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
win.fill((0, 250, 154)) #Fill the pygame window with specific color. You can use hex or rgb color
pygame.display.update() #Refresh the pygame window
You can check more pygame Examples.
https://github.com/01one/Pygame-Examples
I think this will be helpful.
I have a simple Pygame program:
#!/usr/bin/env python
import pygame
from pygame.locals import *
pygame.init()
win = pygame.display.set_mode((400,400))
pygame.display.set_caption("My first game")
But every time I try to run it, I get this:
pygame 2.0.0 (SDL 2.0.12, python 3.8.3)
Hello from the pygame community. https://www.pygame.org/contribute.html
And then nothing happens.
Why I can't run this program?
Your application works well. However, you haven't implemented an application loop:
import pygame
from pygame.locals import *
pygame.init()
win = pygame.display.set_mode((400,400))
pygame.display.set_caption("My first game")
clock = pygame.time.Clock()
run = True
while run:
# handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
# update game objects
# [...]
# clear display
win.fill((0, 0, 0))
# draw game objects
# [...]
# update display
pygame.display.flip()
# limit frames per second
clock.tick(60)
pygame.quit()
The typical PyGame application loop has to:
handle the events by calling either pygame.event.pump() or pygame.event.get().
update the game states and positions of objects dependent on the input events and time (respectively frames)
clear the entire display or draw the background
draw the entire scene (blit all the objects)
update the display by calling either pygame.display.update() or pygame.display.flip()
limit frames per second to limit CPU usage with pygame.time.Clock.tick
repl.it/#Rabbid76/PyGame-MinimalApplicationLoop See also Event and application loop
This is interesting. Computer read your program line by line[python]. When all the line are interpreted, the program closed. To solve this problem you need to add a while loop to make sure the program will continue until you close the program.
import pygame,sys
from pygame.locals import *
pygame.init()
pygame.display.set_caption("My first game")
win = pygame.display.set_mode((400,400))
#game loop keeps the game running until you exit the game.
game_running=True
while game_running:
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
win.fill((0, 250, 154)) #Fill the pygame window with specific color. You can use hex or rgb color
pygame.display.update() #Refresh the pygame window
You can check more pygame Examples.
https://github.com/01one/Pygame-Examples
I think this will be helpful.
import pygame
import sys
from pygame.locals import *
DISPLAY_SURF = pygame.display.set_mode((640,480))
#Sets the resolution to 640 pixels by 720 pixels
class Game:
def __init__(self):
pygame.init()
self.FPS = 60
self.fps_clock = pygame.time.Clock()
self.surface = pygame.display.set_mode((640, 480))
pygame.display.set_caption("The Hunt")
img = pygame.image.load("Graphics/background.png")
self.surface.blit(img)
#This class sets the basic attributes for the window.
#The clock is set to 60 and the name of the window
#is set to The Hunt which is a working title for my project
def run(self):
while True:
pygame.display.update()
self.fps_clock.tick(self.FPS)
self.process_game()
#This updates the window display to refresh every clock tick
def process_game(self):
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
game = Game()
#This creates an instance of the class Game which gives all the attributes
# and behaviours to this instance
game.run()
#Calling this function generates a window with the attributes defined.
I need some help. I have already checked if it is in the same folder, the file is definitely a png and I spelt all the folder names and the destination correctly. I'm open to any suggestions
I'm going to answer this question despite that it is not really a good one for Stack Overflow. On this site, you'll have to be more specific and detailed, because no one intends to read through a huge lot of code for you. I did however pickup some things that I think can be fixed ( some of these are opinion based, something that your question should never force an answer to have), but... here it is anyway:
For starters, when you construct a class, you use parenthesis after the class name, even if it's not going to inherit anything form another class. So change the line where you construct the Game class to this:
class Game():
Second thing about this code is that if your going to create the pygame window surface inside the Game() class, I don't understand why you're creating another window at the beginning of your code. If there is a reason for this please explain it in a comment in your code.
The last thing is more opinion-based . I don't know how many people create Pygame GUI applications like this, but it would be simpler to not use classes so you could understand the code better. When I create a Pygame GUI, I define the window, then the sprites, then I run the main game loop in a While Loop. Here is how I would normally structure your program:
#import pygame
import pygame, sys
from pygame.locals import *
#Initialize pygame and define colours
pygame.init()
white = 255,255,255
#Sets the resolution to 640 pixels by 720 pixels and caption for pygame window
DISPLAY_SURF = pygame.display.set_mode((640,480))
pygame.display.set_caption("The Hunt!")
#Create a clock object
clock = pygame.time.Clock()
FPS = 60
#Define a variable to refer to image
image = pygame.image.load("download.jpg")
#Start main loop
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
DISPLAY_SURF.fill(white)
DISPLAY_SURF.blit(image,(0,0))
pygame.display.update()
I do use classes when creating a sprite, so I can create several instances of it, as well as keep functions I want to perform on the sprite all in one place. Doing this for the WHOLE program does work, and I suppose it's more "pythonic" (since python is object-oriented) but is still unnecessary for something like this. Here is a reference that teaches pygame in a similar way to how I code it, which I personally find to be an excellent tutorial.
Many people also put this code in a main() function and then run it, which is also a wildly used and accepted practice.
I have a simple Pygame program:
#!/usr/bin/env python
import pygame
from pygame.locals import *
pygame.init()
win = pygame.display.set_mode((400,400))
pygame.display.set_caption("My first game")
But every time I try to run it, I get this:
pygame 2.0.0 (SDL 2.0.12, python 3.8.3)
Hello from the pygame community. https://www.pygame.org/contribute.html
And then nothing happens.
Why I can't run this program?
Your application works well. However, you haven't implemented an application loop:
import pygame
from pygame.locals import *
pygame.init()
win = pygame.display.set_mode((400,400))
pygame.display.set_caption("My first game")
clock = pygame.time.Clock()
run = True
while run:
# handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
# update game objects
# [...]
# clear display
win.fill((0, 0, 0))
# draw game objects
# [...]
# update display
pygame.display.flip()
# limit frames per second
clock.tick(60)
pygame.quit()
The typical PyGame application loop has to:
handle the events by calling either pygame.event.pump() or pygame.event.get().
update the game states and positions of objects dependent on the input events and time (respectively frames)
clear the entire display or draw the background
draw the entire scene (blit all the objects)
update the display by calling either pygame.display.update() or pygame.display.flip()
limit frames per second to limit CPU usage with pygame.time.Clock.tick
repl.it/#Rabbid76/PyGame-MinimalApplicationLoop See also Event and application loop
This is interesting. Computer read your program line by line[python]. When all the line are interpreted, the program closed. To solve this problem you need to add a while loop to make sure the program will continue until you close the program.
import pygame,sys
from pygame.locals import *
pygame.init()
pygame.display.set_caption("My first game")
win = pygame.display.set_mode((400,400))
#game loop keeps the game running until you exit the game.
game_running=True
while game_running:
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
win.fill((0, 250, 154)) #Fill the pygame window with specific color. You can use hex or rgb color
pygame.display.update() #Refresh the pygame window
You can check more pygame Examples.
https://github.com/01one/Pygame-Examples
I think this will be helpful.
I'm making a fighting game in pygame but whenever I load a background (940x680 PNG) the program starts to lag really badly, here is my code:
#import section
import os
import pygame
import time
from pygame.locals import *
#end of import section
#initiazing pygame
pygame.init()
print "Loaded Pygame 100%"
#lengths
width, height = 940,680
#opening window
screen = pygame.display.set_mode((width, height))
#position
background = [0,0]
#loading images
back = pygame.image.load("resources/image/Back.png")
print "Loaded graphics 100%"
while 1:
#clear to reload
screen.fill(0)
#drawing background
screen.blit(back, background )
#update
pygame.display.flip()
This may be a newbie question but I just started so yeah...
In pygame you need to use this code you wrote.
#clear to reload
screen.fill(0)
#drawing background
screen.blit(back, background )
#update
pygame.display.flip()
If you add a player to the screen and then move him. You will notice that unless you redraw the background, you will see the character more than once.
Pygame doesn't just let you add an image to the screen and move it around, you must redraw the background to get rid of a previous frame.
Another thing you should add to your code is a wait.
clock = pygame.time.Clock()
FPS = 30
while 1:
clock.tick(FPS)
This will set the frame rate. I'd say you would generally want this at 30 - 60 frames, depending on the game / hardware.
Your current program is probably redrawing the frame a several hundred times, so this will definately add lag.
When I checked this on my pc Ubuntu 12.04 without a background. i.e. A Black screen.
My frame rate was between 1000 - 2000 FPS.