使用Python写坦克大战

阅读: 评论:0

使用Python写坦克大战

使用Python写坦克大战

坦克大战这个游戏相信是一代人的回忆吧,话不多说,这次先上代码
依旧是老样子

Python源码、学习、问题解决
4656 885 91
(备注:苏)

我们先定义这次代码所需要的模块

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:d([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 Truereturn False

定义绘制战场

def draw_battle_field():global symbol_positionglobal symbol_areafor row_index in range(y_max):for column_index in range(x_max):if battle_field[row_index][column_index] == 1:# is a brick_wallscreen.blit(brick_wall_img, (column_index * 30, row_index * 30))if battle_field[row_index][column_index] == 2:# is a cement_wallscreen.blit(cement_wall_img, (column_index * 30, row_index * 30))if symbol_position != None:continueif battle_field[row_index][column_index] == 3:# is a symbolsymbol_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_productglobal enemys_cur_numberif last_product != -1 and time - last_product < enemys_product_interval:returnindex_e = random.randint(0, 1)conflict = Falsefor point in tank.area:if point in enemy_init_area[index_e]:conflict = Truebreakif not conflict:for enemy in enemys:for point_e in enemy.area:if point_e in enemy_init_area[index_e]:conflict = Truebreakif conflict:break;if not conflict:enemys.append(Enemy(enemy_init_position[index_e]))last_product = timeenemys_cur_number += 1returnfor point in tank.area:if point in enemy_init_area[1 - index_e]:returnfor enemy in enemys:for point_e in enemy.area:if point_e in enemy_init_area[1 - index_e]:returnenemys.append(Enemy(enemy_init_position[1 - index_e]))last_product = timeenemys_cur_number += 1

装甲车

class ArmoredCar():def __init__(self, p_position, p_direction, p_image, p_fire_interval):self.position = p_positionself.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_directionself.image = p_image self.missiles = []self.destroyed = Falseself.last_fire = -1self.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 Falseif battle_field[y][x] != 0:return Falsefor enemy in enemys:if enemy == self:continueif area_conflict(enemy.area, new_area):return Falseif isinstance(self, Enemy) and area_conflict(self.area, tank.area):return Falsereturn Truedef update(self):self.draw()index = 0for missile in self.missiles:if missile.update() == False:self.missiles.pop(index)index += 1def up(self):self.direction = 'U'if isinstance(self, Tank):self.image = tank_img_Uelse: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] 

本文发布于:2024-02-02 00:12:53,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170680968640096.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:坦克   大战   Python
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23