攻击有问题,我猜是循环有问题,大佬们帮帮忙

#注:All_lattice是所有72个格子合集,Lattice_attribute是All_lattice内鼠标所在位置实时格子
import pygame, sys, time, random
from pygame.locals import *
pygame.init()  # 出现了,No video mode has been set,的错误。是因为,定义屏幕要在转换图片之前,交换顺序就OK。
size = 1200, 600
screen = pygame.display.set_mode(size)  # 长宽
pygame.display.set_caption("界面")  # 游戏标题
pos_x = 0
pos_y = 0
start = 1  # 表示我方,0表示敌方
moving_game = True  # 鼠标移动
Choice = False  # 选择
Attack_yes = False  # 是否能攻击
Display_Blood=True
Distance = []  # 判断距离时
move_Distance = []  # 已选择移动距离集
Attack_Distance = []  # 攻击距离内所有格子属性值
font = pygame.font.Font(None, 36)
# 攻击(海陆空水),攻击费用/,/血量/移动,斜移格数,移动次数/费用/攻距,是否侧边/单位/海陆空水[-1]表示/图片,左边,右边/移动次数,攻击次数
bu_bing_shuxing = ["步兵", [1, 0, 1, 0, 0,0], [1, 0, 0], [1, 0, 1], 200, [1, 1,1], 1, 2,[1,1]]  # 步兵
qi_che_shuxing = ["汽车", [2, 0, 1, 0, 0,0], [3, 0, 0], [2, 0, 1], 1300, [1, 1,1], 2, 2, [1,1]]  # 汽车
jiao_yan_wei_shuxing = ["验", ["验", "验", "验", "验", "验","验"], [0, 0, 0], ["验", "验", "验"], "验", ["验", "验","验"] , 0,"验", [1,1]]
def print_text(font, x, y, text, q, color=(255, 255, 255)):#窗口打印字体
    imtext = font.render(text, True, color)
    myfont = pygame.font.Font(None, q)
    textimage = myfont.render(text, True, color)  # T/F表示文字抗锯齿
    screen.blit(textimage, (x, y))

def Attack():#主攻击,侧攻击,攻击类型
    if event.type == pygame.MOUSEBUTTONDOWN:  # 点击鼠标事件
        if event.button == 1 and Lattice_attribute[5 - start][6] > 0 :  # 判断此格子是否有敌方
            Lattice_attribute[5 - start][2][0] -= Choice_Arms[1][0]#扣血:所攻击的敌方(即鼠标指向格子敌方)血减去我方攻击
            print(bu_bing_shuxing)
            print(qi_che_shuxing)
            print("攻击后",All_lattice[mouse_number][5 - start][2][0])
    pygame.draw.rect(screen, (150, 0, 255), (cigezi_zuo, cigezi_shang, 100, 100), 7)  # 在选中的这个兵种这里显示紫色

All_lattice = ["校验位置"]
for j in range(1, 7):
    shang = (j - 1) * 100
    xia = j * 100
    for i in range(1, 13):
        zuo = (i - 1) * 100  # 1,2,3
        you = i * 100  # 海,陆,岩浆
        #Lattice_attribute = [shang, xia, zuo, you, 0, 0]  # 0为左边右边兵种
        All_lattice.append( [shang, xia, zuo, you, 0, 0] )
        if i == 5 and j == 2:
            All_lattice[(j - 1) * 12 + i][4] = bu_bing_shuxing  # 先定义第5列第2行为有单位(左方)
        if i == 6 and j == 2:
            All_lattice[(j - 1) * 12 + i][4] = bu_bing_shuxing  ##先定义第6列第2行为有单位(左方)
        if i == 6 and j == 3:
            All_lattice[(j - 1) * 12 + i][5] = qi_che_shuxing  # 先定义第6列第3行为有单位(右方)
        # 判定其他所有格子为校验位
        if All_lattice[(j - 1) * 12 + i][4] == 0:
            All_lattice[(j - 1) * 12 + i][4] = jiao_yan_wei_shuxing
        if All_lattice[(j - 1) * 12 + i][5] == 0:
            All_lattice[(j - 1) * 12 + i][5] = jiao_yan_wei_shuxing

while True:  # 游戏主循环
    for event in pygame.event.get():  # 事件监听
        screen.fill((255, 255, 255))#白板
        for i in range(1, 7):
            for j in range(1, 13):
                pos = pos_x + (j - 1) * 100, pos_y + (i - 1) * 100, 100, 100  # 位置和长宽
                pygame.draw.rect(screen, (0,0,0), pos, 1)  # 屏幕、颜色,位置和长宽,边框宽度
        print_text(font, All_lattice[17][2] + 5, All_lattice[17][0] + 14, str(All_lattice[17][4][2][0]), 25, (255, 0, 0))#血量
        print_text(font, All_lattice[18][2] + 5, All_lattice[18][0] + 14, str(All_lattice[18][4][2][0]), 25,(255, 0, 0))#血量
        print_text(font, All_lattice[18][2] + 5, All_lattice[18][0] + 30, "bubing", 25, (255, 0, 0))# 显示敌方兵种
        print_text(font, All_lattice[17][2] + 5, All_lattice[17][0] + 30, "bubing", 25, (255, 0, 0))# 显示敌方兵种
        print_text(font, All_lattice[30][2] + 50, All_lattice[30][0] + 15, "qiche", 25, (0, 0, 255))# 显示我方兵种
        if event.type == QUIT:
            exit()  # 接收到退出事件后退出程序
        position_game = pygame.mouse.get_pos()  # 获取当前鼠标位置
