#MonthOfCode day 30 - infinity
# thema= INFINITY, tested with python3.1 + pygame1.9.1 | |
import pygame | |
from time import sleep | |
import random | |
from math import cos,sin,pi | |
# constants | |
DISP_WIDTH = 800 | |
DISP_HEIGHT = 600 | |
MAX_FPS = 65 | |
N_PIX = 8 | |
BG_COLOR_DESC = 'lightblue' | |
# let's define two classes: Player, and Terrain | |
class Player: | |
'''models a moving player representation that can move left or right''' | |
BASECOLOR = pygame.Color('darkred') | |
SIZE = 16 | |
def __init__(self, x_init, y_init): | |
self.vx, self.vy = 0.,0. | |
self.x,self.y = float(x_init), float(y_init) | |
self.color= Player.BASECOLOR | |
self.moving_left = self.moving_right = False | |
def push_right(self): | |
self.moving_right = True | |
def push_left(self): | |
self.moving_left = True | |
def freeze(self): | |
if( self.moving_right): | |
self.moving_right= False | |
if( self.moving_left): | |
self.moving_left= False | |
terrain.stopScroll() | |
def update( self ): | |
if not (self.moving_right or self.moving_left): | |
self.vx = 0. | |
else: | |
if self.moving_right: | |
self.vx = float(N_PIX) | |
if self.moving_left: | |
self.vx = -float(N_PIX) | |
#scrolling | |
if( self.x+self.vx> (2./3)*DISP_WIDTH ): | |
if( not terrain.isScrolling()): | |
terrain.startScrollLeft() # scroll in the inverse direction of player mov. to make it happen | |
self.stuck = True | |
elif( self.x+self.vx< (1./3)*DISP_WIDTH ): | |
if( not terrain.isScrolling()): | |
terrain.startScrollRight() | |
self.stuck = True | |
else: | |
self.stuck = False | |
if(not self.stuck): | |
self.x += self.vx | |
self.y = int( DISP_HEIGHT - terrain.l_heights[ int(self.x) ] - Player.SIZE) | |
def draw(self, surface): | |
pygame.draw.circle( surface, self.color, (int(self.x),int(self.y)), Player.SIZE) | |
class Terrain: | |
def __init__(self): | |
self.dist = 0 | |
# the magic (terrain-gen) is done by this function | |
self.height_func = lambda x: 228 + 128*cos(0.0002*x) + 32*sin(0.01*x) + 64*sin(pi/800*x) + 16*abs(sin(0.02 *x + (pi/2))) | |
self.l_heights = list() | |
for i in range(DISP_WIDTH ): | |
self.l_heights.append( self.height_func(i) ) | |
self.color = pygame.Color('darkgreen') | |
self.moving_right = self.moving_left = False | |
def isScrolling( self): | |
return ( self.moving_right or self.moving_left ) | |
def startScrollRight(self): | |
self.moving_right = True | |
def startScrollLeft(self): | |
self.moving_left = True | |
def stopScroll(self): | |
self.moving_left = self.moving_right = False | |
def update(self): | |
#refresh terrain heights | |
if (self.moving_right): | |
for i in range(N_PIX): | |
self.dist -= 1 | |
self.l_heights.pop() | |
self.l_heights.insert(0, self.height_func( self.dist ) ) | |
elif (self.moving_left): | |
for i in range(N_PIX): | |
self.dist += 1 | |
self.l_heights.pop(0) | |
self.l_heights.append(self.height_func( self.dist+DISP_WIDTH-1 ) ) | |
def draw(self, surface): | |
for ind in range(len(self.l_heights) ): | |
h_val = self.l_heights[ ind] | |
p1 = (ind, DISP_HEIGHT-1) | |
p2 = (ind, DISP_HEIGHT-1-h_val) | |
pygame.draw.line(surface, self.color, p1, p2) | |
# source-code body | |
# global vars & init. the game | |
pygame.init() | |
window = pygame.display.set_mode( (DISP_WIDTH, DISP_HEIGHT) ) | |
pl_obj = Player( DISP_WIDTH/2, DISP_HEIGHT/2 ) | |
terrain = Terrain() | |
game_over = False | |
clock = pygame.time.Clock() | |
bg_color = pygame.Color(BG_COLOR_DESC) | |
# - game loop | |
while not game_over: | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: | |
game_over = True | |
continue | |
if event.type is pygame.KEYDOWN: | |
if( pygame.key.get_pressed()[ pygame.K_RIGHT ] ): | |
pl_obj.push_right() | |
if( pygame.key.get_pressed()[ pygame.K_LEFT ] ): | |
pl_obj.push_left() | |
if event.type is pygame.KEYUP: | |
if( not pygame.key.get_pressed()[ pygame.K_RIGHT ] ): | |
pl_obj.freeze() | |
if( not pygame.key.get_pressed()[ pygame.K_LEFT ] ): | |
pl_obj.freeze() | |
# logic | |
pl_obj.update() | |
terrain.update() | |
# rendering | |
window.fill(bg_color) | |
terrain.draw(window) | |
pl_obj.draw(window) | |
pygame.display.flip() | |
clock.tick(MAX_FPS) | |
pygame.quit() | |
print('done.') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment