攻击有问题,我猜是循环有问题,大佬们帮帮忙
#注: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()
推荐文章: