当前位置:   article > 正文

Python实现坦克大战——源代码

Python实现坦克大战——源代码

**

Python实现坦克大战——源代码

**

一、环境:pygame
Pygame被设计用来写游戏的python模块集合,基于SDL库开发。使用python可以导入pygame来开发具有全部特性的游戏和多媒体软件,Pygame是极度轻便的并且可以运行在几乎所有的平台和操作系统上。

二、编辑器:PyCharm
PyCharm是一种Python IDE,带有一整套可以帮助用户在使用Python语言开发时提高其效率的工具,比如调试、语法高亮、Project管理、代码跳转、智能提示、自动完成、单元测试、版本控制。此外,该IDE提供了一些高级功能,以用于支持Django框架下的专业Web开发。

源代码:

import pygame
from pygame.locals import *
import math
import random

def expand_battle_field():
    for row in small_battle_field:
        new_row = []
        for column in row:
            new_row.extend([column] * 2)
        battle_field.append(new_row)
        battle_field.append(new_row[:])

def area_conflict(area1, area2):
    for point1 in area1:
        if point1 in area2:
            return True
    return False

def draw_battle_field():
    global symbol_position
    global symbol_area
    for row_index in range(y_max):
        for column_index in range(x_max):
            if battle_field[row_index][column_index] == 1:
                # is a brick_wall
                screen.blit(brick_wall_img, (column_index * 30, row_index * 30))
            if battle_field[row_index][column_index] == 2:
                # is a cement_wall
                screen.blit(cement_wall_img, (column_index * 30, row_index * 30))
            if symbol_position != None:
                continue
            if battle_field[row_index][column_index] == 3:
                # is a symbol
                symbol_position = (column_index, row_index)
                symbol_area = (
                        (column_index, row_index),
                        (column_index + 1, row_index),
                        (column_index, row_index + 1),
                        (column_index + 1, row_index + 1))
    if game_over:
        screen.blit(symbol_destoryed_img, (symbol_position[0] * 30, symbol_position[1] * 30))
    else:
        screen.blit(symbol_img, (symbol_position[0] * 30, symbol_position[1] * 30))

def produce_enemy(time):
    global last_product
    global enemys_cur_number
    if last_product != -1 and time - last_product < enemys_product_interval:
        return
    index_e = random.randint(0, 1)
    conflict = False
    for point in tank.area:
        if point in enemy_init_area[index_e]:
            conflict = True
            break

    if not conflict:
        for enemy in enemys:
            for point_e in enemy.area:
                if point_e in enemy_init_area[index_e]:
                    conflict = True
                    break
            if conflict:
                break;

    if not conflict:
        enemys.append(Enemy(enemy_init_position[index_e]))
        last_product = time
        enemys_cur_number += 1
        return

    for point in tank.area:
        if point in enemy_init_area[1 - index_e]:
            return

    for enemy in enemys:
        for point_e in enemy.area:
            if point_e in enemy_init_area[1 - index_e]:
                return

    enemys.append(Enemy(enemy_init_position[1 - index_e]))
    last_product = time
    enemys_cur_number += 1

class ArmoredCar():
    def __init__(self, p_position, p_direction, p_image, p_fire_interval):
        self.position = p_position
        self.area = (
            (self.position[0], self.position[1]),
            (self.position[0] + 1, self.position[1]),
            (self.position[0], self.position[1] + 1),
            (self.position[0] + 1, self.position[1] + 1))
        self.direction = p_direction
        self.image = p_image 
        self.missiles = []

        self.destroyed = False

        self.last_fire = -1
        self.fire_interval = p_fire_interval 

    def draw(self):
        screen.blit(self.image, (self.position[0] * 30, self.position[1] * 30))

    def is_legal(self, new_area):
        for (x, y) in new_area:
            if x < 0 or y < 0 or x >= x_max or y >= y_max:
                return False
            if battle_field[y][x] != 0:
                return False

        for enemy in enemys:
            if enemy == self:
                continue
            if area_conflict(enemy.area, new_area):
                return False
        if isinstance(self, Enemy) and area_conflict(self.area, tank.area):
            return False

        return True

    def update(self):
        self.draw()
        index = 0
        for missile in self.missiles:
            if missile.update() == False:
                self.missiles.pop(index)
            index += 1

    def up(self):
        self.direction = 'U'
        if isinstance(self, Tank):
            self.image = tank_img_U
        else:
            self.image = enemy_img_U 
        new_position = (self.position[0], self.position[1] - 1)
        new_area = (
            (new_position[0], new_position[1]),
            (new_position[0] + 1, new_position[1]),
            (new_position[0], new_position[1] + 1),
            (new_position[0] + 1, new_position[1] + 1)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/573044
推荐阅读
相关标签
  

闽ICP备14008679号