#!/usr/bin/env python # -*- coding: utf-8 -*- ''' A pygame template that has the most common setup items Does not break into a main( ) and uses global data for simplicity This template is intended to be simple and easy to follow, not optimized by any means My pygame_helpers.py is recommended as a companion, as the Sprites used in the drawing are of types defined in this file, otherwise comment out/remove these examples Written by Alex Kuhl http://www.alexkuhl.org/teaching/pygame Licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License. Must keep this entire notice in both this file and derivative works ''' import pygame, random, sys from pygame.locals import * from pg_helpers import * # initialize pygame pygame.init( ) # setup the screen scr_size = ( 800, 600 ) # first is width, second height screen = pygame.display.set_mode( scr_size ) pygame.display.set_caption( "Game Title" ) # load the background # solid colors background = pygame.Surface( scr_size ) background.fill( ( 32, 35, 85 ) ) # RGB colors # for an image... # the load_image arguments are filname, whether or not the image uses alpha, and whether uses a colorkey (we won't use colorkey) # background = load_image( filename, True, False )[ 0 ] # background = pygame.transform.scale( background, scr_size ) # draw background and update display so background shows up immediately # because our drawing during game time is sprite-dependent screen.blit( background, ( 0,0 ) ) pygame.display.update( ) # set mouse cursor visibility based on your game design pygame.mouse.set_visible( True ) # seed random number generator random.seed( ) # set up the clock for timing clock = pygame.time.Clock( ) # global data angle = 0 text = TextSprite( "Hello World!", 22, [ scr_size[ 0 ]/6, scr_size[ 1 ]/6 ], ( 0, 255, 0 ) ) cursor = AnimationSprite( "hulk.gif", [ scr_size[ 0 ]/2, scr_size[ 1 ]/2 ] ) snake = ImageSprite( "pysnake.png", [400,300] ) block = RectangleSprite( 25, 35, [ 3*scr_size[ 0 ]/5, 3*scr_size[ 1 ]/5 ], ( 255, 0, 0 ) ) dot = CircleSprite( 20, [ 4*scr_size[ 0 ]/5, 4*scr_size[ 1 ]/5 ], ( 40, 100, 220 ) ) textbox = TextBoxSprite( "Yea!\nWe can use\nnewlines!", 22, [ 100, 100 ], [ 10, 10 ], ( 100, 50, 234 ) ) dotcollide = False dot.set_movement( 200, math.radians( -45 ) ) # set_movement takes radians cursor.scale( ( 100, 100 ) ) # Create groups that could be used for various things here, like collisions # One for drawing is below, add objects to it that need to be drawn each update # Z order (which sprite is drawn on top of the other) is preserved for # OrderedUpdates. The first object added will has the least precedence, so last # object added is drawn above all others drawgrp = pygame.sprite.OrderedUpdates( ) drawgrp.add( text ) drawgrp.add( block ) drawgrp.add( dot ) drawgrp.add( textbox ) drawgrp.add( snake ) drawgrp.add( cursor ) # the game loop -- go! while True: # check if the user has quit the program and if so exit for event in pygame.event.get( ): if event.type == QUIT: sys.exit( ) # adjust the mouse cursor cursor.set_cposition( pygame.mouse.get_pos( ) ) # get ticks since last call time_passed = clock.tick( 60 ) # tick takes optional argument that is an int for max frames per second # update any animated sprites cursor.update_frame( time_passed ) # collision handling and game updates would go here dot.move( time_passed ) # see if the dot has hit the edge of the screen # dot has get_position and get_cposition but get_rect is more appropriate in this case # because we need to see if the appropriate sprite *edge* has touched the edge of the screen, # not just it's upper left or center position, to do appropriate bouncing if dot.get_right( ) > scr_size[ 0 ]: dot.set_velocity_dirX( -dot.get_velocity_dirX( ) ) dot.move( time_passed/2 ) elif dot.get_left( ) < 0: dot.set_velocity_dirX( -dot.get_velocity_dirX( ) ) dot.move( time_passed/2 ) elif dot.get_top( ) < 0: dot.set_velocity_dirY( -dot.get_velocity_dirY( ) ) dot.move( time_passed/2 ) elif dot.get_bottom( ) > scr_size[ 1 ]: dot.set_velocity_dirY( -dot.get_velocity_dirY( ) ) dot.move( time_passed/2 ) # spin items based on the current angle text.rotate_deg( 2 ) snake.rotate_deg( -3 ) # test for a collision with dot # spritescollide_pp is used for sprite-to-sprite collisions # for group collisions use spritecollide_pp or spritecollideany_pp # for group to group collisions use groupcollide_pp if spritescollide_pp( cursor, dot ): # if we aren't already colliding change text if dotcollide != True: text.set_text( "Wham!" ) dotcollide = True # if no collision and we were colliding prior, change text back elif dotcollide == True: text.set_text( "Hello World!" ) dotcollide = False # draw! # clear the screen area that the last drawgrp.draw wrote to # and fill in with background drawgrp.clear( screen, background ) # get a list of the rectangles that are to be drawn this round rectlist = drawgrp.draw( screen ) # update only the rectangles from above on the display pygame.display.update( rectlist ) # end while (game loop)