-
-
Save MatthewJA/7544830 to your computer and use it in GitHub Desktop.
| import sys | |
| import pygame | |
| from pygame.locals import * | |
| pygame.init() | |
| fps = 60 | |
| fpsClock = pygame.time.Clock() | |
| width, height = 640, 480 | |
| screen = pygame.display.set_mode((width, height)) | |
| # Game loop. | |
| while True: | |
| screen.fill((0, 0, 0)) | |
| for event in pygame.event.get(): | |
| if event.type == QUIT: | |
| pygame.quit() | |
| sys.exit() | |
| # Update. | |
| # Draw. | |
| pygame.display.flip() | |
| fpsClock.tick(fps) |
| # PyGame template. | |
| # Import standard modules. | |
| import sys | |
| # Import non-standard modules. | |
| import pygame | |
| from pygame.locals import * | |
| def update(dt): | |
| """ | |
| Update game. Called once per frame. | |
| dt is the amount of time passed since last frame. | |
| If you want to have constant apparent movement no matter your framerate, | |
| what you can do is something like | |
| x += v * dt | |
| and this will scale your velocity based on time. Extend as necessary.""" | |
| # Go through events that are passed to the script by the window. | |
| for event in pygame.event.get(): | |
| # We need to handle these events. Initially the only one you'll want to care | |
| # about is the QUIT event, because if you don't handle it, your game will crash | |
| # whenever someone tries to exit. | |
| if event.type == QUIT: | |
| pygame.quit() # Opposite of pygame.init | |
| sys.exit() # Not including this line crashes the script on Windows. Possibly | |
| # on other operating systems too, but I don't know for sure. | |
| # Handle other events as you wish. | |
| def draw(screen): | |
| """ | |
| Draw things to the window. Called once per frame. | |
| """ | |
| screen.fill((0, 0, 0)) # Fill the screen with black. | |
| # Redraw screen here. | |
| # Flip the display so that the things we drew actually show up. | |
| pygame.display.flip() | |
| def runPyGame(): | |
| # Initialise PyGame. | |
| pygame.init() | |
| # Set up the clock. This will tick every frame and thus maintain a relatively constant framerate. Hopefully. | |
| fps = 60.0 | |
| fpsClock = pygame.time.Clock() | |
| # Set up the window. | |
| width, height = 640, 480 | |
| screen = pygame.display.set_mode((width, height)) | |
| # screen is the surface representing the window. | |
| # PyGame surfaces can be thought of as screen sections that you can draw onto. | |
| # You can also draw surfaces onto other surfaces, rotate surfaces, and transform surfaces. | |
| # Main game loop. | |
| dt = 1/fps # dt is the time since last frame. | |
| while True: # Loop forever! | |
| update(dt) # You can update/draw here, I've just moved the code for neatness. | |
| draw(screen) | |
| dt = fpsClock.tick(fps) |
HypersDevelopment
Actually... It doesn't do anything when you use the second one. In CMD it just outputs
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
And then Kicks You back to the Command Interpreter instead of doing anything.
Thanks for making this, i just copy this whenever i need to make something with pygame
HypersDevelopment
Actually... It doesn't do anything when you use the second one. In CMD it just outputspygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.htmlAnd then Kicks You back to the Command Interpreter instead of doing anything.
You need to add this line to the bottom of it: runPyGame()
https://gist.github.com/MatthewJA/7544830#file-pygame-beginner-template-py-L60
should be dt = 1 / fps * 1000
reference for futureself
https://github.com/robemapes/pg-template/blob/main/main.py
import sys
import pygame as pg
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
WIDTH, HEIGHT = 640, 480
FPS = 60
pg.init()
fps_clock = pg.time.Clock()
screen = pg.display.set_mode((WIDTH, HEIGHT))
while True:
screen.fill(BLACK)
for event in pg.event.get():
if event.type == pg.QUIT:
pg.quit()
sys.exit()
pg.display.flip()
fps_clock.tick(FPS)
PLEASE don't limit the fps to 60 as you do in fpsClock.tick(fps)! It's very annoying for anyone with a high refresh rate screen. Instead just omit fps and use fpsClock.tick()
Thankyou! By the way, the 2nd one doesn't show the screen as of 3.6