最近想自己写pygame版的坦克大战,今晚已经完成如下功能:
1,我方坦克,可手动移动;敌方坦克,自动转方向与移动
2,坦克颜色随机,坦克形态大小可调。
3,双方坦克速度可调。
4,刷新坦克的位置随机。
5,坦克不会出界。
6,游戏窗口大小可调。
目前存在的问题:
1,表示坦克方向的列表,还未放到类里。未表示是否存活。
2,坦克会重叠(碰撞检测)。
3,炮弹类还未写。
4,……
# !/usr/bin/env python
import pygame
import random
from pygame.locals import *
pygame.init()
FPS = pygame.time.Clock()
fps = 10
screen_x = 640
screen_y = 480
done = False
fill_color = 0, 0, 0 # 黑色
surface = pygame.display.set_mode((screen_x, screen_y))
pygame.display.set_caption("Draw a tank")
blockage = 5
tank_u = [1, 0, 0, 3, 0, 0, 1,
1, 2, 2, 3, 2, 2, 1,
1, 2, 2, 3, 2, 2, 1,
1, 2, 2, 3, 2, 2, 1,
1, 2, 2, 2, 2, 2, 1,
1, 2, 2, 2, 2, 2, 1,
1, 0, 0, 0, 0, 0, 1]
tank_d = [1, 0, 0, 0, 0, 0, 1,
1, 2, 2, 2, 2, 2, 1,
1, 2, 2, 2, 2, 2, 1,
1, 2, 2, 3, 2, 2, 1,
1, 2, 2, 3, 2, 2, 1,
1, 2, 2, 3, 2, 2, 1,
1, 0, 0, 3, 0, 0, 1]
tank_l = [1, 1, 1, 1, 1, 1, 1,
0, 2, 2, 2, 2, 2, 0,
0, 2, 2, 2, 2, 2, 0,
3, 3, 3, 3, 2, 2, 0,
0, 2, 2, 2, 2, 2, 0,
0, 2, 2, 2, 2, 2, 0,
1, 1, 1, 1, 1, 1, 1]
tank_r = [1, 1, 1, 1, 1, 1, 1,
0, 2, 2, 2, 2, 2, 0,
0, 2, 2, 2, 2, 2, 0,
0, 2, 2, 3, 3, 3, 3,
0, 2, 2, 2, 2, 2, 0,
0, 2, 2, 2, 2, 2, 0,
1, 1, 1, 1, 1, 1, 1]
def get_one_color():
'''
随机得到一种颜色
:return: 颜色R,G,B值
'''
r = random.randint(0, 255)
g = random.randint(0, 255)
b = random.randint(0, 255)
color = (r, g, b)
return color
class Tank(pygame.sprite.Sprite):
'坦克类'
def __init__(self):
self.direction = random.choice([tank_u, tank_d, tank_l, tank_r])
self.color_1 = get_one_color()
self.color_2 = get_one_color()
self.color_3 = get_one_color()
self.pos_x = random.randrange(0, screen_x - 7 * blockage + 1, 7)
self.pos_y = random.randrange(0, screen_y - 7 * blockage + 1, 7)
self.rect = [self.pos_x, self.pos_y, blockage, blockage]
self.speed = 0.5
def get\_speed(self):
return self.speed
def get\_direction(self):
return self.direction
def get\_color\_1(self):
return self.color\_1
def get\_color\_2(self):
return self.color\_2
def get\_color\_3(self):
return self.color\_3
def get\_rect(self):
return self.rect
def draw\_tank(self):
'''
根据列表中的数字,对应的涂上颜色
'''
list0 = self.get\_direction()
color1 = self.get\_color\_1(),
color2 = self.get\_color\_2(),
color3 = self.get\_color\_3(),
rect = self.get\_rect()
# 先计算每一项的起点坐标
def get\_pos(index):
'''
给定下标序号,计算每该项的起点坐标
:param index: 下标
:return: 该项的起点坐标
'''
# 行号,列号
row = index // 7
column = index % 7
return (row, column)
for i in range(len(list0)):
cur\_pos = get\_pos(i)
cur\_rect = (rect\[0\] + cur\_pos\[1\] \* blockage,
rect\[1\] + cur\_pos\[0\] \* blockage,
rect\[2\],
rect\[3\])
if list0\[i\] == 0:
pass # 跳过,不画
elif list0\[i\] == 1:
cur\_color = color1
pygame.draw.rect(surface, cur\_color, cur\_rect)
elif list0\[i\] == 2:
cur\_color = color2
pygame.draw.rect(surface, cur\_color, cur\_rect)
elif list0\[i\] == 3:
cur\_color = color3
pygame.draw.rect(surface, cur\_color, cur\_rect)
else:
print("出错,坦克列表中的值只能是0、1、2或者3")
pygame.quit()
# 防止加入速度变量后,有时会小部分出界
if rect\[0\] < 0:
rect\[0\] = 0
elif rect\[1\] < 0:
rect\[1\] = 0
elif rect\[0\] > screen\_x - 7 \* blockage:
rect\[0\] = screen\_x - 7 \* blockage
elif rect\[1\] > screen\_y - 7 \* blockage:
rect\[1\] = screen\_y - 7 \* blockage
def move(self):
temp = random.randint(1, 40)
if temp == 1: # 向上
# 先判断当前方向是否向上,如果是,则向上走,否则,先把方向调整向上,其他方向同理
if self.direction == tank\_u:
# 判断是否出界
if self.rect\[1\] > 0:
self.rect\[1\] -= int(blockage \* self.speed)
else:
self.direction = tank\_u
elif temp == 2: # 向下
if self.direction == tank\_d:
if self.rect\[1\] < screen\_y - 7 \* blockage:
self.rect\[1\] += int(blockage \* self.speed)
else:
self.direction = tank\_d
elif temp == 3: # 向左
if self.direction == tank\_l:
if self.rect\[0\] > 0:
self.rect\[0\] -= int(blockage \* self.speed)
else:
self.direction = tank\_l
elif temp == 4: # 向右
if self.direction == tank\_r:
if self.rect\[0\] < screen\_x - 7 \* blockage:
self.rect\[0\] += int(blockage \* self.speed)
else:
self.direction = tank\_r
else: # 一直向前
if self.direction == tank\_u:
if self.rect\[1\] > 0:
self.rect\[1\] -= int(blockage \* self.speed)
elif self.direction == tank\_d:
if self.rect\[1\] < screen\_y - 7 \* blockage:
self.rect\[1\] += int(blockage \* self.speed)
elif self.direction == tank\_l:
if self.rect\[0\] > 0:
self.rect\[0\] -= int(blockage \* self.speed)
else:
if self.rect\[0\] < screen\_x - 7 \* blockage:
self.rect\[0\] += int(blockage \* self.speed)
class MyTank(Tank):
def __init__(self):
Tank.__init__(self)
self.speed = 1
def move(self):
key = pygame.key.get\_pressed()
if key\[K\_w\] or key\[K\_UP\]:
# 先判断当前方向是否向上,如果是,则向上走,否则,先把方向调整向上,其他方向同理
if self.direction == tank\_u:
# 判断是否出界
if self.rect\[1\] > 0:
self.rect\[1\] -= int(blockage \* self.speed)
else:
self.direction = tank\_u
elif key\[K\_s\] or key\[K\_DOWN\]:
if self.direction == tank\_d:
if self.rect\[1\] < screen\_y - 7 \* blockage:
self.rect\[1\] += int(blockage \* self.speed)
else:
self.direction = tank\_d
elif key\[K\_a\] or key\[K\_LEFT\]:
if self.direction == tank\_l:
if self.rect\[0\] > 0:
self.rect\[0\] -= int(blockage \* self.speed)
else:
self.direction = tank\_l
elif key\[K\_d\] or key\[K\_RIGHT\]:
if self.direction == tank\_r:
if self.rect\[0\] < screen\_x - 7 \* blockage:
self.rect\[0\] += int(blockage \* self.speed)
else:
self.direction = tank\_r
self.draw\_tank()
class EnemyTank(Tank):
def __init__(self):
Tank.__init__(self)
self.speed = 2
def move(self):
Tank.move(self)
Tank.draw_tank(self)
my_tank = MyTank()
other_tank_1 = EnemyTank()
other_tank_2 = EnemyTank()
other_tank_3 = EnemyTank()
other_tank_4 = EnemyTank()
other_tank_5 = EnemyTank()
other_tank_6 = EnemyTank()
other_tank_7 = EnemyTank()
other_tank_8 = EnemyTank()
other_tank_9 = EnemyTank()
other_tank_10 = EnemyTank()
while not done:
FPS.tick(fps)
for event in pygame.event.get():
if event.type == QUIT:
done = True
surface.fill(fill\_color)
my\_tank.move()
other\_tank\_1.move()
other\_tank\_2.move()
other\_tank\_3.move()
other\_tank\_4.move()
other\_tank\_5.move()
other\_tank\_6.move()
other\_tank\_7.move()
other\_tank\_8.move()
other\_tank\_9.move()
other\_tank\_10.move()
pygame.display.flip()
pygame.quit()
手机扫一扫
移动阅读更方便
你可能感兴趣的文章