#这里开始循环72次
        for gezi in range(72):
            Lattice_attribute = All_lattice[gezi + 1] # 将鼠标在这格子范围内
            if position_game[1] >= Lattice_attribute[0] and position_game[1] < Lattice_attribute[1] \
                    and position_game[0] >= Lattice_attribute[2] and position_game[0] < Lattice_attribute[3]: #记录鼠标实时格子
                mouse_number = gezi + 1#鼠标的实时第几个格子数,因为gezi是从0开始,所以+1
                pygame.draw.rect(screen, (0, 0, 255), (Lattice_attribute[2], Lattice_attribute[0], 100, 100), 7)
                if event.type == pygame.MOUSEBUTTONDOWN:  # 点击鼠标事件
                    if event.button == 1 and int(Lattice_attribute[4 + start][6]) > 0:  # 点击鼠标左键     判断我方是否有单位
                        Choice = True  # 设置垃圾图片移动状态为移动
                        cigezi_shang = Lattice_attribute[0]
                        cigezi_xia = Lattice_attribute[1]
                        cigezi_zuo = Lattice_attribute[2]
                        cigezi_you = Lattice_attribute[3]
                        cigezi_number = gezi + 1#鼠标实时第几个格子
                if Choice == True:#不要放外面这里是一直循环
                    Choice_Arms = All_lattice[cigezi_number][4 + start]#选择的兵种
                    #print(Choice_Arms)
                    Attack()#上面定义的攻击
    pygame.display.update()
讨论数量: 7
Jason990420

代码格式错乱, 请使用
```python
代码
```

2周前 评论
Godzilla_baobaolong (楼主) 2周前
Godzilla_baobaolong (楼主) 2周前
Godzilla_baobaolong (楼主) 2周前
Godzilla_baobaolong (楼主) 2周前
Jason990420

没有看完代码, 如果没错, 问题就在这里 (不确定是否还有别的问题)

All_lattice[(2 - 1) * 12 + 5][4], All_lattice[(2 - 1) * 12 + 6][4] 以及 bu_bing_shuxing 都是同一个"东西", 而不是同一个"內容"; All_lattice[(3 - 1) * 12 + 6][5]qi_che_shuxing也是同一个"东西". 不管你改变哪一个, 结果都是一样, 因为全部只有一个.

        if i == 5 and j == 2:
            All_lattice[(j - 1) * 12 + i][4] = bu_bing_shuxing
        if i == 6 and j == 2:
            All_lattice[(j - 1) * 12 + i][4] = bu_bing_shuxing
        if i == 6 and j == 3:
            All_lattice[(j - 1) * 12 + i][5] = qi_che_shuxing
        if All_lattice[(j - 1) * 12 + i][4] == 0:
            All_lattice[(j - 1) * 12 + i][4] = jiao_yan_wei_shuxing
        if All_lattice[(j - 1) * 12 + i][5] == 0:
            All_lattice[(j - 1) * 12 + i][5] = jiao_yan_wei_shuxing

Python的物件复制

  • 赋值(Assignment): 用于将名称(重)绑定到特定值。
  • 浅层复制(Shallow Copy): 浅层复制建构一个新的复合物件,然后(在尽可能的范围内)将原始物件中找到的物件的参照插入其中。
  • 深层复制(Deep Copy): 深层复制建构一个新的复合物件,然后递回地将在原始物件里找到的物件的副本插入其中。

以下例子说明一般的赋值

>>> a = [0, 0, 0, 0]
>>> b = [a for i in range(3)]
>>> b
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
>>> b[1][2] = 8
>>> b
[[0, 0, 8, 0], [0, 0, 8, 0], [0, 0, 8, 0]]
>>> a
[0, 0, 8, 0]
>>> id(a)
2769546014528
>>> [id(item) for item in b]
[2769546014528, 2769546014528, 2769546014528]

修改方法

from copy import deepcopy

        if i == 5 and j == 2:
            All_lattice[(j - 1) * 12 + i][4] = deepcopy(bu_bing_shuxing)
        if i == 6 and j == 2:
            All_lattice[(j - 1) * 12 + i][4] = deepcopy(bu_bing_shuxing)
        if i == 6 and j == 3:
            All_lattice[(j - 1) * 12 + i][5] = deepcopy(qi_che_shuxing)
        if All_lattice[(j - 1) * 12 + i][4] == 0:
            All_lattice[(j - 1) * 12 + i][4] = deepcopy(jiao_yan_wei_shuxing)
        if All_lattice[(j - 1) * 12 + i][5] == 0:
            All_lattice[(j - 1) * 12 + i][5] = deepcopy(jiao_yan_wei_shuxing)
2周前 评论
Godzilla_baobaolong (楼主) 2周前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!