def renderScreen(): global posX,posY,boardArr, boardArrHeight, DISPLAYSURF PINK = (255,0,128) PURPLE = (147,112,219) BLUE = (135,216,250) WHITE = (255,255,255) color = WHITE DISPLAYSURF.fill((255,255,255)) for i in xrange(0,boardArrHeight): for j in xrange(0,10): tileType = boardArr[i][j] #print "tile type is ", tileType if tileType == 1 or tileType == "1": print "PINK" color = PINK elif tileType == 2: print "PURPLE" color = PURPLE elif tileType == 3: print "BLUE" color = BLUE elif tileType == 4 or tileType == 5: color = (0,0,0) else: color = WHITE startPos = 300-posY*50 if startPos+i*50 > 1000: break pygame.draw.rect(DISPLAYSURF,color,pygame.Rect(j*50,startPos+i*50,50,50),0) pygame.draw.rect(DISPLAYSURF,WHITE,pygame.Rect(j*50,startPos+i*50,50,50),5) #print "<3<3" renderScoreAndStomach() renderCharacter() pygame.display.flip()
def draw_edge(self): """Draw edge around the arena.""" self.edges = [] for rect in [ (0,0,self.x,5), (0,self.y-5,self.x,5), (0,0,5,self.y), (self.x-5,0,5,self.y-5) ]: edge_rect = pygame.Rect(rect) self.edges.append(pygame.draw.rect(self.screen, (43,50,58), edge_rect, 0)) self.update_rect.append(edge_rect)
def __init__(self, matrix, x, y, identity, speed, mode): self.matrix = matrix self.identity = identity self.mode = mode self.x, self.y = x, y self.speed = speed self.DIR = { 'U':(0,-1), 'D':(0,1), 'L':(-1,0), 'R':(1,0) } self.DEG = { 'U':0, 'D':180, 'L':90, 'R':-90 } if self.identity == 'Serpent1': self.direction = 'R' else: self.direction = 'L' self.new_direction = None self.step, self.growing, self.rate = 0, 0, 0 self.segments = pygame.sprite.RenderUpdates() self.segment_spares = [] self.serpent_body = {} self.grow(self.x, self.y, self.DIR[self.direction]) self.serpent_body[0].image = self.serpent_body[0].images[self.identity+'_head'][self.direction] self.scan_rect = pygame.sprite.Sprite() self.scan_rect.rect = pygame.Rect(0,0,10,10) self.scan_detect = False self.pause = 20 self.active = True self.alive = True
def bounded_cam_func(self, player_rect): left, top, pw, ph = player_rect _, _, w, h = self.camframe l = -left + w // 2 - pw // 2 l = min(l, 0) l = max(l, w - WIDTH * TILES) t = -top + h // 2 - ph // 2 t = min(t, 0) t = max(t, h - HEIGHT * TILES) return pygame.Rect(l, t, w, h)
def draw_statusbar(self, display): pygame.draw.rect(display, pygame.Color(191, 87, 0), pygame.Rect(0, 0, SCREEN_WIDTH, TEXT_HEIGHT + 5)) surface = self.font.render( "Time: " + str("%.1f" % self.player.t) + " seconds | " + "θ: " + "%.1f" % (self.player.rotation * 180 / math.pi) + "° | " + "Vx: " + "%.1f" % self.player.vx + "c | " + "Vy: " + "%.1f" % self.player.vy + "c | " + "γ: " + ("%.3f" % self.player.get_gamma()), True, (144, 238, 144)) self.display.blit(surface, (0, 0))
def init_display(self): # percent of width that middle rect should fill c_scale = .4 # percent of width that remaining two rects should fill r_scale = 1 - c_scale * 2 self.rects['left'] = pygame.Rect(0, self.pad, self.rect.width * c_scale - self.pad, self.rect.height - self.pad * 2) self.rects['middle'] = pygame.Rect( self.rect.width * c_scale + self.pad, self.pad, self.rect.width * r_scale - self.pad * 2, self.rect.height - self.pad * 2) self.rects['right'] = pygame.Rect( self.rect.width - self.rect.width * c_scale + self.pad, self.pad, self.rect.width * c_scale - self.pad, self.rect.height - self.pad * 2) self.blits['score_title'] = (OSD_FONT.render("Score:", True, (255, 255, 255)), (self.rects['left'].x + self.text_pad, self.rects['left'].y + self.text_pad / 2, self.rects['left'].width, self.rects['left'].height)) self.blits['score'] = (OSD_FONT.render("0", True, (255, 255, 255)), (self.rects['left'].x + self.text_pad, self.rects['left'].y + self.rects['left'].height / 2, self.rects['left'].width, self.rects['left'].height)) self.blits['level_title'] = (OSD_FONT.render( "Level:", True, (255, 255, 255)), (self.rects['right'].x + self.text_pad, self.rects['right'].y + self.text_pad / 2, self.rects['right'].width, self.rects['right'].height)) self.blits['level'] = (OSD_FONT.render("0", True, (255, 255, 255)), (self.rects['right'].x + self.text_pad, self.rects['right'].y + self.rects['right'].height / 2, self.rects['right'].width, self.rects['right'].height))
def attach_buttons(): global buttons buttons = [] buttons.append( Button( screen, pygame.Rect(SCREEN_WIDTH - BUTTON_SIZE + 1, BUTTON_SIZE, BUTTON_SIZE, BUTTON_SIZE), 5, 'pause', [GameState_PAUSED, GameState_PLAYING], pause_toggle)) buttons.append( Button( screen, pygame.Rect(SCREEN_WIDTH - BUTTON_SIZE + 1, 0, BUTTON_SIZE, BUTTON_SIZE), 5, 'restart', [GameState_PAUSED, GameState_GAMEOVER, GameState_PLAYING], restart, 0)) buttons.append( Button( screen, pygame.Rect(SCREEN_WIDTH - BUTTON_SIZE + 1, SCREEN_HEIGHT - BUTTON_SIZE * 5, BUTTON_SIZE, BUTTON_SIZE), 5, 'left', [GameState_PLAYING], getattr(grid, 'move_block_left'))) buttons.append( Button( screen, pygame.Rect(SCREEN_WIDTH - BUTTON_SIZE + 1, SCREEN_HEIGHT - BUTTON_SIZE * 4, BUTTON_SIZE, BUTTON_SIZE), 5, 'right', [GameState_PLAYING], getattr(grid, 'move_block_right'))) buttons.append( Button( screen, pygame.Rect(SCREEN_WIDTH - BUTTON_SIZE + 1, SCREEN_HEIGHT - BUTTON_SIZE * 3, BUTTON_SIZE, BUTTON_SIZE), 5, 'rotate', [GameState_PLAYING], getattr(grid, 'rotate_block'))) buttons.append( Button( screen, pygame.Rect(SCREEN_WIDTH - BUTTON_SIZE + 1, SCREEN_HEIGHT - BUTTON_SIZE * 2, BUTTON_SIZE, BUTTON_SIZE), 5, 'down', [GameState_PLAYING], getattr(grid, 'move_block_down'))) buttons.append( Button( screen, pygame.Rect(SCREEN_WIDTH - BUTTON_SIZE + 1, SCREEN_HEIGHT - BUTTON_SIZE, BUTTON_SIZE, BUTTON_SIZE), 5, 'bottom', [GameState_PLAYING], getattr(grid, 'move_block_to_bottom')))
def draw_rect(self, x, y, color): """ fills specific coordinate with color """ box = pygame.Rect((y * UNIT_SIZE, x * UNIT_SIZE, UNIT_SIZE, UNIT_SIZE)) pygame.draw.rect(self.screen, color, box, 0) self.update_rect.append(box)
def run(): # Pull in global variables so that run function remains paramaterless global DEBUG global game_state global text_display_counter global down_held global grid global osd deltaTime = clock.tick(FPS) # main event loop (mostly for keyboard) for event in pygame.event.get(): # Debug functions if DEBUG > 0: # Debug level incrementer / decrementer - numpad + and - if event.type == pygame.KEYDOWN and event.key == pygame.K_PAGEUP: DEBUG = min(DEBUG + 1, 10) print("Debug level = {}".format(DEBUG)) if event.type == pygame.KEYDOWN and event.key == pygame.K_PAGEDOWN: DEBUG = max(1, DEBUG - 1) print("Debug level = {}".format(DEBUG)) # Respawn a new block if event.type == pygame.KEYDOWN and event.key == pygame.K_1: grid._create_block() # Respawn the grid with custom array if event.type == pygame.KEYDOWN and event.key == pygame.K_2: grid.grid = [[0 for x in range(BLOCK_WIDTH)] for y in range(BLOCK_HEIGHT)] # Print grid array to console if event.type == pygame.KEYDOWN and event.key == pygame.K_3: grid.print_grid() # Print block array to console if event.type == pygame.KEYDOWN and event.key == pygame.K_4: grid.current_block.print_block() # Increase level if event.type == pygame.KEYDOWN and event.key == pygame.K_5: grid.level = min(grid.level + 1, 100) grid._set_tick() print("Level = {}".format(grid.level)) # Decrease level if event.type == pygame.KEYDOWN and event.key == pygame.K_6: grid.level = max(0, grid.level + 1) grid._set_tick() print("Level = {}".format(grid.level)) # Increase lines if event.type == pygame.KEYDOWN and event.key == pygame.K_7: grid.lines += 1 grid._check_levelup() print("Lines = {}".format(grid.lines)) print("Tick = {}".format(grid.tick)) # reset down_held so that we recheck down_held = False if event.type == pygame.QUIT: pygame.quit() exit() # restart game if event.type == pygame.KEYUP and event.key == pygame.K_r: restart() if game_state == GameState_PLAYING or game_state == GameState_PAUSED: if event.type == pygame.KEYUP and event.key == pygame.K_p: pause_toggle() if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1: for button in buttons: button.start_click_event(game_state, *pygame.mouse.get_pos()) if game_state == GameState_PLAYING: # rotate block +90deg if event.type == pygame.KEYDOWN and event.key == pygame.K_x: grid.rotate_block() # rotate block +270deg (-90deg) if event.type == pygame.KEYDOWN and event.key == pygame.K_z: grid.rotate_block(True) # move block as far down as it will go if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE: grid.move_block_to_bottom() # move block right if event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT: grid.move_block_right() # move block left if event.type == pygame.KEYDOWN and event.key == pygame.K_LEFT: grid.move_block_left() # track if the down key is being held if event.type == pygame.KEYDOWN and event.key == pygame.K_DOWN: down_held = True if game_state == GameState_PLAYING: # perform additional updates to block if we want to go down faster if down_held: grid.update_block( DEBUG, deltaTime * max(.5, SPEED_MULTIPLIER - grid.level)) # transition to game over state if grid flags a game over if grid.state == grid.GridState_GAMEOVER: game_state = GameState_GAMEOVER # update grid and osd grid.update(DEBUG, deltaTime) osd.update_values(grid.score, grid.level, grid.next_block) # display pause / restart state blinking text elif game_state == GameState_PAUSED or game_state == GameState_GAMEOVER: renders = [] # blink text color on and off every 600 ms text_display_counter += deltaTime if text_display_counter >= 600: if text_display_counter >= 1200: text_display_counter = 0 text_color = PAUSE_COLOR else: text_color = (255, 255, 255) # generate appropriate text elements for state if game_state == GameState_PAUSED: text = ["Pause", "Press P to play"] elif game_state == GameState_GAMEOVER: text = ["Game Over", "Press R to restart"] # render each text item for i in range(len(text)): if i == 0: renders.append(PAUSED_FONT.render(text[i], True, text_color)) else: renders.append( PAUSED_SUB_FONT.render(text[i], True, text_color)) # get max width of rect max_width = max(r.get_width() for r in renders) # get sum of all renders heights for a max height, round up to the nearest BLOCK_SIZE max_height = -(-sum(r.get_height() for r in renders) // BLOCK_SIZE) * BLOCK_SIZE # calculate a centered rectangle based on max_width and max_height render_rect = pygame.Rect( ((SCREEN_WIDTH - OSD_WIDTH) / 2 - max_width / 2) - max_width / 40, 8 * BLOCK_SIZE + OSD_HEIGHT, max_width + max_width / 20, max_height) # draw rect and outline pygame.draw.rect(screen, PAUSE_COLOR, render_rect) pygame.draw.rect(screen, GRID_COLOR, render_rect, 2) # blit each text item to an appropriate offset in the center of grid for i in range(len(renders)): # center and space the renders out in render_rect evenly pos = ((SCREEN_WIDTH - OSD_WIDTH) / 2 - renders[i].get_width() / 2, 8 * BLOCK_SIZE + OSD_HEIGHT + render_rect.height / len(renders) * i) screen.blit(renders[i], pos) # always draw osd and buttons osd.draw(DEBUG) for button in buttons: button.draw(DEBUG, game_state, deltaTime) pygame.display.flip()
else: import pygame # setup pygame instance pygame.display.init() screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption(CAPTION) clock = pygame.time.Clock() # js compiler does not like enums, setting varaibles instead for state tracking. GameState_PAUSED = 0 GameState_GAMEOVER = 1 GameState_PLAYING = 2 grid = None osd = OSD(screen, pygame.Rect(0, 0, SCREEN_WIDTH - OSD_WIDTH, OSD_HEIGHT), 2, 3, 6) buttons = [] # functionised pause for button calls def pause_toggle(): global game_state global text_display_counter text_display_counter = 0 game_state = GameState_PLAYING if game_state != GameState_PLAYING else GameState_PAUSED # functionised restart for button calls def restart(): global grid
def __init__(self): self.camframe = pygame.Rect(0, 0, VIEWPORT_SIZE[0], VIEWPORT_SIZE[1])