/demo_dodger.py
Python | 135 lines | 100 code | 25 blank | 10 comment | 36 complexity | a8ed9f5b8928da4330bd8167a187629b MD5 | raw file
1# Pygcurse Dodger 2# By Al Sweigart al@inventwithpython.com 3 4# This program is a demo for the Pygcurse module. 5# Simplified BSD License, Copyright 2011 Al Sweigart 6 7import pygame, random, sys, time, pygcurse 8from pygame.locals import * 9 10GREEN = (0, 255, 0) 11BLACK = (0,0,0) 12WHITE = (255,255,255) 13RED = (255,0,0) 14 15WINWIDTH = 40 16WINHEIGHT = 50 17TEXTCOLOR = WHITE 18BACKGROUNDCOLOR = (0, 0, 0) 19FPS = 40 20BADDIEMINSIZE = 1 21BADDIEMAXSIZE = 5 22BADDIEMINSPEED = 4 23BADDIEMAXSPEED = 1 24ADDNEWBADDIERATE = 3 25 26win = pygcurse.PygcurseWindow(WINWIDTH, WINHEIGHT, fullscreen=False) 27pygame.display.set_caption('Pygcurse Dodger') 28win.autoupdate = False 29 30def main(): 31 showStartScreen() 32 pygame.mouse.set_visible(False) 33 mainClock = pygame.time.Clock() 34 gameOver = False 35 36 newGame = True 37 while True: 38 if gameOver and time.time() - 4 > gameOverTime: 39 newGame = True 40 if newGame: 41 newGame = False 42 pygame.mouse.set_pos(win.centerx * win.cellwidth, (win.bottom - 4) * win.cellheight) 43 mousex, mousey = pygame.mouse.get_pos() 44 cellx, celly = win.getcoordinatesatpixel(mousex, mousey) 45 baddies = [] 46 baddieAddCounter = 0 47 gameOver = False 48 score = 0 49 50 win.fill(bgcolor=BLACK) 51 52 for event in pygame.event.get(): 53 if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE): 54 terminate() 55 if event.type == MOUSEMOTION and not gameOver: 56 mousex, mousey = event.pos 57 cellx, celly = win.getcoordinatesatpixel(mousex, mousey) 58 59 # add new baddies if needed 60 if baddieAddCounter == ADDNEWBADDIERATE: 61 speed = random.randint(BADDIEMAXSPEED, BADDIEMINSPEED) 62 baddies.append({'size': random.randint(BADDIEMINSIZE, BADDIEMAXSIZE), 63 'speed': speed, 64 'x': random.randint(0, win.width), 65 'y': -BADDIEMAXSIZE, 66 'movecounter': speed}) 67 baddieAddCounter = 0 68 else: 69 baddieAddCounter += 1 70 71 72 # move baddies down, remove if needed 73 for i in range(len(baddies)-1, -1, -1): 74 if baddies[i]['movecounter'] == 0: 75 baddies[i]['y'] += 1 76 baddies[i]['movecounter'] = baddies[i]['speed'] 77 else: 78 baddies[i]['movecounter'] -= 1 79 80 if baddies[i]['y'] > win.height: 81 del baddies[i] 82 83 84 # check if hit 85 if not gameOver: 86 for baddie in baddies: 87 if cellx >= baddie['x'] and celly >= baddie['y'] and cellx < baddie['x']+baddie['size'] and celly < baddie['y']+baddie['size']: 88 gameOver = True 89 gameOverTime = time.time() 90 break 91 score += 1 92 93 # draw baddies to screen 94 for baddie in baddies: 95 win.fill('#', GREEN, BLACK, (baddie['x'], baddie['y'], baddie['size'], baddie['size'])) 96 97 if not gameOver: 98 playercolor = WHITE 99 else: 100 playercolor = RED 101 win.putchars('GAME OVER', win.centerx-4, win.centery, fgcolor=RED, bgcolor=BLACK) 102 103 win.putchar('@', cellx, celly, playercolor) 104 win.putchars('Score: %s' % (score), win.width - 14, 1, fgcolor=WHITE) 105 win.update() 106 mainClock.tick(FPS) 107 108 109def showStartScreen(): 110 while checkForKeyPress() is None: 111 win.fill(bgcolor=BLACK) 112 win.putchars('Pygcurse Dodger', win.centerx-8, win.centery, fgcolor=TEXTCOLOR) 113 if int(time.time() * 2) % 2 == 0: # flashing 114 win.putchars('Press a key to start!', win.centerx-11, win.centery+1, fgcolor=TEXTCOLOR) 115 win.update() 116 117 118def checkForKeyPress(): 119 # Go through event queue looking for a KEYUP event. 120 # Grab KEYDOWN events to remove them from the event queue. 121 for event in pygame.event.get([KEYDOWN, KEYUP]): 122 if event.type == KEYDOWN: 123 continue 124 if event.key == K_ESCAPE: 125 terminate() 126 return event.key 127 return None 128 129 130def terminate(): 131 pygame.quit() 132 sys.exit() 133 134if __name__ == '__main__': 135 main()