Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Wormy (a Nibbles clone)
- # By Al Sweigart al@inventwithpython.com
- # http://inventwithpython.com/pygame
- # Released under a "Simplified BSD" license
- #KRT 14/06/2012 modified Start Screen and Game Over screen to cope with mouse events
- #KRT 14/06/2012 Added a non-busy wait to Game Over screen to reduce processor loading from near 100%
- #
- # Modified by Grant Clarke
- # g.clarke@abertay.ac.uk
- #
- #
- import random, sys
- import pygame, math
- from pygame.locals import *
- FPS = 60
- WINDOWWIDTH = 640
- WINDOWHEIGHT = 480
- # R G B
- WHITE = (255, 255, 255)
- BLACK = ( 0, 0, 0)
- RED = (255, 0, 0)
- GREEN = ( 0, 255, 0)
- DARKGREEN = ( 0, 155, 0)
- DARKGRAY = ( 40, 40, 40)
- BGCOLOR = BLACK
- UP = 'up'
- DOWN = 'down'
- LEFT = 'left'
- RIGHT = 'right'
- #
- # GLOBAL VARIABLES used by multiple functions
- #
- # Set a random start point.
- gStartx = random.randint(0, WINDOWWIDTH)
- gStarty = random.randint(5, WINDOWHEIGHT)
- gPlayerCoords = {'x': gStartx, 'y': gStarty}
- gPlayerVelocity = {'x': 0, 'y': 0}
- gPlayerAcceleration = 0.4
- gPlayerSpaceResistanceMult = 0.98 #space resistance is like air resistance, but in space
- gPlayerAngle = 0;
- gPlayerAngleStep = 0.06; #amount to increase angle by each tick the left/right key is held, in degrees
- #
- #
- #
- def main():
- init()
- showStartScreen()
- while True:
- runGame()
- showGameOverScreen()
- def init():
- global FPSCLOCK, DISPLAYSURF, BASICFONT
- pygame.init()
- FPSCLOCK = pygame.time.Clock()
- DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
- BASICFONT = pygame.font.Font('freesansbold.ttf', 18)
- pygame.display.set_caption('Assteroids')
- def runGame():
- game_init()
- while True: # main game loop
- game_over = game_update()
- game_render()
- if game_over == True:
- return # game over, stop running the game
- def game_init():
- # Set a random start point.
- gStartx = random.randint(10, WINDOWWIDTH -5)
- gStarty = random.randint(10, WINDOWHEIGHT -5)
- gPlayerCoords = {'x': gStartx, 'y': gStarty}
- gPlayerVelocity = {'x': 0, 'y': 0}
- def game_update():
- global gPlayerCoords, gPlayerVelocity, gPlayerAcceleration, gPlayerSpaceResistanceMult, gPlayerAngle, gPlayerAngleStep
- for event in pygame.event.get(): # event handling loop
- if event.type == QUIT:
- terminate()
- elif event.type == KEYDOWN:
- if event.key == K_ESCAPE:
- terminate()
- keys = pygame.key.get_pressed()
- if keys[K_LEFT]:
- gPlayerAngle -= gPlayerAngleStep
- if keys[K_UP]:
- gPlayerVelocity['y'] += gPlayerAcceleration*math.sin(gPlayerAngle);
- gPlayerVelocity['x'] += gPlayerAcceleration*math.cos(gPlayerAngle);
- if keys[K_RIGHT]:
- gPlayerAngle += gPlayerAngleStep
- if keys[K_DOWN]:
- gPlayerVelocity['y'] -= gPlayerAcceleration*math.sin(gPlayerAngle);
- gPlayerVelocity['x'] -= gPlayerAcceleration*math.cos(gPlayerAngle);
- gPlayerVelocity['x'] *= gPlayerSpaceResistanceMult
- gPlayerVelocity['y'] *= gPlayerSpaceResistanceMult
- gPlayerCoords['x'] += gPlayerVelocity['x']
- gPlayerCoords['y'] += gPlayerVelocity['y']
- if gPlayerCoords['x'] > WINDOWWIDTH:
- gPlayerCoords['x'] -= WINDOWWIDTH
- if gPlayerCoords['x'] < 0:
- gPlayerCoords['x'] += WINDOWWIDTH
- if gPlayerCoords['y'] > WINDOWHEIGHT:
- gPlayerCoords['y'] -= WINDOWHEIGHT
- if gPlayerCoords['y'] < 0:
- gPlayerCoords['y'] += WINDOWHEIGHT
- # game is not over, return False
- return False
- def game_render():
- DISPLAYSURF.fill(BGCOLOR)
- drawPlayer(gPlayerCoords, gPlayerVelocity)
- pygame.display.update()
- FPSCLOCK.tick(FPS)
- def drawPressKeyMsg():
- pressKeySurf = BASICFONT.render('Press a key to play.', True, DARKGRAY)
- pressKeyRect = pressKeySurf.get_rect()
- pressKeyRect.topleft = (WINDOWWIDTH - 200, WINDOWHEIGHT - 30)
- DISPLAYSURF.blit(pressKeySurf, pressKeyRect)
- # KRT 14/06/2012 rewrite event detection to deal with mouse use
- def checkForKeyPress():
- for event in pygame.event.get():
- if event.type == QUIT: #event is quit
- terminate()
- elif event.type == KEYDOWN:
- if event.key == K_ESCAPE: #event is escape key
- terminate()
- else:
- return event.key #key found return with it
- # no quit or key events in queue so return None
- return None
- def showStartScreen():
- titleFont = pygame.font.Font('freesansbold.ttf', 100)
- titleSurf1 = titleFont.render('Assteroids!', True, WHITE, DARKGREEN)
- titleSurf2 = titleFont.render('Assteroids!', True, GREEN)
- degrees1 = 0
- degrees2 = 0
- #KRT 14/06/2012 rewrite event detection to deal with mouse use
- pygame.event.get() #clear out event queue
- while True:
- DISPLAYSURF.fill(BGCOLOR)
- rotatedSurf1 = pygame.transform.rotate(titleSurf1, degrees1)
- rotatedRect1 = rotatedSurf1.get_rect()
- rotatedRect1.center = (WINDOWWIDTH / 2, WINDOWHEIGHT / 2)
- DISPLAYSURF.blit(rotatedSurf1, rotatedRect1)
- rotatedSurf2 = pygame.transform.rotate(titleSurf2, degrees2)
- rotatedRect2 = rotatedSurf2.get_rect()
- rotatedRect2.center = (WINDOWWIDTH / 2, WINDOWHEIGHT / 2)
- DISPLAYSURF.blit(rotatedSurf2, rotatedRect2)
- drawPressKeyMsg()
- #KRT 14/06/2012 rewrite event detection to deal with mouse use
- if checkForKeyPress():
- return
- pygame.display.update()
- FPSCLOCK.tick(FPS)
- degrees1 += 3 # rotate by 3 degrees each frame
- degrees2 += 7 # rotate by 7 degrees each frame
- def terminate():
- pygame.quit()
- sys.exit()
- def getRandomLocation():
- return {'x': random.randint(0, CELLWIDTH - 1), 'y': random.randint(0, CELLHEIGHT - 1)}
- def showGameOverScreen():
- gameOverFont = pygame.font.Font('freesansbold.ttf', 150)
- gameSurf = gameOverFont.render('Game', True, WHITE)
- overSurf = gameOverFont.render('Over', True, WHITE)
- gameRect = gameSurf.get_rect()
- overRect = overSurf.get_rect()
- gameRect.midtop = (WINDOWWIDTH / 2, 10)
- overRect.midtop = (WINDOWWIDTH / 2, gameRect.height + 10 + 25)
- DISPLAYSURF.blit(gameSurf, gameRect)
- DISPLAYSURF.blit(overSurf, overRect)
- drawPressKeyMsg()
- pygame.display.update()
- pygame.time.wait(500)
- #KRT 14/06/2012 rewrite event detection to deal with mouse use
- pygame.event.get() #clear out event queue
- while True:
- if checkForKeyPress():
- return
- #KRT 12/06/2012 reduce processor loading in gameover screen.
- pygame.time.wait(100)
- def drawScore(score):
- scoreSurf = BASICFONT.render('Score: %s' % (score), True, WHITE)
- scoreRect = scoreSurf.get_rect()
- scoreRect.topleft = (WINDOWWIDTH - 120, 10)
- DISPLAYSURF.blit(scoreSurf, scoreRect)
- def drawPlayer(gPlayerCoords, gPlayerVelocity):
- x = gPlayerCoords['x']
- y = gPlayerCoords['y']
- Rect = pygame.Rect(x, y, 5, 5)
- pygame.draw.rect(DISPLAYSURF, DARKGREEN, Rect)
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement