/demo_dodger.py

http://github.com/asweigart/pygcurse · Python · 135 lines · 100 code · 25 blank · 10 comment · 31 complexity · a8ed9f5b8928da4330bd8167a187629b MD5 · raw file

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