最近想自己写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()

  

pygame坦克大战前夕的更多相关文章

  1. pygame小游戏之坦克大战

    以前在学校的时候无聊的学了会pygame.看了大概一周的教学视频,做出来个坦克大战的小游戏 Python3.5  pycharm import pygame,sys,time from random ...

  2. Python3+pygame实现的90坦克大战 代码完整 有演示效果

    我是一个典型的80后,年轻时玩过了特别多的游戏,所以这几天用Python3+pygame实现了一个另外小游戏"坦克大战"(其他的游戏,请翻阅我的博客) 本实例代码量有些多,完整的版 ...

  3. Python3——坦克大战

    # coding=utf-8 # Version:python3.6.1 __date__ = '2018/9/20 18:51' __author__ = 'Lgsp_Harold' import ...

  4. 简易坦克大战python版

      #! /usr/bin/env python # -*- coding:utf8 -*- ''' *author:wasua *purpose:学习python语言,其中的类以及pygame应用 ...

  5. Python开发坦克大战

    Python不仅能开发网站,爬虫数据分析等,他其实也可以写游戏,接下来就给大家分享下坦克大战的代码: PS:很多人在学习Python的过程中,往往因为遇问题解决不了或者没好的教程从而导致自己放弃,为此 ...

  6. 手把手教你用Python实现“坦克大战”,附详细代码!

    小时候玩的“坦克大战”,你还记得吗? ​ 满满的回忆 ! 今天,我们使用Python以及强大的第三方库来实现一个简单的坦克大战游戏. ​ 整体效果 环境依赖 python3.7 pygame1.9.6 ...

  7. 用 Python 写个坦克大战

    坦克大战是一款策略类的平面射击游戏,于 1985 年由 Namco 游戏公司发布,尽管时至今日已经有了很多衍生类的游戏,但这款游戏仍然受到了相当一部分人的欢迎,本文我们看一下如何使用 Python 来 ...

  8. 3D坦克大战游戏源码

    3D坦克大战游戏源码,该游戏是基于xcode 4.3,ios sdk 5.1开发.在xcode4.3.3上完美无报错.兼容ios4.3-ios6.0 ,一款ios平台上难得的3D坦克大战游戏源码,有2 ...

  9. 【blade04】用面向对象的方法写javascript坦克大战

    前言 javascript与程序的语言比如C#或者java不一样,他并没有“类”的概念,虽然最新的ECMAScript提出了Class的概念,我们却没有怎么用 就单以C#与Java来说,要到真正理解面 ...

随机推荐

  1. Solution -「SHOI2016」「洛谷 P4336」黑暗前的幻想乡

    \(\mathcal{Description}\)   link.   有一个 \(n\) 个结点的无向图,给定 \(n-1\) 组边集,求从每组边集选出恰一条边最终构成树的方案树.对 \(10^9+ ...

  2. splunk设置索引周期和索引大小

    步骤一: 编辑/opt/splunk/etc/apps/search/local/indexs.conf ,在每个索引下面 加入最后两行内容 [messages] coldPath = $SPLUNK ...

  3. pytest--mark基本使用(主要通过pytest.ini文件注册标签名,对用例进行标记分组)

    1.pytest中的mark介绍 mark主要用于在测试用例/测试类中给用例打标记(只能使用已注册的标记 名),实现测试分组功能,并能和其它插件配合设置测试方法执行顺序等.如下 图,现在需要只执行红色 ...

  4. Renix软件如何添加VLAN头部——网络测试仪实操

    一.添加VLAN头部 打开Renix软件,连接机箱, 预约端口 添加VLAN头部,选中"IPv4 Header"右键选择"Insert Before",会弹出& ...

  5. Java处理.tif或.tiff图片

    前言 Java将图片读取到内存用的是ImageIO,默认可以处理的图片格式如下: ImageIO.getWriterFileSuffixes() //此方法返回可以处理的图片格式数组 jpg bmp ...

  6. Java课程设计---添加学生

    1.创建添加窗体 package com.student.view; import java.awt.EventQueue; import javax.swing.ButtonGroup; impor ...

  7. spring项目获取ApplicationContext(能手动从Spring获取所需要的bean)

    最流行的方法就是  实现ApplicationContextAware接口 @Component public class SpringContextUtil implements Applicati ...

  8. ValueError: not enough values to unpack

    当在同一行为两个变量赋值时,要用分号;而非逗号,隔开

  9. Excel:如何为某一列写相同的数而不是累加的数

    按住CTRL,双击单元格右下角的加号,则该行以下的行都是这个单元格的值 不按CTRL,双击单元格右下角的加号,该行以下的行都是每次加一的数

  10. MySQL:Win10系统中设置默认编码为UTF-8

    Win10 系统下 Mysql 字符集(utf8)的设置 补充: 在[mysqld]下添加语句:init_connect='SET collation_connection = utf8_unicod ...