def main(): #初始化pygame。启用Pygame必不可少的一步,在程序开始阶段执行。 pygame.init() #创建屏幕对象 screen = pygame.display.set_mode((1200, 900)) #分辨率是1200*900 pygame.display.set_caption("俄罗斯方块") #窗口标题 pygame.key.set_repeat(100, 100) # 一直按下某个键,每过100毫秒就引发一个KEYDOWN事件 #屏幕背景色 bg_color = (230, 230, 230) game_state = GameState(screen) game_resource = GameResource() game_resource.play_bg_music() #游戏主循环 while True: #方块触底的话 if game_state.piece and game_state.piece.is_on_bottom: game_state.touch_bottom() #监视键盘和鼠标事件 check_events(game_state, game_resource) #设定屏幕背景 screen.blit(game_resource.load_bg_img(), (0, 0)) #绘制方块 if game_state.piece: game_state.piece.paint() #绘制游戏区域网格线和墙体 GameDisplay.draw_game_window(screen, game_state, game_resource) #让最近绘制的屏幕可见 pygame.display.flip()
def draw_vs_img(screen, player_side): computer_side = OFFENSIVE_SIDE if player_side == DEFENSIVE_SIDE else DEFENSIVE_SIDE side_position = (DEFENSIVE_SIDE_X - 120, DEFENSIVE_SIDE_Y - 40) if player_side == DEFENSIVE_SIDE: screen.blit(GameResource.load_you_x_vs_computer_o_img(), side_position) else: screen.blit(GameResource.load_you_o_vs_computer_x_img(), side_position)
def draw_winner_img(screen, winner): img_postion = (MARGIN_LEFT + 50, MARGIN_TOP + 50) if winner == "player": screen.blit(GameResource.load_you_won_img(), img_postion) elif winner == "duce": screen.blit(GameResource.load_duce_img(), img_postion) else: screen.blit(GameResource.load_computer_won_img(), img_postion)
def __init__(self): self.screen = pygame.display.set_mode((320, 400)) pygame.display.set_caption("HRD 2.0") self.clock = pygame.time.Clock() self.person = Person(self.screen) self.resource = GameResource() self.resource.play_bg_music() self.auto = 0
def draw_piece(screen, cell, piece_type): cell_width = BOARD_SIZE // 3 r, c = cell left = MARGIN_LEFT + 25 + c * cell_width top = MARGIN_TOP + 25 + r * cell_width if piece_type == DEFENSIVE_SIDE: screen.blit(GameResource.load_x_piece_img(), (left, top)) else: screen.blit(GameResource.load_o_piece_img(), (left, top))
def main(): pygame.init() screen = pygame.display.set_mode((1200, 900)) #resolution ratio 1200*900 pygame.display.set_caption("TetrisGame") #window title pygame.key.set_repeat(50, 100) #background color bg_color = (230, 230, 230) #create blocks #piece = None random.seed(int(time.time())) #game_wall = GameWall(screen) #piece = Piece(random.choice(PIECE_TYPES), screen,game_wall) game_state = GameState(screen) game_resource = GameResource() game_resource.play_bg_music() #main body while True: if game_state.piece and game_state.piece.is_on_bottom: '''game_state.wall.add_to_wall(game_state.piece) game_state.add_score(game_state.wall.eliminate_lines()) game_state.piece = Piece(random.choice(PIECE_TYPES), screen,game_state.wall)''' game_state.touch_bottom() check_events(game_state, game_resource) #Fill in the screen background color screen.blit(game_resource.load_bg_img(), (0, 0)) #screen.fill(bg_color) #draw the wall #GameDisplay.draw_game_area(screen, game_state) #draw block with method if game_state.piece: game_state.piece.paint() GameDisplay.draw_game_area(screen, game_state, game_resource) # make draw visible pygame.display.flip()
def main(): pygame.init() screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption(u'俄罗斯方块') pygame.key.set_repeat(100, 100) game_resource = GameResource() game_state = GameState(screen, game_resource) game_resource.play_bg_music() while True: if game_state.piece and game_state.piece.is_at_bottom: game_state.touch_bottom() if game_state.score >= game_state.difficulty * SCORE_PER_LEVEL: game_state.add_difficulty() check_events(game_state, game_resource) screen.blit(game_resource.load_bg_img(), (0, 0)) GameDisplay.draw_game_area(game_state, game_resource) if game_state.piece: GameDisplay.draw_piece(game_state) pygame.display.flip()
def draw_select_side(screen): select_tip_font = pygame.font.SysFont('simhei', 24) select_tip_surface = select_tip_font.render('点击鼠标选择:', False, BLACK) select_tip_position = (MARGIN_LEFT - 200, DEFENSIVE_SIDE_Y + 15) screen.blit(select_tip_surface, select_tip_position) draw_select_button(screen, DEFENSIVE_SIDE_X, DEFENSIVE_SIDE_Y, '选后手(X)') draw_select_button(screen, OFFENSIVE_SIDE_X, OFFENSIVE_SIDE_Y, '选先手(O)') demo_position = (MARGIN_LEFT - 100, DEFENSIVE_SIDE_Y + 100) screen.blit(GameResource.load_howto_sanlianqi_img(), demo_position)
def main(): pygame.init() #初始化 #创建屏幕对象 screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption("俄罗斯方块") pygame.key.set_repeat(10, 100) #按下按键超过10ms触发下一次按键 bg_color = BG_COLOR #建立方块对象 #piece = Piece('J',screen) random.seed(int(time.time())) #产生不同的随机序列 #定义一个二维数组,且全部初始化为'_' #game_wall = GameWall(screen) # random.choice(range)在范围内随机选取一个作为返回值 #piece = Piece(random.choice(PIECE_TYPES), screen,game_wall) game_state = GameState(screen) game_resource = GameResource() #pygame.event.get():从事件队列中取出所有事件对象, #得到待处理事件列表 while True: #当前方块触底 if game_state.piece and game_state.piece.is_on_botton: #将当前方块标记为wall game_state.wall.add_to_wall(game_state.piece) game_state.add_score(game_state.wall.eliminate_lines()) #生成新的方块在游戏区域最上 #piece = Piece(random.choice(PIECE_TYPES),screen,game_state.wall) game_state.piece = Piece(random.choice(PIECE_TYPES), screen, game_state.wall) #监视键盘和鼠标事件 check_events(game_state.piece) #填充屏幕背景颜色 screen.fill(bg_color) #绘制游戏区域 GameDisplay.draw_game_area(screen, game_state, game_resource) #绘制小方块 #draw_cell(screen,GAME_AREA_LEFT+GAME_AREA_WIDTH//2,GAME_AREA_TOP) if game_state.piece: game_state.piece.paint() #让最近绘制的屏幕可见 pygame.display.flip()
def draw_newgame_img(screen): img_position = (MARGIN_LEFT - 50, MARGIN_TOP + BOARD_SIZE + 50) screen.blit(GameResource.load_newgame_tip_img(), img_position)
def draw_title(screen): title_postion = (MARGIN_LEFT, MARGIN_TOP - 300) screen.blit(GameResource.load_game_title_img(), title_postion)
class HRD(): def __init__(self): self.screen = pygame.display.set_mode((320, 400)) pygame.display.set_caption("HRD 2.0") self.clock = pygame.time.Clock() self.person = Person(self.screen) self.resource = GameResource() self.resource.play_bg_music() self.auto = 0 def run(self): global enable_print self.screen.fill((0, 0, 0)) while True: # max fps limit self.clock.tick(30) for event in pygame.event.get(): if event.type == QUIT: return elif event.type == KEYDOWN: # press esc to clear screen if event.key == K_ESCAPE: enable_print = 1 print('ESC KEY') pygame.quit exit() elif event.key ==K_m: self.resource.pause_bg_music() elif event.key ==K_b: self.person.step_back_move() elif event.key ==K_f: self.auto = 0 self.person.step_forward_move() elif event.key ==K_a: self.person.__init__(self.screen) self.auto = 1 #self.person.auto_move() elif event.key ==K_UP: enable_print = 1 self.auto = 0 self.person.__init__(self.screen) elif event.type == MOUSEBUTTONDOWN: if self.person.click_button(event.pos): #pos means coordinate when mouse.down #print('mouse-down: click a person') pass else: print('mouse-down: Not click a person') #self.brush.start_draw(event.pos) elif event.type == MOUSEMOTION: self.person.move_button(event.pos, event.rel) #rel代表相对距离 #print('mouse motion') #self.brush.draw(event.pos) elif event.type == MOUSEBUTTONUP: self.person.up_mouse(event.pos) #print('mouse up') #self.brush.end_draw() if(self.auto==1) : pygame.time.wait(400) self.person.step_forward_move() self.person.draw() pygame.display.update()