Python Forum
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] boilerplate
#1
Boilerplate is the basic code you need in order for something to work.  The bare minimum, with a little added on top to help you understand how to use it.  Anytime you start a new script, boilerplate code is what you start with, and you make your modifications based upon that.

Here's my version of pygame boilerplate.  There's some setup and teardown, and also a basic surface is added to demonstrate how drawing works, and how to blit things to the screen.

import pygame

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)

# initialize pygame
pygame.init()
screen_size = (700, 500)

# create a window
screen = pygame.display.set_mode(screen_size)
pygame.display.set_caption("pygame Test")

# clock is used to set a max fps
clock = pygame.time.Clock()

# create a demo surface, and draw a red line diagonally across it
surface_size = (25, 45)
test_surface = pygame.Surface(surface_size)
test_surface.fill(WHITE)
pygame.draw.aaline(test_surface, RED, (0, surface_size[1]), (surface_size[0], 0))

running = True
while running:
	for event in pygame.event.get():
		if event.type == pygame.QUIT:
			running = False
	
	#clear the screen
	screen.fill(BLACK)
	
	# draw to the screen
	# YOUR CODE HERE
	x = (screen_size[0]/2) - (surface_size[0]/2)
	y = (screen_size[1]/2) - (surface_size[1]/2)
	screen.blit(test_surface, (x, y))

	# flip() updates the screen to make our changes visible
	pygame.display.flip()
	
	# how many updates per second
	clock.tick(60)

pygame.quit()
If you run it, you'll see this (click for full size):     
Reply


Messages In This Thread
boilerplate - by nilamo - Sep-19-2016, 09:12 PM
RE: pygame boilerplate - by pydsigner - Sep-20-2016, 05:44 AM
RE: pygame boilerplate - by nilamo - Sep-20-2016, 02:35 PM
RE: pygame boilerplate - by Windspar - Oct-03-2016, 12:14 PM
RE: boilerplate - by metulburr - Oct-15-2016, 01:21 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020