Python Forum
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] boilerplate
#5
bolerplate code would depend on each person and how far they are into the learning process. For example, this is mostly my boiler plate code when i start a new game (minus the movie)

https://github.com/Mekire/pygame-mutisce...with-movie

Also you should use pygame rects instead. This will help tremendously as your game gets more and more complex the more you add. As you can see it is much easier to read and understand. This is an example of your post modified to such that. 
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)
screen_rect = screen.get_rect()
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([25,45])
test_surface_rect = test_surface.get_rect(center=screen_rect.center)
test_surface.fill(WHITE)

 
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))
    screen.blit(test_surface, test_surface_rect)
    pygame.draw.aaline(screen, RED, test_surface_rect.bottomleft, test_surface_rect.topright)
 
    # flip() updates the screen to make our changes visible
    pygame.display.flip()
     
    # how many updates per second
    clock.tick(60)
 
pygame.quit()
Recommended Tutorials:
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