最近想自己写pygame版的坦克大战,今晚已经完成如下功能:

1,我方坦克,可手动移动;敌方坦克,自动转方向与移动

2,坦克颜色随机,坦克形态大小可调。

3,双方坦克速度可调。

4,刷新坦克的位置随机。

5,坦克不会出界。

6,游戏窗口大小可调。

目前存在的问题:

1,表示坦克方向的列表,还未放到类里。未表示是否存活。

2,坦克会重叠(碰撞检测)。

3,炮弹类还未写。

4,......

# !/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Hiuhung Wan 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")
# 小方块边长,一辆坦克是是7*7个小方块组成的
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()

  

最新文章

  1. 阿里笔试题:在n个人中找明星
  2. linux系统性能调优第一步——性能分析(vmstat)
  3. TextEdit 回车事件
  4. 简易CSS3 Tab菜单 Tab切换滑块动画
  5. CentOS下MySQL 5.7编译安装
  6. JQuery jsonp使用小记
  7. 秒味课堂Angular js笔记------过滤器
  8. Effective C++ 24,25
  9. 一步一步学Vue(二)
  10. App引导界面,可以这么玩
  11. 停止预览时调用Camera.release(), 出现Method called after release()异常问题原因及解决办法
  12. Rectangular Covering [POJ2836] [状压DP]
  13. 于bugku中游荡意外得到关于CBC翻转攻击思路
  14. cad.net 利用win32api实现不重复打开dwg路径的文件夹(资源管理器)
  15. vue教程3-06 vue路由嵌套(多层路由),路由其他信息
  16. robot framework学习笔记1之_环境安装(win7)
  17. Coxph model Pvalue Select
  18. 如何添加ORACLE 的 ODBC
  19. MVC5 model常见的写法
  20. CODING 告诉你硅谷的研发项目管理之道(3)

热门文章

  1. 简述keepalived工作原理
  2. jquery-easyui环境的搭建及测试
  3. 开源GenICam项目上手-1
  4. for循环,stream,parallelStream的性能区别
  5. linux服务器随机10字符病毒/libudev4.so病毒清理的过程
  6. python+pytest(2)-HTTP协议基础
  7. 【C# 线程】RPC中常见的Stub| marshalling怎么理解
  8. 【C# 线程】Thread类 以及使用案例
  9. 【C#基础概念】虚方法virtual
  10. 【C# IO 操作】 Path 路径类 |Directory类 |DirectoryInfo 类|DriveInfo类|File类|FileInfo类|FileStream类