python 炸飞机小游戏
本帖最后由 昨非 于 2020-8-16 11:40 编辑这个“炸飞机”小游戏算是大一期末大作业,初来乍到,必有很多不足之处,还望指正。
功能简介(游戏规则)
双方各自在自己9*9的战场(表格)中布置三架指定形状(士字形)的飞机,布置过程可以用鼠标右键旋转飞机,点击左键确定。
交战阶段:双方互相猜测对方飞机的位置,左键单击投放炸弹,单击右键可对战场进行标记。根据是否炸中飞机、是否炸中飞机头部,会产生三种不同的反馈,以单元格颜色显示,并规定:只有爆头才会将飞机击毁。
胜利判定:先将对手三架飞机全部爆头者获胜。
框架及效果图如下:
https://s1.ax1x.com/2020/08/16/dAzhqK.png
https://s1.ax1x.com/2020/08/16/dAz7PH.png
https://s1.ax1x.com/2020/08/16/dAzjqf.png
https://s1.ax1x.com/2020/08/16/dESizn.png
https://s1.ax1x.com/2020/08/16/dESkMq.png
https://s1.ax1x.com/2020/08/16/dESmoF.png
源码分为四部分:
main模块:
import pygame
import sys
import traceback
import random
import board
import random
from random import *
from pygame.locals import *
#初始化pygame
pygame.init()
pygame.mixer.init()
size = width, height = 500,500
#创建指定大小的窗口
screen = pygame.display.set_mode(size)
#设置窗口标题
pygame.display.set_caption('炸飞机')
#导入图片
background = pygame.image.load("image/background.jpg").convert_alpha()
intro1 =pygame.image.load("image/intro1.png").convert_alpha()
intro2 =pygame.image.load("image/intro2.png").convert_alpha()
bluewin = pygame.image.load("image/bluewin.png").convert_alpha()
goldwin = pygame.image.load("image/goldwin.png").convert_alpha()
lost = pygame.image.load("image/lost.png").convert_alpha()
choose = pygame.image.load("image/choose.png").convert_alpha()
begin1 = pygame.image.load("image/begin.png").convert_alpha()
begin2 = pygame.image.load("image/rebegin.png").convert_alpha()
begin = begin1
begin_rect =begin1.get_rect()
begin_rect.left,begin_rect.top = 200,50
start1 = pygame.image.load("image/start.png").convert_alpha()
start2 = pygame.image.load("image/restart.png").convert_alpha()
start = start1
start_rect =start1.get_rect()
start_rect.left,start_rect.top = 200,10
introduce1 = pygame.image.load("image/introduce.png").convert_alpha()
introduce2 = pygame.image.load("image/reintroduce.png").convert_alpha()
introduce = introduce1
introduce_rect = introduce1.get_rect()
introduce_rect.left,introduce_rect.top = 200,90
back1 = pygame.image.load("image/back.png").convert_alpha()
back2 = pygame.image.load("image/reback.png").convert_alpha()
back = back1
back_rect =back1.get_rect()
back_rect.left,back_rect.top = 400,290
next1 = pygame.image.load("image/next.png").convert_alpha()
next2 = pygame.image.load("image/renext.png").convert_alpha()
nexta = next1
next_rect =next1.get_rect()
next_rect.left,next_rect.top = 400,250
def main():
page= 1
clock = pygame.time.Clock()
while True:
#检验用户鼠标的位置
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == MOUSEMOTION:
if begin_rect.collidepoint(event.pos):
begin = begin2
else:
begin = begin1
if start_rect.collidepoint(event.pos):
start = start2
else:
start = start1
if introduce_rect.collidepoint(event.pos):
introduce = introduce2
else:
introduce = introduce1
if next_rect.collidepoint(event.pos):
nexta = next2
else:
nexta = next1
if back_rect.collidepoint(event.pos):
back = back2
else:
back = back1
#检验用户是否按下鼠标
if event.type == MOUSEBUTTONDOWN and start_rect.collidepoint(event.pos):
page = 4
if event.type == MOUSEBUTTONDOWN and introduce_rect.collidepoint(event.pos):
page = 2
if event.type == MOUSEBUTTONDOWN and next_rect.collidepoint(event.pos):
if page == 2:
page = 3
elif page == 3:
page = 2
if event.type == MOUSEBUTTONDOWN and back_rect.collidepoint(event.pos) :
page = 1
if event.type == MOUSEBUTTONDOWN and begin_rect.collidepoint(event.pos) :
page = 5
key_pressed = pygame.key.get_pressed()
#设置主页面
if page == 1 :
screen.blit(background,(0,0))
screen.blit(start,start_rect)
screen.blit(introduce,introduce_rect)
screen.blit(begin,begin_rect)
if page == 2:
screen.blit(intro1,(0,0))
screen.blit(nexta,next_rect)
screen.blit(back,back_rect)
if page == 3:
screen.blit(intro2,(0,0))
screen.blit(nexta,next_rect)
screen.blit(back,back_rect)
#玩家对战玩家
if page == 4:
again1 = pygame.image.load("image/again.png").convert_alpha()
again2 = pygame.image.load("image/reagain.png").convert_alpha()
again = again1
again_rect =again1.get_rect()
again_rect.left,again_rect.top = 10,350
menu1 = pygame.image.load("image/menu.png").convert_alpha()
menu2 = pygame.image.load("image/remenu.png").convert_alpha()
menu = menu1
menu_rect = menu1.get_rect()
menu_rect.left,menu_rect.top = 10,400
showgold1 = pygame.image.load("image/showgold.png").convert_alpha()
showgold2 = pygame.image.load("image/reshowgold.png").convert_alpha()
showgold = showgold1
showgold_rect = showgold1.get_rect()
showgold_rect.left,showgold_rect.top = 10,300
showblue1 = pygame.image.load("image/showblue.png").convert_alpha()
showblue2 = pygame.image.load("image/reshowblue.png").convert_alpha()
showblue = showblue1
showblue_rect = showblue1.get_rect()
showblue_rect.left,showblue_rect.top = 10,250
back3 = pygame.image.load("image/back.png").convert_alpha()
back4 = pygame.image.load("image/reback.png").convert_alpha()
back5 = back3
back5_rect =back3.get_rect()
back5_rect.left,back5_rect.top = 10,450
clock=pygame.time.Clock()
running = True
result = 0
showtype = 0
#摆放飞机
board.points = 0
blue = board.Color(board.blue)
while board.points<3:
blue.make_plane()
pygame.display.flip()
if board.points == 4:
running = False
page = 1
result = 1
if board.points == 3:
board.points = 0
gold = board.Color(board.gold)
while board.points<3:
gold.make_plane()
pygame.display.flip()
if board.points == 4:
running = False
page = 1
result = 2
#轮流攻击
while result == 0 :
if board.turn%2 == 1 :
gold.attark()
if gold.score == 3 or blue.score <0:
result = 1
board.turn = 1
if board.turn%2 == 0 :
blue.attark()
if blue.score == 3 or gold.score < 0:
result = 2
board.turn = 1
pygame.display.flip()
clock.tick(60)
#加载结束界面
while running :
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == MOUSEMOTION:
if again_rect.collidepoint(event.pos):
again = again2
else:
again = again1
if menu_rect.collidepoint(event.pos):
menu = menu2
else:
menu = menu1
if showgold_rect.collidepoint(event.pos):
showgold = showgold2
else:
showgold = showgold1
if showblue_rect.collidepoint(event.pos):
showblue = showblue2
else:
showblue = showblue1
if back5_rect.collidepoint(event.pos):
back5 = back4
else:
back5 = back3
if event.type == MOUSEBUTTONDOWN and again_rect.collidepoint(event.pos):
page = 4
running = False
if event.type == MOUSEBUTTONDOWN and menu_rect.collidepoint(event.pos) :
page = 1
running = False
if event.type == MOUSEBUTTONDOWN and showgold_rect.collidepoint(event.pos) :
showtype = 2
if event.type == MOUSEBUTTONDOWN and showblue_rect.collidepoint(event.pos) :
showtype = 1
if event.type == MOUSEBUTTONDOWN and back5_rect.collidepoint(event.pos) :
showtype = 0
if showtype == 0:
if result == 1 :
screen.blit(bluewin,(0,0))
if result == 2 :
screen.blit(goldwin,(0,0))
screen.blit(again,again_rect)
screen.blit(menu,menu_rect)
screen.blit(showgold,showgold_rect)
screen.blit(showblue,showblue_rect)
if showtype == 1:
screen.fill(board.white)
blue.show()
screen.blit(back5,back5_rect)
if showtype == 2:
screen.fill(board.white)
gold.show()
screen.blit(back5,back5_rect)
pygame.display.flip()
clock.tick(60)
#玩家对战电脑
if page == 5:
choice = 0
again1 = pygame.image.load("image/again.png").convert_alpha()
again2 = pygame.image.load("image/reagain.png").convert_alpha()
again = again1
again_rect =again1.get_rect()
again_rect.left,again_rect.top = 10,350
menu1 = pygame.image.load("image/menu.png").convert_alpha()
menu2 = pygame.image.load("image/remenu.png").convert_alpha()
menu = menu1
menu_rect = menu1.get_rect()
menu_rect.left,menu_rect.top = 10,400
showgold1 = pygame.image.load("image/showgold.png").convert_alpha()
showgold2 = pygame.image.load("image/reshowgold.png").convert_alpha()
showgold = showgold1
showgold_rect = showgold1.get_rect()
showgold_rect.left,showgold_rect.top = 10,300
showblue1 = pygame.image.load("image/showblue.png").convert_alpha()
showblue2 = pygame.image.load("image/reshowblue.png").convert_alpha()
showblue = showblue1
showblue_rect = showblue1.get_rect()
showblue_rect.left,showblue_rect.top = 10,250
back3 = pygame.image.load("image/back.png").convert_alpha()
back4 = pygame.image.load("image/reback.png").convert_alpha()
back5 = back3
back5_rect =back3.get_rect()
back5_rect.left,back5_rect.top = 10,450
easy1 = pygame.image.load("image/easy.png").convert_alpha()
easy2 = pygame.image.load("image/reeasy.png").convert_alpha()
easy = easy1
easy_rect =easy1.get_rect()
easy_rect.left,easy_rect.top = 200,100
normal1 = pygame.image.load("image/normal.png").convert_alpha()
normal2 = pygame.image.load("image/renormal.png").convert_alpha()
normal = normal1
normal_rect =normal1.get_rect()
normal_rect.left,normal_rect.top = 200,140
hard1 = pygame.image.load("image/hard.png").convert_alpha()
hard2 = pygame.image.load("image/rehard.png").convert_alpha()
hard = hard1
hard_rect =hard1.get_rect()
hard_rect.left,hard_rect.top = 200,180
while choice == 0:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == MOUSEMOTION:
if easy_rect.collidepoint(event.pos):
easy = easy2
else:
easy = easy1
if normal_rect.collidepoint(event.pos):
normal = normal2
else:
normal = normal1
if hard_rect.collidepoint(event.pos):
hard = hard2
else:
hard = hard1
if event.type == MOUSEBUTTONDOWN and easy_rect.collidepoint(event.pos):
choice = 1
if event.type == MOUSEBUTTONDOWN and normal_rect.collidepoint(event.pos):
choice = 2
board.body = 0
board.miss = 0
if event.type == MOUSEBUTTONDOWN and hard_rect.collidepoint(event.pos):
choice = 2
board.body = 9
board.miss = 1
screen.blit(choose,(0,0))
screen.blit(easy,easy_rect)
screen.blit(normal,normal_rect)
screen.blit(hard,hard_rect)
pygame.display.flip()
clock=pygame.time.Clock()
running = True
result = 0
showtype = 0
board.points = 0
blue = board.Color(board.blue)
while board.points<3:
blue.make_plane()
pygame.display.flip()
if board.points == 4:
running = False
page = 1
result = 1
if board.points == 3:
board.points = 0
gold = board.Color(board.gold)
while board.points<3:
gold.computer_make_plane()
pygame.display.flip()
if board.points == 4:
running = False
page = 1
result = 2
board.turn = 1
while result == 0 :
if board.turn%2 == 0 :
if blue.score == 3 or gold.score < 0:
result = 2
break
if choice == 2:
blue.computer_attark_main()
if choice == 1:
blue.computer_attark()
if blue.score == 3 or gold.score < 0:
result = 2
break
if board.turn%2 == 1 :
if gold.score == 3 or blue.score <0:
result = 1
break
gold.attark()
if gold.score == 3 or blue.score <0:
result = 1
break
pygame.display.flip()
clock.tick(60)
while running :
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == MOUSEMOTION:
if again_rect.collidepoint(event.pos):
again = again2
else:
again = again1
if menu_rect.collidepoint(event.pos):
menu = menu2
else:
menu = menu1
if showgold_rect.collidepoint(event.pos):
showgold = showgold2
else:
showgold = showgold1
if showblue_rect.collidepoint(event.pos):
showblue = showblue2
else:
showblue = showblue1
if back5_rect.collidepoint(event.pos):
back5 = back4
else:
back5 = back3
if event.type == MOUSEBUTTONDOWN and again_rect.collidepoint(event.pos):
page = 5
running = False
if event.type == MOUSEBUTTONDOWN and menu_rect.collidepoint(event.pos) :
page = 1
running = False
if event.type == MOUSEBUTTONDOWN and showgold_rect.collidepoint(event.pos) :
showtype = 2
if event.type == MOUSEBUTTONDOWN and showblue_rect.collidepoint(event.pos) :
showtype = 1
if event.type == MOUSEBUTTONDOWN and back5_rect.collidepoint(event.pos) :
showtype = 0
if showtype == 0:
if result == 1 :
screen.blit(bluewin,(0,0))
if result == 2 :
screen.blit(lost,(0,0))
screen.blit(again,again_rect)
screen.blit(menu,menu_rect)
screen.blit(showgold,showgold_rect)
screen.blit(showblue,showblue_rect)
if showtype == 1:
screen.fill(board.white)
blue.show()
screen.blit(back5,back5_rect)
if showtype == 2:
screen.fill(board.white)
gold.show()
screen.blit(back5,back5_rect)
pygame.display.flip()
clock.tick(60)
pygame.display.flip()
clock.tick(60)
pygame.display.flip()
clock.tick(60)
if __name__=="__main__":
try:
main()
except SystemExit:
pass
except:
traceback.print_exc()
pygame.quit()
input()
board模块:
import pygame
import sys
import traceback
import random
import pickle
from random import *
from pygame.locals import *
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((500,500))
screen.fill((255,255,255))
white = (255,255,255)
black = (0,0,0)
grey = (190,190,190)
gold = (255,215,0)
green = (0,255,0)
red = (255,0,0)
blue = (0,0,255)
cyan = (0,255,255)
purple = (255,0,255)
deepskyblue = (0,191,255)
cishu = 0
giveup1 = pygame.image.load("image/giveup.png").convert_alpha()
giveup2 = pygame.image.load("image/regiveup.png").convert_alpha()
giveup = giveup1
direction = 0
y = 0
x = 0
points = 0
num = -100
turn = 1
body = 0
miss = 0
#定义棋盘类,存储棋盘格子变量
class Board(pygame.sprite.Sprite):
def __init__(self,x,y,color):
pygame.sprite.Sprite.__init__(self)
self.rect = (x,y,50,50)
self.circle = (x+25,y+25)
self.color = color
self.nature = 1
self.true = 0
self.judge = 0
def draw(self,col):
if self.true == 0:
pygame.draw.rect(screen,col,self.rect,self.nature)
self.nature = 1
if (self.true == 1 or self.true == 2) and self.nature ==1:
pygame.draw.rect(screen,self.color,self.rect,0)
if (self.true == 1 or self.true == 2) and self.nature ==0:
pygame.draw.rect(screen,red,self.rect,0)
self.nature = 1
def redraw(self,col):
if self.judge == 0 and self.nature == 1 :
pygame.draw.rect(screen,black,self.rect,1)
if self.judge == 0 and self.nature == 0:
pygame.draw.rect(screen,col,self.rect,0)
self.nature = 1
if self.judge == 1 :
pygame.draw.rect(screen,black,self.rect,1)
if self.true == 1 :
pygame.draw.circle(screen,col,self.circle,25,0)
if self.true == 2:
pygame.draw.circle(screen,red,self.circle,25,0)
if self.true == 0:
pygame.draw.circle(screen,grey,self.circle,25,0)
if self.judge == 2 :
pygame.draw.rect(screen,black,self.rect,1)
pygame.draw.circle(screen,col,self.circle,15,0)
def clean(self):
pygame.draw.rect(screen,white,self.rect,0)
#颜色类,内置函数用来进行游戏(分为蓝色方与黄色方)
class Color(pygame.sprite.Sprite):
def __init__(self,color):
pygame.sprite.Sprite.__init__(self)
self.color = color
self.map = []
if color == blue :
self.set = gold
if color == gold :
self.set = blue
self.score = 0
for i in range(9):
for q in range(9):
color_ = Board(50*i,50*q,color)
color_.draw(black)
self.map.append(color_)
#摆放飞机函数
def make_plane(self):
global direction,x,y,points,num
color=
lay1 = pygame.image.load("image/lay.png").convert_alpha()
lay2 = pygame.image.load("image/relay.png").convert_alpha()
lay = lay1
lay_rect =lay1.get_rect()
lay_rect.left,lay_rect.top = 0,450
back1 = pygame.image.load("image/back.png").convert_alpha()
back2 = pygame.image.load("image/reback.png").convert_alpha()
back = back1
back_rect =back1.get_rect()
back_rect.left,back_rect.top = 100,450
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == MOUSEMOTION:
if back_rect.collidepoint(event.pos):
back = back2
else:
back = back1
if lay_rect.collidepoint(event.pos):
lay = lay2
else:
lay = lay1
if event.type == MOUSEBUTTONDOWN and lay_rect.collidepoint(event.pos):
points = 0
for each in self.map:
each.true = 0
each.color = self.color
if event.type == MOUSEBUTTONDOWN and back_rect.collidepoint(event.pos):
points = 4
if event.type == MOUSEBUTTONDOWN and event.button == 3:
if direction == 3:
direction =0
else :
direction += 1
if event.type == MOUSEMOTION or event.type == MOUSEBUTTONDOWN :
screen.fill(white)
pos = pygame.mouse.get_pos()
x = pos//50
y = pos//50
if y==9:
num = -100
else :
num = 9*x+y
q = 0
key = 0
for each in self.map:
#绘制飞机头向上时的图案
if direction == 0:
if y<6 and x>=2 and x<=6:
if q==num or q==num+1 or q==num-8 or q==num+10 or q==num-17 or q==num+19 or q==num+2 or q==num+3 or q==num-6 or q==num+12:
each.nature = 0
each.draw(each.color)
q+=1
if each.true == 0:
key += 1
else :
each.draw(black)
q+=1
if y<6 and (x<2 or x>6):
if q==num or q==num+1 or q==num-8 or q==num+10 or q==num-17 or q==num+19 or q==num+2 or q==num+3 or q==num-6 or q==num+12:
each.nature = 0
each.draw(red)
q+=1
else :
each.draw(black)
q+=1
if y==6 :
if q==num or q==num+1 or q==num-8 or q==num+10 or q==num-17 or q==num+19 or q==num+2:
each.nature = 0
each.draw(red)
q+=1
else :
each.draw(black)
q+=1
if y==7 :
if q==num or q==num+1 or q==num-8 or q==num+10 or q==num-17 or q==num+19:
each.nature = 0
each.draw(red)
q+=1
else :
each.draw(black)
q+=1
if y>=8 :
if q==num:
each.nature = 0
each.draw(red)
q+=1
else :
each.draw(black)
q+=1
#绘制飞机头向右时的图案
if direction == 1:
if 8>=x>=3 and y>=2 and y<=6:
if q==num or q==num-9 or q==num-8 or q==num-10 or q==num-7 or q==num-11 or q==num-18 or q==num-27 or q==num-26 or q==num-28:
each.nature = 0
each.draw(each.color)
q+=1
if each.true == 0:
key += 1
else :
each.draw(black)
q+=1
if (x<3 or x==9) and y>=2 and y<=6:
if q==num or q==num-9 or q==num-8 or q==num-10 or q==num-7 or q==num-11 or q==num-18 or q==num-27 or q==num-26 or q==num-28:
each.nature = 0
each.draw(red)
q+=1
else :
each.draw(black)
q+=1
ify == 1 :
if q==num or q==num-9 or q==num-8 or q==num-10 or q==num-7 or q==num-18 or q==num-27 or q==num-26 or q==num-28:
each.nature = 0
each.draw(red)
q+=1
else :
each.draw(black)
q+=1
ify == 0 :
if q==num or q==num-9 or q==num-8 or q==num-7 or q==num-18 or q==num-27 or q==num-26:
each.nature = 0
each.draw(red)
q+=1
else :
each.draw(black)
q+=1
if y==7:
if q==num or q==num-9 or q==num-8 or q==num-10or q==num-11 or q==num-18 or q==num-27 or q==num-26 or q==num-28:
each.nature = 0
each.draw(red)
q+=1
else :
each.draw(black)
q+=1
if y>=8:
if q==num or q==num-9 or q==num-10or q==num-11 or q==num-18 or q==num-27 or q==num-28:
each.nature = 0
each.draw(red)
q+=1
else :
each.draw(black)
q+=1
#绘制飞机头向下时的图案
if direction == 2:
if 2<=x<=6 and y>=3:
if q==num or q==num-1 or q==num+8 or q==num-10 or q==num+17 or q==num-19 or q==num-2 or q==num-3 or q==num+6 or q==num-12:
each.nature = 0
each.draw(each.color)
q+=1
if each.true == 0:
key += 1
else :
each.draw(black)
q+=1
if (x<2 or x>6) and y>=3:
if q==num or q==num-1 or q==num+8 or q==num-10 or q==num+17 or q==num-19 or q==num-2 or q==num-3 or q==num+6 or q==num-12:
each.nature = 0
each.draw(red)
q+=1
else :
each.draw(black)
q+=1
if y == 2 :
if q==num or q==num-1 or q==num+8 or q==num-10 or q==num+17 or q==num-19 or q==num-2:
each.nature = 0
each.draw(red)
q+=1
else :
each.draw(black)
q+=1
if y == 1 :
if q==num or q==num-1 or q==num+8 or q==num-10 or q==num+17 or q==num-19 :
each.nature = 0
each.draw(red)
q+=1
else :
each.draw(black)
q+=1
if y == 0:
if q==num :
each.nature = 0
each.draw(red)
q+=1
else :
each.draw(black)
q+=1
#绘制飞机头向左时的图案
if direction == 3:
if 5>=x>=0 and y>=2 and y<=6:
if q==num or q==num+9 or q==num+8 or q==num+10 or q==num+7 or q==num+11 or q==num+18 or q==num+27 or q==num+26 or q==num+28:
each.nature = 0
each.draw(each.color)
q+=1
if each.true == 0:
key += 1
else :
each.draw(black)
q+=1
if x>5 and y>=2 and y<=6:
if q==num or q==num+9 or q==num+8 or q==num+10 or q==num+7 or q==num+11 or q==num+18 or q==num+27 or q==num+26 or q==num+28:
each.nature = 0
each.draw(red)
q+=1
else :
each.draw(black)
q+=1
if y==1:
if q==num or q==num+9 or q==num+8 or q==num+10or q==num+11 or q==num+18 or q==num+27 or q==num+26 or q==num+28:
each.nature = 0
each.draw(red)
q+=1
else :
each.draw(black)
q+=1
if y==0:
if q==num or q==num+9or q==num+10or q==num+11 or q==num+18 or q==num+27 or q==num+28:
each.nature = 0
each.draw(red)
q+=1
else :
each.draw(black)
q+=1
if y==7:
if q==num or q==num+9 or q==num+8 or q==num+10 or q==num+7or q==num+18 or q==num+27 or q==num+26 or q==num+28:
each.nature = 0
each.draw(red)
q+=1
else :
each.draw(black)
q+=1
if y>=8:
if q==num or q==num+9 or q==num+8 or q==num+7or q==num+18 or q==num+27 or q==num+26:
each.nature = 0
each.draw(red)
q+=1
else :
each.draw(black)
q+=1
if event.type == MOUSEBUTTONDOWN and event.button == 1:
if key == 10 and points<3:
q = 0
for each in self.map:
if direction == 0:
if q==num+1 or q==num-8 or q==num+10 or q==num-17 or q==num+19 or q==num+2 or q==num+3 or q==num-6 or q==num+12:
each.true = 1
each.color = color
if q == num:
each.true = 2
each.color = color
q += 1
if direction == 1:
if q==num-9 or q==num-8 or q==num-10 or q==num-7 or q==num-11 or q==num-18 or q==num-27 or q==num-26 or q==num-28:
each.true = 1
each.color = color
if q == num:
each.true = 2
each.color = color
q += 1
if direction == 2:
ifq==num-1 or q==num+8 or q==num-10 or q==num+17 or q==num-19 or q==num-2 or q==num-3 or q==num+6 or q==num-12:
each.true = 1
each.color = color
if q == num:
each.true = 2
each.color = color
q += 1
if direction == 3:
ifq==num+9 or q==num+8 or q==num+10 or q==num+7 or q==num+11 or q==num+18 or q==num+27 or q==num+26 or q==num+28:
each.true = 1
each.color = color
if q == num:
each.true = 2
each.color = color
q += 1
points += 1
screen.blit(back,back_rect)
screen.blit(lay,lay_rect)
#互相攻击函数
def attark(self) :
global num,turn,giveup,giveup1,giveup2
giveup_rect = giveup1.get_rect()
giveup_rect.left,giveup_rect.top = 0,450
clock = pygame.time.Clock()
time = False
screen.fill(white)
t = 0
for this in self.map :
if t == num :
this.nature = 0
this.redraw(self.set)
else :
this.redraw(self.set)
t += 1
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == MOUSEMOTION:
if giveup_rect.collidepoint(event.pos):
giveup = giveup2
else:
giveup = giveup1
if event.type == MOUSEBUTTONDOWN and giveup_rect.collidepoint(event.pos):
self.score = -100
turn += 1
if event.type == MOUSEMOTION or event.type == MOUSEBUTTONDOWN :
pos = pygame.mouse.get_pos()
x = pos//50
y = pos//50
if y==9:
num = -100
else :
num = 9*x+y
if event.type == MOUSEBUTTONDOWN and event.button == 1:
q = 0
f = 0
for this in self.map:
if q == num and this.judge==0:
this.judge = 1
f = 1
this.clean()
this.redraw(self.set)
pygame.display.flip()
if this.true == 2:
self.score += 1
q += 1
if f == 1:
time = True
if event.type == MOUSEBUTTONDOWN and event.button == 3:
q=0
for this in self.map:
if q == num :
a = this
break
q += 1
if a.judge == 0 :
a.judge = 2
a.redraw(self.set)
pygame.display.flip()
elif a.judge == 2 :
a.judge = 0
a.redraw(self.set)
pygame.display.flip()
#延时,在攻击后不会快速跳转到下一界面
screen.blit(giveup,giveup_rect)
if time :
for this in self.map :
if t == num :
this.nature = 0
this.redraw(self.set)
else :
this.redraw(self.set)
t += 1
turn += 1
time = False
clock.tick(1)
#展示棋盘函数,用于游戏结束后观看棋盘
def show(self):
for this in self.map:
this.draw(black)
this.redraw(self.set)
#电脑摆放飞机函数,算法是随机数,所以电脑的飞机是随机摆放的,并无规律可循
def computer_make_plane(self):
direction = randint(0,3)
x = randint(0,8)
y = randint(0,8)
global points,cishu
cishu += 1
color=
if y==9:
num = -100
else :
num = 9*x+y
q = 0
key = 0
for each in self.map:
if direction == 0:
if y<6 and x>=2 and x<=6:
if q==num or q==num+1 or q==num-8 or q==num+10 or q==num-17 or q==num+19 or q==num+2 or q==num+3 or q==num-6 or q==num+12:
if each.true == 0:
key += 1
if direction == 1:
if 8>=x>=3 and y>=2 and y<=6:
if q==num or q==num-9 or q==num-8 or q==num-10 or q==num-7 or q==num-11 or q==num-18 or q==num-27 or q==num-26 or q==num-28:
if each.true == 0:
key += 1
if direction == 2:
if 2<=x<=6 and y>=3:
if q==num or q==num-1 or q==num+8 or q==num-10 or q==num+17 or q==num-19 or q==num-2 or q==num-3 or q==num+6 or q==num-12:
if each.true == 0:
key += 1
if direction == 3:
if 5>=x>=0 and y>=2 and y<=6:
if q==num or q==num+9 or q==num+8 or q==num+10 or q==num+7 or q==num+11 or q==num+18 or q==num+27 or q==num+26 or q==num+28:
if each.true == 0:
key += 1
q += 1
if key == 10 and points<3:
q = 0
for each in self.map:
if direction == 0:
if q==num+1 or q==num-8 or q==num+10 or q==num-17 or q==num+19 or q==num+2 or q==num+3 or q==num-6 or q==num+12:
each.true = 1
each.color = color
if q == num:
each.true = 2
each.color = color
q += 1
if direction == 1:
if q==num-9 or q==num-8 or q==num-10 or q==num-7 or q==num-11 or q==num-18 or q==num-27 or q==num-26 or q==num-28:
each.true = 1
each.color = color
if q == num:
each.true = 2
each.color = color
q += 1
if direction == 2:
ifq==num-1 or q==num+8 or q==num-10 or q==num+17 or q==num-19 or q==num-2 or q==num-3 or q==num+6 or q==num-12:
each.true = 1
each.color = color
if q == num:
each.true = 2
each.color = color
q += 1
if direction == 3:
ifq==num+9 or q==num+8 or q==num+10 or q==num+7 or q==num+11 or q==num+18 or q==num+27 or q==num+26 or q==num+28:
each.true = 1
each.color = color
if q == num:
each.true = 2
each.color = color
q += 1
points += 1
cishu = 0
#计数器,如果超过1000次无法成功摆放飞机,就证明当前无法放下第三架飞机,清除数据,重新摆放飞机(如果没有这个有可能陷入死循环,因为9*9格子中无法确保三架飞机无论怎么摆放都能放得下)
if cishu > 1000:
points = 0
for each in self.map:
each.true = 0
each.color = self.color
#电脑攻击函数,算法为随机数,就是说此方法下电脑并不具有智能,只会随机在棋盘上进行轰炸。
def computer_attark(self):
global turn
t = turn
while turn == t :
x = randint(0,8)
y = randint(0,8)
if y==9:
num = -100
else :
num = 9*x+y
clock = pygame.time.Clock()
time = False
screen.fill(white)
for this in self.map :
this.redraw(self.set)
q = 0
f = 0
for this in self.map:
if q == num and this.judge==0:
this.judge = 1
f = 1
this.clean()
this.redraw(self.set)
pygame.display.flip()
if this.true == 2:
self.score += 1
q += 1
if f == 1:
time = True
if time :
for this in self.map :
this.redraw(self.set)
turn += 1
time = False
clock.tick(1)
def computer_attark_main(self):
global turn,body,miss
andm =
andm= *81
pop = []
for each in final:
gi = 0
fi = 0
for im in range(81):
if self.map.judge == 1:
gi += 1
if self.map.true == each:
fi += 1
if gi == fi :
pop.append(each)
for each in pop:
inti = 0
for this in each:
if this == 2:
andm += 100
if this == 1:
andm += body
if this == 0:
andm -= miss
inti += 1
a = max(andm)
num = andm.index(a)
while self.map.judge == 1:
andm = 0
a = max(andm)
num = andm.index(a)
if turn == 2:
it = randint(0,3)
numku =
num = numku
clock = pygame.time.Clock()
time = False
screen.fill(white)
for this in self.map :
this.redraw(self.set)
q = 0
f = 0
for this in self.map:
if q == num and this.judge==0:
this.judge = 1
f = 1
this.clean()
this.redraw(self.set)
pygame.display.flip()
if this.true == 2:
self.score += 1
q += 1
if f == 1:
time = True
if time :
for this in self.map :
this.redraw(self.set)
turn += 1
time = False
clock.tick(1)
'''调试函数,求出不同加权值下电脑主要攻击算法赢得胜利所需的
平均步数
'''
def tiaoshi(self,body,miss):
global turn
andm =
andm= *81
pop = []
for each in final:
gi = 0
fi = 0
for im in range(81):
if self.map.judge == 1:
gi += 1
if self.map.true == each:
fi += 1
if gi == fi :
pop.append(each)
for each in pop:
inti = 0
for this in each:
if this == 2:
andm += 100
if this == 1:
andm += body
if this == 0:
andm -= miss
inti += 1
a = max(andm)
num = andm.index(a)
while self.map.judge == 1:
andm = 0
a = max(andm)
num = andm.index(a)
q = 0
for this in self.map:
if q == num and this.judge==0:
this.judge = 1
if this.true == 2:
self.score += 1
q += 1
#导入电脑主要算法资源,将所有飞机摆放情况map从文件提取出来,并存储到列表final中
wenjian = open("map.txt",'rb')
final = pickle.load(wenjian)
wenjian.close()
另两个模块主要用于调试,暂时不附上了。
截图和全部源码链接如下:链接:https://pan.baidu.com/s/1U6oeCqEqiXZb3Kqcsjohhw
提取码:zf00
萌新初来乍到,感谢您的支持和鼓励!
如有不足之处,敬请指正!
(上次技术原因,仅附上了一个链接,在此道歉,此次编辑,望多多支持!) 可以图床上传网络图片:
打开这个网址上传路过图床
拷贝图中黑框位置的网址
切回论坛,在高级编辑模式里点[图片] -> [网络图片]
宽高可不填,只填刚才拷贝的网址就行
我真的是个好人 发表于 2020-7-20 10:54
可以图床上传网络图片:
打开这个网址上传路过图床
拷贝图中黑框位置的网址
学到了,十分感谢 昨非 发表于 2020-7-20 11:22
学到了,十分感谢
{:10_323:} {:10_257:}厉害 谢谢,一起加油吧 新作了编辑,代码很长,也比较乱,见谅{:10_245:} 你管这叫“萌新”????{:10_284:} 别骂了别骂了{:10_285:} 这。。。那么长 厉害了 ,一脸懵路过 jekin3017 发表于 2020-8-16 14:26
这。。。那么长 厉害了 ,一脸懵路过
其实我自己冷不丁再看也是一脸懵逼(小声){:10_245:} 再试一次{:10_297:} 哇塞大佬 {:10_266:}还不中 开心小傻猪 发表于 2020-8-18 17:04
还不中
兄弟你想鱼币想疯了吧,,,这儿没回帖奖励{:10_256:} 昨非 发表于 2020-8-18 17:07
兄弟你想鱼币想疯了吧,,,这儿没回帖奖励
啊,尴尬{:10_245:} 昨非 发表于 2020-7-20 11:22
学到了,十分感谢
https://imgchr.com/i/d89MjI 活力小子 发表于 2020-8-20 11:29
https://s1.ax1x.com/2020/08/20/d89MjI.png 活力小子 发表于 2020-8-20 11:30
嗯哼? 昨非 发表于 2020-8-20 11:47
嗯哼?
学会了,感谢!
页:
[1]
2