游戏简介:
没有美术,画面简洁(懒得做)。玩家控制小球躲避敌人(上下左右,闪避),敌人体积越大速度越慢,随机生成道具球(目前只有生命球),靠近道具球可拾取。
未来展望:
1. 添加其他道具球
2. 添加攻击手段,目前只能闪避。
3. 添加耐力条
4. 添加更多属性
核心代码
玩家移动
def player_move(space_down, player):"""控制玩家移动处理:param space_down: 是否按下空格:param player: 玩家:return:"""global monitorif space_down:speed = player.dodge_speedelse:speed = player.speedkeys = pygame.key.get_pressed()if keys[pygame.K_LEFT]:player.x -= speedif keys[pygame.K_RIGHT]:player.x += speedif keys[pygame.K_UP]:player.y -= speedif keys[pygame.K_DOWN]:player.y += speedif player.x < 0:player.x = monitor.width + player.xelif player.x > monitor.width:player.x = player.x - monitor.widthif player.y < 0:player.y = monitor.height + player.yelif player.y > monitor.height:player.y = player.y - monitor.height
生成小怪
def make_enemy():"""生成小怪:return:"""global monitor, scoreboss = Falseif score % 20000 == 0 and score != 0:boss = TruesizeList = [10, 30, 50, 70, 100, 130, 150, 180, 200, 250]random_int = random.randint(1, 10) if boss is False else 20enemy = Enemy(atc=random_int,max_health=random_int,defense=0,speed=(11 - random_int) * 1.5 if boss is False else 1.5,attribute=0,x=random.uniform(0.1, 1) * monitor.width,y=random.uniform(0.1, 1) * monitor.height,size=sizeList[random_int - 1] if boss is False else 500)return enemy
道具球处理
def propBall_handle(propBall_list, window, player):"""道具球处理:param propBall_list::param window::param player::return:"""count = 0for propBall in propBall_list:pygame.draw.circle(window, propBall.color, (propBall.x, propBall.y), propBall.size)propBall.moveToPlayer(player.x, player.y)if detectIntersect(player, propBall):propBall_function(player, propBall)del propBall_list[count]count += 1if score % 200 == 0:propBall = generate_propBall()if propBall is not None:propBall_list.append(propBall)return propBall_list
游戏主要逻辑
def main():global is_running, is_playing, font, pass_time, pass_time_made, score, start_time, monitorpropBall_list = []window = pygame.display.set_mode((monitor.width, monitor.height))pygame.display.set_caption("demo")player = init_player()health_bal = pygame.Rect(20, 20, player.health * 20, 20)enemyList = [make_enemy()]button_playAgain = pygame.Rect(monitor.width // 2 - button_width // 2, monitor.height * 0.6,button_width, button_height)button_quit = pygame.Rect(monitor.width // 2 - button_width // 2,monitor.height * 0.6 + 30 + button_height,button_width, button_height)buttonList = [button_playAgain, button_quit]while is_running:score += 1window.fill(color_dict['black']) # 填充黑色health_bal.width = player.health * 20space_down = Falsefor event in pygame.event.get():if event.type == pygame.QUIT:is_running = Falseelif event.type == KEYDOWN:if event.key == K_SPACE:space_down = Trueif event.type == pygame.MOUSEBUTTONDOWN:mouse_pos = pygame.mouse.get_pos()button_serialNum = button_clicked(buttonList, mouse_pos)if button_serialNum == 0:is_playing = Trueplayer = init_player()enemyList = [make_enemy()]elif button_serialNum == 1:is_running = Falseif is_playing:if pass_time_made is False:pass_time = 0start_time = time.perf_counter()pass_time_made = Trueif player.health == 0:is_playing = Falseif score % 400 == 0:enemyList.append(make_enemy())propBall_list = propBall_handle(propBall_list, window, player)player_move(space_down, player) # 玩家移动player_twinkle(player, window) # 玩家绘制draw_healthBar(window, player, health_bal) # 血条更新make_enemyThreading(enemyList, window, player) # 小怪更新draw_score(window)pass_time = int(time.perf_counter() - start_time)else:draw_scoreTitle(window)draw_button(buttonList, window)pass_time_made = FalsepropBall_list = []enemyList = []pygame.display.flip() # 刷新屏幕time.sleep(0.01)pygame.quit()
游戏画面
完整代码
有需要者自取,盘内还有打包好的exe文件
链接:https://pan.baidu.com/s/1rZ1xNZJYtvyXPIG9Rgh5Hw
提取码:iq6l