import traceback import random import pygame from pygame.locals import * pygame.display.init() pygame.font.init() sizes = { "screen" : ( 300, 480 ) } colors = { "font" : ( 138, 69, 252 ), "block" : ( 200, 200, 200 ), "block_border" : ( 100, 100, 100 ), "border" : ( 52, 135, 184 ), "background" : ( 255, 255, 255 ) } fonts = { "18" : pygame.font.SysFont( "Times New Roman", 18, bold = True ), "24" : pygame.font.SysFont( "Times New Roman", 24, bold = True ) } screen = pygame.display.set_mode( sizes["screen"], 0, 32 ) clock = pygame.time.Clock() LOW = 1 MIDDLE = 2 HIGH = 3 class Block( object ): def __init__( self, game, col ): self.x = game.XS[col] + game.BORDER_WIDTH / 2 + 1 self.y = -200 self.height = game.BLOCK_HEIGHT self.width = game.BLOCK_WIDTH self.speed = game.SPEED self.column = col def draw( self ): rect = ( self.x, self.y, self.width, self.height ) pygame.draw.rect( screen, colors["block"], rect ) pygame.draw.rect( screen, colors["block_border"], rect, 0 ) def move( self ): self.y += self.speed * 1 if self.y + self.height >= sizes["screen"][1]: return False return True class Game( object ): BORDER_WIDTH = 10 def __init__( self, level ): if level == LOW: Game.COLUMN_NUM = 4 Game.BLOCK_HEIGHT = 100 Game.SPEED = 1 elif level == MIDDLE: Game.COLUMN_NUM = 5 Game.BLOCK_HEIGHT = 80 Game.SPEED = 2 elif level == HIGH: Game.COLUMN_NUM = 6 Game.BLOCK_HEIGHT = 60 Game.SPEED = 3 Game.BLOCK_WIDTH, Game.XS = Game.calculate( sizes["screen"][0], Game.COLUMN_NUM, Game.BORDER_WIDTH ) self.blocks = [] def move( self ): for block in self.blocks: if not block.move(): return False return True def create( self ): self.can_product = [ True for i in range( Game.COLUMN_NUM ) ] added = [] for col in xrange( Game.COLUMN_NUM ): if random.random() < 0.01: added.append( Block( self, col ) ) for block in self.blocks: if block.y < 0: self.can_product[block.column] = False added = filter( lambda x : self.can_product[x.column], added ) self.blocks.extend( added ) def draw( self ): screen.fill( colors["background"] ) for x in Game.XS: pygame.draw.line( screen, colors["border"], ( x, 0 ), ( x, sizes["screen"][1] ), Game.BORDER_WIDTH ) for block in self.blocks: block.draw() pygame.display.update() def cin( self ): for e in pygame.event.get(): if e.type == KEYDOWN: if e.key == K_ESCAPE: return False elif e.type == MOUSEBUTTONDOWN: if e.button == 1: mouse_position = pygame.mouse.get_pos() for block in self.blocks: if block.x <= mouse_position[0] <= block.x + block.width: if block.y <= mouse_position[1] <= block.y + block.height: self.blocks.remove( block ) break return True def run( self ): while True: if not self.cin(): return False if not self.move(): return False self.draw() self.create() clock.tick( 100 ) @staticmethod def calculate( screen_width, column_num, border_width ): xs = [] begin_x = border_width / 2 - 1 column_width = float( screen_width - ( 1 + column_num ) * border_width ) / column_num for i in range( column_num + 1 ): xs.append( begin_x + ( column_width + border_width ) * i ) return column_width + 1, xs blink = -30 def main( level ): def menu_draw(): global blink screen.fill( colors["background"] ) screen.blit( fonts["24"].render( "A Easy Game", True, colors["font"] ), ( 65, 150 ) ) if blink < 0: screen.blit( fonts["18"].render( "Press Any Key", True, colors["font"] ), ( 80, 200 ) ) blink += 1 if blink == 30: blink = -30 pygame.display.update() def menu_cin(): for e in pygame.event.get(): mouse_pos = pygame.mouse.get_pos() if e.type == KEYDOWN: if e.key == K_ESCAPE: return False game = Game( level ) score = game.run() if score == False: return False return True pygame.event.set_grab( True ) while True: if not menu_cin(): break menu_draw() clock.tick( 60 ) pygame.event.set_grab( False ) pygame.quit() if __name__ == '__main__': try: level = LOW main( level ) except: traceback.print_exc() pygame.quit() input()
原文地址:http://blog.csdn.net/pandora_madara/article/details/39158673