Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

pygame template

# 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)
Comment

pygame template

import pygame

pygame.init()

screen = pygame.display.set_mode((1280,720))

clock = pygame.time.Clock()

while True:
    # Process player inputs.
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            raise SystemExit

    # Do logical updates here.
    # ...

    screen.fill("purple")  # Fill the display with a solid color

    # Render the graphics here.
    # ...

    pygame.display.flip()  # Refresh on-screen display
    clock.tick(60)         # wait until next frame (at 60 FPS)
Comment

PREVIOUS NEXT
Code Example
Python :: Function in python with input method 
Python :: python dlib 
Python :: aiohttps 
Python :: python cursor placement 
Python :: newsapi 
Python :: change xlabel python 
Python :: python docker 
Python :: python in kali linux 
Python :: stemmer nltk 
Python :: cascaed models in django 
Python :: importing logistic regression 
Python :: python tkinter treeview column width auto 
Python :: django query filter greater than or equal to 
Python :: pearsons correlation calculation 
Python :: tkinter convert Entry to string 
Python :: Send Fetch Request Django(Get Method) 
Python :: get table wikipedia 
Python :: model.predict Decision Tree Model 
Python :: dict comprehensions 
Python :: pandas replace multiple values in column 
Python :: chat application in python 
Python :: Write a Python program to remove a key from a dictionary. 
Python :: update python 2 to 3 
Python :: How to Get the length of all items in a list of lists in Python 
Python :: Generate bar plot python 
Python :: how to do input python 
Python :: set time complexity python 
Python :: python search a string in another string get last result 
Python :: python request body json 
Python :: expand pandas dataframe into separate rows 
ADD CONTENT
Topic
Content
Source link
Name
9+6 =