如何在我的RTS游戏中扩展单位选择逻辑以应用于多个单位?

时间:2016-10-19 19:57:17

标签: python python-3.x pygame 2d-games

目前,如果您左键单击该设备,它将被选中' (或'取消选择'),并在其周围绘制绿色方块。然后,当您右键单击屏幕上的某个位置时,该单元会整齐地移动到您单击的位置的方块中。

此外,如果您使用向上,向下,向左或向右键,它将滚动屏幕。

import pygame
import random
pygame.init()

#Define mouse position
mouse_position_x = 525
mouse_position_y = 315

# Define colors
green = (0,255,0)
brown = (150,75,0)

#Define border position
border_x = 0
border_y = 0

#Define character selection box
def character_selection_box():
    pygame.draw.line(screen,green,(character_location_x,character_location_y),(character_location_x+character_width,character_location_y),2) # Top bar
    pygame.draw.line(screen,green,(character_location_x,character_location_y+character_height),(character_location_x+character_width,character_location_y+character_height),2) # Bottom bar
    pygame.draw.line(screen,green,(character_location_x,character_location_y),(character_location_x,character_location_y+character_height),2) # Left bar
    pygame.draw.line(screen,green,(character_location_x+character_width,character_location_y),(character_location_x+character_width,character_location_y+character_height+1),2) # Right bar

#Define round
def assign_square(n):
    div = (n/35)
    rou = round(div)
    mul = (35*rou)
    return int(mul)

#Set window
screen_width = 981
screen_height = 700
game_screen_width = 800
game_screen_height = 700
screen_size = (screen_width,screen_height)
screen = pygame.display.set_mode(screen_size)
pygame.display.set_caption("Warpath")

#Set block character
character_width = 35
character_height = 35
character_location_x = 525
character_location_y = 315
movement = 1
unit_selected = 0

#Load images
orc_grunt_forward = pygame.image.load('orc_forward3.png') #(35 by 35 pixel image)
character_image = orc_grunt_forward

#Loop until the user clicks the close button
shutdown = False

#Set clock
clock = pygame.time.Clock()

#Set scroll limit
scroll_x = 0
scroll_y = 0

 # ---------- Main program loop -----------
while not shutdown:

    # --- Main event loop ---
    for event in pygame.event.get():

        # --- If quit button pressed, shutdown
        if event.type == pygame.QUIT: 
            shutdown = True

        # --- If mouse button pressed
        elif event.type == pygame.MOUSEBUTTONDOWN: # If a mouse button is pressed
            mouse_position = pygame.mouse.get_pos() # Get mouse position
            button_type = pygame.mouse.get_pressed() # Check which button was pressed

            # --- If left click pressed and the curser was on a character, select that character 
            if button_type[0] == 1 and mouse_position[0] >= character_location_x and mouse_position[0] <= character_location_x + character_width and mouse_position[1] >= character_location_y and mouse_position[1] <= character_location_y + character_height:
                print("Unit selected",unit_selected)
                print(button_type)
                unit_selected += 1
                unit_selected /= unit_selected #(Otherwise it will add up unit selected if you click more than once)
                int(unit_selected)

            # --- If right click pressed and a character was selected (and it's within the game screen), move the character to the location     
            elif button_type[2] == 1 and unit_selected == 1 and mouse_position[0] > 175: 
                mouse_position_x *= 0
                mouse_position_y *= 0

                if mouse_position[0] >= assign_square(mouse_position[0]):
                    mouse_position_x += assign_square(mouse_position[0])

                elif mouse_position[0] <= assign_square(mouse_position[0]):
                    mouse_position_x -= 35
                    mouse_position_x += assign_square(mouse_position[0])

                if mouse_position[1] >= assign_square(mouse_position[1]):
                    mouse_position_y += assign_square(mouse_position[1])

                elif mouse_position[1] <= assign_square(mouse_position[1]):
                    mouse_position_y -= 35
                    mouse_position_y += assign_square(mouse_position[1])

            # --- If left click pressed and the curser was not on a character, deselect the character        
            elif button_type[0] == 1 and mouse_position[0] < character_location_x or mouse_position[0] > character_location_x + character_width or mouse_position[1] < character_location_y or mouse_position[1] > character_location_y + character_height:
                print("Unit not selected")
                print(button_type)
                unit_selected *= 0
                int(unit_selected)

        # --- If key pressed, scroll the screen
        elif event.type == pygame.KEYDOWN:

            if event.key == pygame.K_RIGHT and scroll_x > -10:
                direction = "right"
                character_location_x -= 35
                mouse_position_x -= 35
                border_x -= 35
                scroll_x -= 1

            if event.key == pygame.K_LEFT and scroll_x < 10:
                direction = "left"
                character_location_x += 35
                mouse_position_x += 35
                border_x += 35
                scroll_x += 1

            if event.key == pygame.K_UP and scroll_y < 10:
                direction = "up"
                character_location_y += 35
                mouse_position_y += 35
                border_y += 35
                scroll_y += 1

            if event.key == pygame.K_DOWN and scroll_y > -10:
                direction = "down"
                character_location_y -= 35
                mouse_position_y -= 35
                border_y -= 35
                scroll_y -= 1

    # --- Game logic ---

    # --- Set character movement      
    if character_location_x < mouse_position_x:
        character_location_x += movement
    if character_location_x > mouse_position_x:
        character_location_x -= movement
    if character_location_y < mouse_position_y:
        character_location_y += movement
    if character_location_y > mouse_position_y:
        character_location_y -= movement

    # --- Drawing ---
    screen.fill(brown) # Draw background
    screen.blit(character_image,(character_location_x,character_location_y)) # Draw character

    if unit_selected == 1: 
        character_selection_box() # Draw character selection box if unit is selected

    clock.tick(30)
    pygame.display.flip()

#Shutdown
if shutdown == True:
    pygame.quit()

问题在于我无法弄清楚如何将其扩展到多个单元 - 目前如果我想添加更多单元,我只能设法:

a)立即将它们全部移动

b)多次调整字符变量粘贴相同的代码(不是强大/可扩展的解决方案)

如何调整我的代码,以便我有一个可扩展的解决方案:

1)我可以选择一个单位并移动它,而不必一次移动每个单元

2)我可以通过单独点击每个单位来选择多个单位,并立即将它们全部移动(现在不用担心寻路)

我也尝试使用类来实现这一点,但我仍感觉我正在复制/粘贴多个函数,而不是拥有一个强大的解决方案。

在保留正常运行的程序的同时,我删除了任何与此无关的代码。

由于

1 个答案:

答案 0 :(得分:1)

有几件事要做:

  1. 将变量character_*更改为包含有关该单元的所有数据的对象。
  2. 创建单位/字符数组。这样,阵列中的每个单元都可以具有独特的位置,速度ets。
  3. 在您检查curl -X POST -H "Content-Type: application/json" -d '{"setting_type" : "call_to_actions","thread_state" : "existing_thread","call_to_actions":[{"type":"postback","title":"TITLE1","payload":"action?action1"}, {"type":"postback","title":"TITLE2","payload":"action?action2"},{"type":"postback","title":"TITLE3","payload":"action?action3"}]}' "https://graph.facebook.com/v2.6/me/thread_settings?access_token={YOUR ACCESS TOKEN}" 的代码中的任何地方,更改为for循环,其中您遍历字符数组以检查每个单元。
  4. 下一步应该是添加移动/拍摄等功能到字符类,使按键事件适用于多个单元。
  5. 这应该为您提供代码,您可以在其中选择多个单位(如果它们占据相同的位置)并独立于取消选择的单位移动它们。