Battleship-摆放部分 逻辑

import os

import pygame
from pygame.locals import *
import time


def ship_drawing(ship_img, size, position, alpha: 0 - 255 = '255', color='blue', offset=True):

    pos_x, pos_y = position
    vertical_adjustment, horizontal_adjustment = size
    if offset is True:
        pos_x = pos_x - vertical_adjustment/2
        pos_y = pos_y - horizontal_adjustment/2

    if color == 'blue':
        # 半透明效果参数 alpha:0-255 数字越大越透明
        transparent_surface = pygame.Surface(size, pygame.SRCALPHA)
        transparent_surface.fill((255, 255, 255, alpha))
        transparent_surface.blit(ship_img, (pos_x, pos_y))
        screen.blit(ship_img, (pos_x, pos_y))
        screen.blit(transparent_surface, (pos_x, pos_y))





pygame.init()

myfont = pygame.font.Font(r"C:\\Users\\Admin\venv\\Scripts\Fonts\\SIMLI.TTF", 25)
screen = pygame.display.set_mode((1024, 768))

exs_pos = []
draw_list_temp = []
draw_list = []
ship_choose = ''
mouse_x = mouse_y = 0
move_flag = 0
draw_flag = 0
stop_flag = 0
hand = 0
ship_counts = {'11b': 4, '21b': 3, '31b': 2, '41b': 1}
ship_counts_2 = {'112b': 4, '212b': 3, '312b': 2, '412b': 1}

i = j = 50
ship_11_b_pos = (750, 100)
ship_21_b_pos = (750, 140)
ship_31_b_pos = (750, 180)
ship_41_b_pos = (750, 220)

list_11b = []
list_21b = []
list_31b = []
list_41b = []
list_212b = []
list_312b = []
list_412b = []
for x in range(75, 426, 50):
    for y in range(70, 351, 40):
        list_11b.append((x, y))
for x in range(100, 401, 50):
    for y in range(70, 351, 40):
        list_21b.append((x, y))
for x in range(125, 376, 50):
    for y in range(70, 351, 40):
        list_31b.append((x, y))
for x in range(150, 351, 50):
    for y in range(70, 351, 40):
        list_41b.append((x, y))
for x in range(75, 426, 50):
    for y in range(90, 331, 40):
        list_212b.append((x, y))
for x in range(75, 426, 50):
    for y in range(110, 311, 40):
        list_312b.append((x, y))
for x in range(75, 426, 50):
    for y in range(130, 291, 40):
        list_412b.append((x, y))

pygame.display.set_caption('海战棋')

ship_11_b = pygame.image.load("blue_11.png").convert_alpha()
ship_11_b_size = ship_11_b.get_size()
ship_21_b = pygame.image.load("blue_21.png").convert_alpha()
ship_21_b_size = ship_21_b.get_size()
ship_31_b = pygame.image.load("blue_31.png").convert_alpha()
ship_31_b_size = ship_31_b.get_size()
ship_41_b = pygame.image.load("blue_41.png").convert_alpha()
ship_41_b_size = ship_41_b.get_size()
ship_112_b = pygame.image.load("blue_11_2.png").convert_alpha()
ship_112_b_size = ship_112_b.get_size()
ship_212_b = pygame.image.load("blue_21_2.png").convert_alpha()
ship_212_b_size = ship_212_b.get_size()
ship_312_b = pygame.image.load("blue_31_2.png").convert_alpha()
ship_312_b_size = ship_312_b.get_size()
ship_412_b = pygame.image.load("blue_41_2.png").convert_alpha()
ship_412_b_size = ship_412_b.get_size()


