不使用pygame写一个坦克大战
时间: 2024-04-21 21:16:45 浏览: 207
好的,我可以为您提供一个简单的命令行版坦克大战。
首先,我们需要定义一个地图,可以使用二维数组表示。每个元素表示地图上的一个格子,其中0表示空地,1表示铁墙,2表示砖墙,3表示家,4表示玩家1的坦克,5表示玩家2的坦克,6表示玩家1的子弹,7表示玩家2的子弹。
接下来,我们需要定义坦克和子弹的类,以及它们的移动、射击等方法。在每个回合中,玩家可以选择移动坦克、射击、或者不进行任何操作,然后由程序计算所有坦克和子弹的移动和碰撞,并更新地图状态。游戏结束的条件可以是某个玩家的家被摧毁,或者某个玩家的坦克被摧毁。
这里是一个简单的示例代码,仅供参考:
```python
import random
# 地图大小
WIDTH = 15
HEIGHT = 15
# 地图元素
EMPTY = 0
IRON_WALL = 1
BRICK_WALL = 2
HOME = 3
PLAYER1_TANK = 4
PLAYER2_TANK = 5
PLAYER1_BULLET = 6
PLAYER2_BULLET = 7
# 坦克方向
UP = 0
RIGHT = 1
DOWN = 2
LEFT = 3
# 坦克移动速度
TANK_SPEED = 1
# 子弹速度
BULLET_SPEED = 2
class Tank:
def __init__(self, x, y, direction, player):
self.x = x
self.y = y
self.direction = direction
self.player = player
self.bullet = None
def move(self):
if self.direction == UP:
self.y -= TANK_SPEED
elif self.direction == RIGHT:
self.x += TANK_SPEED
elif self.direction == DOWN:
self.y += TANK_SPEED
elif self.direction == LEFT:
self.x -= TANK_SPEED
def shoot(self):
if self.bullet is not None:
return
if self.direction == UP:
self.bullet = Bullet(self.x, self.y-1, UP, self.player)
elif self.direction == RIGHT:
self.bullet = Bullet(self.x+1, self.y, RIGHT, self.player)
elif self.direction == DOWN:
self.bullet = Bullet(self.x, self.y+1, DOWN, self.player)
elif self.direction == LEFT:
self.bullet = Bullet(self.x-1, self.y, LEFT, self.player)
class Bullet:
def __init__(self, x, y, direction, player):
self.x = x
self.y = y
self.direction = direction
self.player = player
def move(self):
if self.direction == UP:
self.y -= BULLET_SPEED
elif self.direction == RIGHT:
self.x += BULLET_SPEED
elif self.direction == DOWN:
self.y += BULLET_SPEED
elif self.direction == LEFT:
self.x -= BULLET_SPEED
class Game:
def __init__(self):
self.map = [[0 for i in range(WIDTH)] for j in range(HEIGHT)]
self.player1 = Tank(1, 1, DOWN, 1)
self.player2 = Tank(WIDTH-2, HEIGHT-2, UP, 2)
self.player1_home = (0, HEIGHT//2)
self.player2_home = (WIDTH-1, HEIGHT//2)
self.turn = 1
self.game_over = False
# 初始化地图
for i in range(WIDTH):
self.map[0][i] = IRON_WALL
self.map[HEIGHT-1][i] = IRON_WALL
for i in range(1, HEIGHT-1):
self.map[i][0] = IRON_WALL
self.map[i][WIDTH-1] = IRON_WALL
for i in range(HEIGHT//3, HEIGHT//3*2):
self.map[i][WIDTH//2] = IRON_WALL
self.map[i][WIDTH//2-1] = BRICK_WALL
self.map[i][WIDTH//2+1] = BRICK_WALL
for i in range(3):
for j in range(3):
self.map[self.player1_home[1]+i][self.player1_home[0]+j] = HOME
self.map[self.player2_home[1]+i][self.player2_home[0]+j] = HOME
def print_map(self):
for i in range(HEIGHT):
for j in range(WIDTH):
if self.map[i][j] == EMPTY:
print(' ', end='')
elif self.map[i][j] == IRON_WALL:
print('#', end='')
elif self.map[i][j] == BRICK_WALL:
print('*', end='')
elif self.map[i][j] == HOME:
print('H', end='')
elif self.map[i][j] == PLAYER1_TANK:
print('1', end='')
elif self.map[i][j] == PLAYER2_TANK:
print('2', end='')
elif self.map[i][j] == PLAYER1_BULLET:
print('^', end='')
elif self.map[i][j] == PLAYER2_BULLET:
print('v', end='')
print()
def get_input(self):
cmd = input('Player {}\'s turn, input a command (move, shoot, or skip): '.format(self.turn)).strip().lower()
if cmd == 'move':
direction = input('Input a direction (up, right, down, or left): ').strip().lower()
if direction == 'up':
self.player_move(UP)
elif direction == 'right':
self.player_move(RIGHT)
elif direction == 'down':
self.player_move(DOWN)
elif direction == 'left':
self.player_move(LEFT)
elif cmd == 'shoot':
self.player_shoot()
elif cmd == 'skip':
pass
def player_move(self, direction):
if self.turn == 1:
tank = self.player1
else:
tank = self.player2
tank.direction = direction
if self.can_move(tank):
tank.move()
if self.turn == 1:
self.map[self.player1.y][self.player1.x] = PLAYER1_TANK
else:
self.map[self.player2.y][self.player2.x] = PLAYER2_TANK
def can_move(self, tank):
if tank.direction == UP:
x, y = tank.x, tank.y-1
elif tank.direction == RIGHT:
x, y = tank.x+1, tank.y
elif tank.direction == DOWN:
x, y = tank.x, tank.y+1
elif tank.direction == LEFT:
x, y = tank.x-1, tank.y
if x < 0 or x >= WIDTH or y < 0 or y >= HEIGHT:
return False
if self.map[y][x] in [IRON_WALL, BRICK_WALL, PLAYER1_TANK, PLAYER2_TANK]:
return False
return True
def player_shoot(self):
if self.turn == 1:
tank = self.player1
else:
tank = self.player2
tank.shoot()
if tank.bullet is not None:
if self.turn == 1:
self.map[tank.bullet.y][tank.bullet.x] = PLAYER1_BULLET
else:
self.map[tank.bullet.y][tank.bullet.x] = PLAYER2_BULLET
def update(self):
# 移动子弹
if self.player1.bullet is not None:
self.player1.bullet.move()
if self.player1.bullet.y < 0:
self.player1.bullet = None
elif self.map[self.player1.bullet.y][self.player1.bullet.x] in [IRON_WALL, BRICK_WALL, PLAYER1_TANK, PLAYER2_TANK]:
self.player1.bullet = None
if self.map[self.player1.bullet.y][self.player1.bullet.x] in [PLAYER1_TANK, PLAYER2_TANK]:
if self.turn == 1:
self.map[self.player1.y][self.player1.x] = EMPTY
self.player1 = None
self.game_over = True
else:
self.map[self.player2.y][self.player2.x] = EMPTY
self.player2 = None
self.game_over = True
elif self.map[self.player1.bullet.y][self.player1.bullet.x] == HOME:
self.game_over = True
if self.player2.bullet is not None:
self.player2.bullet.move()
if self.player2.bullet.y >= HEIGHT:
self.player2.bullet = None
elif self.map[self.player2.bullet.y][self.player2.bullet.x] in [IRON_WALL, BRICK_WALL, PLAYER1_TANK, PLAYER2_TANK]:
self.player2.bullet = None
if self.map[self.player2.bullet.y][self.player2.bullet.x] in [PLAYER1_TANK, PLAYER2_TANK]:
if self.turn == 1:
self.map[self.player1.y][self.player1.x] = EMPTY
self.player1 = None
self.game_over = True
else:
self.map[self.player2.y][self.player2.x] = EMPTY
self.player2 = None
self.game_over = True
elif self.map[self.player2.bullet.y][self.player2.bullet.x] == HOME:
self.game_over = True
# 切换回合
if not self.game_over:
if self.turn == 1:
if self.player1.bullet is None:
self.turn = 2
else:
if self.player2.bullet is None:
self.turn = 1
def run(self):
while not self.game_over:
self.print_map()
self.get_input()
self.update()
if self.player1 is None:
print('Player 2 wins!')
else:
print('Player 1 wins!')
if __name__ == '__main__':
game = Game()
game.run()
```
这个实现非常简单,只是一个命令行版的坦克大战,但是可以作为一个基础,您可以在此基础上进行更改和扩展,增加更多的功能和玩法。
阅读全文