import pygame, random, sys, time from pygame.locals import * windowWidth = 600 windowHeight = 600 FPS = 6000 Green = (0, 255, 0) Blue = (0, 0, 255) Red = (255, 0, 0) Grey = (100, 100, 100) Black = (0, 0, 0) White = (255, 255, 255) moveLeft,moveRight=0,0 moveUp,moveDown=0,0 pygame.init() Clock = pygame.time.Clock() windowSurface = pygame.display.set_mode((windowWidth,windowHeight)) pygame.display.set_caption('Rect Race') pygame.mouse.set_visible(True) def terminate(): pygame.quit() sys.exit() def waitForPlayerToPressKey(): while True: for event in pygame.event.get(): if event.type == QUIT: terminate() if event.type == KEYDOWN: if event.key == K_ESCAPE: terminate() if event.key == K_RETURN: return if event.key == ord ('e'): return class Sprite: def __init__(self,x,y): #demensions self.x=x self.y=y self.width=50 self.height=50 #images self.i0 = pygame.image.load('sprite1.png') self.i1 = pygame.image.load('sprite2.png') #frame update self.timeTarget=10 self.timeNum=0 self.currentImage=0 def update(self): self.timeNum+=1 if self.timeNum==self.timeTarget: if self.currentImage==0: self.currentImage=1 else: self.currentImage=0 self.timeNum=0 self.render() def render(self): if (self.currentImage==0): windowSurface.blit(self.i0, (self.x, self.y)) else: windowSurface.blit(self.i1, (self.x, self.y)) def playerHasHitSquare(playerRect, Square): for s in square: if Player.coliderect(s['rect']): return True return False def drawTextGrey(text, font, surface, x, y): textobj = font.render(text, 1, Grey) textrect = textobj.get_rect() textrect.topleft = (x, y) surface.blit(textobj, textrect) def drawTextRed(text, font, surface, x, y): textobj = font.render(text, 1, Red) textrect = textobj.get_rect() textrect.topleft = (x, y) surface.blit(textobj, textrect) def drawTextBlue(text, font, surface, x, y): textobj = font.render(text, 1, Blue) textrect = textobj.get_rect() textrect.topleft = (x, y) surface.blit(textobj, textrect) def drawTextGreen(text, font, surface, x, y): textobj = font.render(text, 1, Green) textrect = textobj.get_rect() textrect.topleft = (x, y) surface.blit(textobj, textrect) def drawTextBlack(text, font, surface, x, y): textobj = font.render(text, 1, Black) textrect = textobj.get_rect() textrect.topleft = (x, y) surface.blit(textobj, textrect) Player = Sprite (100,150) font = pygame.font.SysFont(None, 48) while True: # the game loop runs while the game part is playing for event in pygame.event.get(): if event.type == QUIT: terminate() if event.type == KEYDOWN: if event.key == K_LEFT or event.key == ord('a'): moveRight = False moveLeft = True if event.key == K_RIGHT or event.key == ord('d'): moveLeft = False moveRight = True if event.key == K_UP or event.key == ord('w'): moveUp = True moveDown = False if event.key == K_DOWN or event.key == ord('s'): moveUp = False moveDown = True if event.type == KEYUP: if event.key == K_ESCAPE: terminate() if event.key == K_LEFT or event.key == ord('a'): moveLeft = False if event.key == K_RIGHT or event.key == ord('d'): moveRight = False if event.key == K_DOWN or event.key == ord('s'): moveDown = False if event.key == K_UP or event.key == ord('w'): moveUp = False windowSurface.fill(White) # Move the player around. if moveLeft and Player.x > 0: Player.x += -5 if moveRight and Player.x < 500: Player.x += 5 if moveUp and Player.y > 0: Player.y += -5 if moveDown and Player.y < windowHeight: Player.y += 5 Player.update() Clock.tick(60) pygame.display.flip()