while True:
    for event in pygame.event.get():
        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                pygame.quit()
            elif event.key == K_SPACE:
                if hand == 1 and move_flag == 1:
                    ship_mapping = {
                        '11b': '112b',
                        '112b': '11b',
                        '21b': '212b',
                        '212b': '21b',
                        '31b': '312b',
                        '312b': '31b',
                        '412b': '41b',
                        '41b': '412b'
                    }
                    if ship_choose in ship_mapping:
                        ship_choose = ship_mapping[ship_choose]
            elif event.key == K_r:
                exs_pos = []
                draw_list = []
                ship_choose = ''
                mouse_x = mouse_y = 0
                move_flag = 0
                draw_flag = 0
                stop_flag = 0
                hand = 0
                ship_counts = {'11b': 4, '21b': 3, '31b': 2, '41b': 1}
                ship_counts_2 = {'112b': 4, '212b': 3, '312b': 2, '412b': 1}
        if event.type == MOUSEBUTTONDOWN:
            mouse_down_x, mouse_down_y = event.pos
            if mouse_down_x in range(165, 261) and mouse_down_y in range(600, 651) and hand == 0:
                exs_pos = []
                draw_list = []
                ship_choose = ''
                mouse_x = mouse_y = 0
                move_flag = 0
                draw_flag = 0
                stop_flag = 0
                hand = 0
                ship_counts = {'11b': 4, '21b': 3, '31b': 2, '41b': 1}
                ship_counts_2 = {'112b': 4, '212b': 3, '312b': 2, '412b': 1}
            if (mouse_down_x in range(310, 406) and mouse_down_y in range(600, 651) and hand == 0
                    and not bool(ship_counts['11b'] + ship_counts['21b'] + ship_counts['31b'] + ship_counts['41b'])):
                draw_list_temp = draw_list
                draw_list = []
                ship_choose = ''
                mouse_x = mouse_y = 0
                move_flag = 0
                draw_flag = 0
                stop_flag = 0
                hand = 0
            if mouse_down_x in range(750, 846) and mouse_down_y in range(600, 651) and hand == 0:
                pygame.quit()
            if hand == 0:
                if (mouse_down_x in range(750, 801) and mouse_down_y in range(100, 141)
                        and ship_counts['11b'] > 0 and ship_counts_2['112b'] >0):
                    move_flag = 1
                    hand = 1
                    ship_choose = '11b'
                elif (mouse_down_x in range(750, 851) and mouse_down_y in range(140, 181)
                      and ship_counts['21b'] > 0 and ship_counts_2['212b'] >0):
                    move_flag = 1
                    hand = 1
                    ship_choose = '21b'
                elif (mouse_down_x in range(750, 901) and mouse_down_y in range(180, 221)
                      and ship_counts['31b'] > 0 and ship_counts_2['312b'] >0):
                    move_flag = 1
                    hand = 1
                    ship_choose = '31b'
                elif (mouse_down_x in range(750, 951) and mouse_down_y in range(220, 261)
                      and ship_counts['41b'] > 0 and ship_counts_2['412b'] >0):
                    move_flag = 1
                    hand = 1
                    ship_choose = '41b'
            # 当鼠标上有船时
            elif hand == 1:
                if mouse_down_x in range(50, 451) and mouse_down_y in range(50, 371):
                    if draw_flag == 1:
                        mouse_x, mouse_y = draw_pos_centre
                        if ship_choose in ('11b','112b'):
                            for pos_store in exs_pos:
                                if (mouse_x, mouse_y) == pos_store:
                                    stop_flag = 1
                                    break
                            else:
                                exs_pos.append((mouse_x, mouse_y))
                                draw_list.append((mouse_x, mouse_y, ship_choose))
                                # print(exs_pos)
                        elif ship_choose in ('21b','212b'):
                            for pos_store in exs_pos:
                                if ship_choose == '21b':
                                    if pos_store in ((mouse_x - 25, mouse_y), (mouse_x + 25, mouse_y)):
                                        stop_flag = 1
                                        break
                                elif ship_choose == '212b':
                                    if pos_store in ((mouse_x, mouse_y - 20), (mouse_x, mouse_y + 20)):
                                        stop_flag = 1
                                        break
                            else:
                                if ship_choose == '21b':
                                    exs_pos.append((mouse_x - 25, mouse_y))
                                    exs_pos.append((mouse_x + 25, mouse_y))
                                    draw_list.append((mouse_x, mouse_y, ship_choose))
                                elif ship_choose == '212b':
                                    exs_pos.append((mouse_x, mouse_y - 20))
                                    exs_pos.append((mouse_x, mouse_y + 20))
                                    draw_list.append((mouse_x, mouse_y, ship_choose))
                                    # print(exs_pos)
                        elif ship_choose in ('31b','312b'):
                            for pos_store in exs_pos:
                                if ship_choose == '31b':
                                    if pos_store in ((mouse_x, mouse_y), (mouse_x - 50, mouse_y)
                                                     , (mouse_x + 50, mouse_y)):
                                        stop_flag = 1
                                        break
                                if ship_choose == '312b':
                                    if pos_store in ((mouse_x, mouse_y), (mouse_x, mouse_y - 40)
                                                     , (mouse_x, mouse_y + 40)):
                                        stop_flag = 1
                                        break
                            else:
                                if ship_choose == '31b':
                                    exs_pos.append((mouse_x, mouse_y))
                                    exs_pos.append((mouse_x - 50, mouse_y))
                                    exs_pos.append((mouse_x + 50, mouse_y))
                                    draw_list.append((mouse_x, mouse_y, ship_choose))
                                if ship_choose == '312b':
                                    exs_pos.append((mouse_x, mouse_y))
                                    exs_pos.append((mouse_x, mouse_y - 40))
                                    exs_pos.append((mouse_x, mouse_y + 40))
                                    draw_list.append((mouse_x, mouse_y, ship_choose))
                                # print(exs_pos)
                        elif ship_choose in ('41b','412b'):
                            for pos_store in exs_pos:
                                if ship_choose == '41b':
                                    if pos_store in ((mouse_x - 25, mouse_y), (mouse_x + 25, mouse_y)
                                                     , (mouse_x - 75, mouse_y), (mouse_x + 75, mouse_y)):
                                        stop_flag = 1
                                        break
                                elif ship_choose == '412b':
                                    if pos_store in ((mouse_x, mouse_y - 20), (mouse_x, mouse_y + 20)
                                                     , (mouse_x, mouse_y - 60), (mouse_x, mouse_y + 60)):
                                        stop_flag = 1
                                        break
                            else:
                                if ship_choose == '41b':
                                    exs_pos.append((mouse_x - 25, mouse_y))
                                    exs_pos.append((mouse_x + 25, mouse_y))
                                    exs_pos.append((mouse_x - 75, mouse_y))
                                    exs_pos.append((mouse_x + 75, mouse_y))
                                    draw_list.append((mouse_x, mouse_y, ship_choose))
                                if ship_choose == '412b':
                                    exs_pos.append((mouse_x, mouse_y - 20))
                                    exs_pos.append((mouse_x, mouse_y + 20))
                                    exs_pos.append((mouse_x, mouse_y - 60))
                                    exs_pos.append((mouse_x, mouse_y + 60))
                                    draw_list.append((mouse_x, mouse_y, ship_choose))
                                # print(exs_pos)
                        if stop_flag == 0:
                            move_flag = 0
                            hand = 0
                            draw_flag = 0
                            # 根据ship_choose来更新船只数量
                            if ship_choose in ship_counts:
                                ship_counts[ship_choose] -= 1
                                ship_counts_2[f"{ship_choose[:2]}2{ship_choose[2:]}"] -= 1
                            elif ship_choose in ship_counts_2:
                                ship_counts_2[ship_choose] -= 1
                                ship_counts[ship_choose[:2] + ship_choose[3:]] -= 1
                        elif stop_flag == 1:
                            draw_flag = 0
                            stop_flag = 0
            print(mouse_down_x, mouse_down_y)
            print(draw_list)
            print(exs_pos)
        if event.type == MOUSEMOTION:
            mouse_x, mouse_y = event.pos
            move_x, move_y = event.rel
    # 清屏
    screen.fill((255, 255, 255))
    # 绘制船的常规样例
    ship_drawing(ship_11_b, ship_11_b_size, ship_11_b_pos, 0, 'blue', False)
    ship_drawing(ship_21_b, ship_21_b_size, ship_21_b_pos, 0, 'blue', False)
    ship_drawing(ship_31_b, ship_31_b_size, ship_31_b_pos, 0, 'blue', False)
    ship_drawing(ship_41_b, ship_41_b_size, ship_41_b_pos, 0, 'blue', False)
    # 绘制点击后的样例
    if move_flag == 1:
        if ship_choose == '11b':
            ship_drawing(ship_11_b, ship_11_b_size, ship_11_b_pos, 128, 'blue', False)
        elif ship_choose == '21b':
            ship_drawing(ship_21_b, ship_21_b_size, ship_21_b_pos, 128, 'blue', False)
        elif ship_choose == '31b':
            ship_drawing(ship_31_b, ship_31_b_size, ship_31_b_pos, 128, 'blue', False)
        elif ship_choose == '41b':
            ship_drawing(ship_41_b, ship_41_b_size, ship_41_b_pos, 128, 'blue', False)
    # 绘制样例外框
    pygame.draw.rect(screen, (0, 0, 0), (750, 100, 250, 40), 1)
    pygame.draw.rect(screen, (0, 0, 0), (750, 140, 250, 40), 1)
    pygame.draw.rect(screen, (0, 0, 0), (750, 180, 250, 40), 1)
    pygame.draw.rect(screen, (0, 0, 0), (750, 220, 250, 40), 1)
    # 绘制重置船只、摆放完成、退出按钮外框
    pygame.draw.rect(screen, (0, 0, 0), (165, 600, 95, 50), 1)
    pygame.draw.rect(screen, (0, 0, 0), (310, 600, 95, 50), 1)
    pygame.draw.rect(screen, (0, 0, 0), (750, 600, 95, 50), 1)
    textImage_k1 = myfont.render(f'重置船只', True, (0, 0, 0))
    textImage_k2 = myfont.render(f'摆放完成', True, (0, 0, 0))
    textImage_k3 = myfont.render(f'退出', True, (0, 0, 0))
    screen.blit(textImage_k1, (165, 610))
    screen.blit(textImage_k2, (310, 610))
    screen.blit(textImage_k3, (770, 610))
    # 绘制船的剩余数目显示
    textImage_k1 = myfont.render(f"{ship_counts['11b']}", True, (0, 0, 0))
    textImage_k2 = myfont.render(f"{ship_counts['21b']}", True, (0, 0, 0))
    textImage_k3 = myfont.render(f"{ship_counts['31b']}", True, (0, 0, 0))
    textImage_k4 = myfont.render(f"{ship_counts['41b']}", True, (0, 0, 0))
    screen.blit(textImage_k1, (970, 110))
    screen.blit(textImage_k2, (970, 150))
    screen.blit(textImage_k3, (970, 190))
    screen.blit(textImage_k4, (970, 230))
    # 绘制棋盘
    for i in range(50, 451, 50):
        for j in range(50, 371, 40):
            pygame.draw.line(screen, (0, 0, 0), (i, j), (i, 370), 1)
            pygame.draw.line(screen, (0, 0, 0), (i, j), (450, j), 1)
    # 绘制已部署的船
    for value in draw_list:
        if value[2] == '11b':
            ship_drawing(ship_11_b, ship_11_b_size, (value[0], value[1]), 0, 'blue')
        elif value[2] == '21b':
            ship_drawing(ship_21_b, ship_21_b_size, (value[0], value[1]), 0, 'blue')
        elif value[2] == '31b':
            ship_drawing(ship_31_b, ship_31_b_size, (value[0], value[1]), 0, 'blue')
        elif value[2] == '41b':
            ship_drawing(ship_41_b, ship_41_b_size, (value[0], value[1]), 0, 'blue')
        if value[2] == '112b':
            ship_drawing(ship_112_b, ship_112_b_size, (value[0], value[1]), 0, 'blue')
        elif value[2] == '212b':
            ship_drawing(ship_212_b, ship_212_b_size, (value[0], value[1]), 0, 'blue')
        elif value[2] == '312b':
            ship_drawing(ship_312_b, ship_312_b_size, (value[0], value[1]), 0, 'blue')
        elif value[2] == '412b':
            ship_drawing(ship_412_b, ship_412_b_size, (value[0], value[1]), 0, 'blue')
    # 移动时绘制轨迹船,绘制船的摆放预览图
    if move_flag == 1:
        position_m = (mouse_x, mouse_y)
        if ship_choose == '11b':
            ship_drawing(ship_11_b, ship_11_b_size, position_m,0, 'blue')
            if mouse_x in range(50, 451) and mouse_y in range(50, 371):
                pos_ajd_x = (mouse_x-75) // 25 * 25 + 75
                pos_ajd_y = (mouse_y-70) // 20 * 20 + 70
                draw_flag = 0
                for pos_ajd in list_11b:
                    if (pos_ajd_x, pos_ajd_y) == pos_ajd:
                        ship_drawing(ship_11_b, ship_11_b_size, pos_ajd, 128, 'blue')
                        draw_pos_centre = pos_ajd
                        draw_flag = 1
                        continue
        elif ship_choose == '21b':
            ship_drawing(ship_21_b, ship_21_b_size, position_m,0, 'blue')
            if mouse_x in range(75, 426) and mouse_y in range(50, 371):
                pos_ajd_x = (mouse_x-100) // 25 * 25 + 100
                pos_ajd_y = (mouse_y-70) // 20 * 20 + 70
                draw_flag = 0
                for pos_ajd in list_21b:
                    if (pos_ajd_x, pos_ajd_y) == pos_ajd:
                        ship_drawing(ship_21_b, ship_21_b_size, pos_ajd, 128, 'blue')
                        draw_pos_centre = pos_ajd
                        draw_flag = 1
                        continue
        elif ship_choose == '31b':
            ship_drawing(ship_31_b, ship_31_b_size, position_m,0, 'blue')
            if mouse_x in range(100, 401) and mouse_y in range(50, 371):
                pos_ajd_x = (mouse_x-125) // 25 * 25 + 125
                pos_ajd_y = (mouse_y-70) // 20 * 20 + 70
                draw_flag = 0
                for pos_ajd in list_31b:
                    if (pos_ajd_x, pos_ajd_y) == pos_ajd:
                        ship_drawing(ship_31_b, ship_31_b_size, pos_ajd, 128, 'blue')
                        draw_pos_centre = pos_ajd
                        draw_flag = 1
                        continue
        elif ship_choose == '41b':
            ship_drawing(ship_41_b, ship_41_b_size, position_m,0, 'blue')
            if mouse_x in range(125, 376) and mouse_y in range(50, 371):
                pos_ajd_x = (mouse_x-150) // 25 * 25 + 150
                pos_ajd_y = (mouse_y-70) // 20 * 20 + 70
                draw_flag = 0
                for pos_ajd in list_41b:
                    if (pos_ajd_x, pos_ajd_y) == pos_ajd:
                        ship_drawing(ship_41_b, ship_41_b_size, pos_ajd, 128, 'blue')
                        draw_pos_centre = pos_ajd
                        draw_flag = 1
                        continue
        elif ship_choose == '112b':
            ship_drawing(ship_112_b, ship_112_b_size, position_m,0, 'blue')
            if mouse_x in range(50, 451) and mouse_y in range(50, 371):
                pos_ajd_x = (mouse_x-75) // 25 * 25 + 75
                pos_ajd_y = (mouse_y-70) // 20 * 20 + 70
                draw_flag = 0
                for pos_ajd in list_11b:
                    if (pos_ajd_x, pos_ajd_y) == pos_ajd:
                        ship_drawing(ship_112_b, ship_112_b_size, pos_ajd, 128, 'blue')
                        draw_pos_centre = pos_ajd
                        draw_flag = 1
                        continue
        elif ship_choose == '212b':
            ship_drawing(ship_212_b, ship_212_b_size, position_m,0, 'blue')
            if mouse_x in range(50, 451) and mouse_y in range(70, 351):
                pos_ajd_x = (mouse_x-75) // 25 * 25 + 75
                pos_ajd_y = (mouse_y-90) // 20 * 20 + 90
                draw_flag = 0
                for pos_ajd in list_212b:
                    if (pos_ajd_x, pos_ajd_y) == pos_ajd:
                        ship_drawing(ship_212_b, ship_212_b_size, pos_ajd, 128, 'blue')
                        draw_pos_centre = pos_ajd
                        draw_flag = 1
                        continue
        elif ship_choose == '312b':
            ship_drawing(ship_312_b, ship_312_b_size, position_m,0, 'blue')
            if mouse_x in range(50, 451) and mouse_y in range(90, 331):
                pos_ajd_x = (mouse_x-75) // 25 * 25 + 75
                pos_ajd_y = (mouse_y-110) // 20 * 20 + 110
                draw_flag = 0
                for pos_ajd in list_312b:
                    if (pos_ajd_x, pos_ajd_y) == pos_ajd:
                        ship_drawing(ship_312_b, ship_312_b_size, pos_ajd, 128, 'blue')
                        draw_pos_centre = pos_ajd
                        draw_flag = 1
                        continue
        elif ship_choose == '412b':
            ship_drawing(ship_412_b, ship_412_b_size, position_m,0, 'blue')
            if mouse_x in range(50, 451) and mouse_y in range(110, 311):
                pos_ajd_x = (mouse_x-75) // 25 * 25 + 75
                pos_ajd_y = (mouse_y-130) // 20 * 20 + 130
                draw_flag = 0
                for pos_ajd in list_412b:
                    if (pos_ajd_x, pos_ajd_y) == pos_ajd:
                        ship_drawing(ship_412_b, ship_412_b_size, pos_ajd, 128, 'blue')
                        draw_pos_centre = pos_ajd
                        draw_flag = 1
                        continue
    # 显示画面
    pygame.display.flip()

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值