最近想自己写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. https校验问题

    一般会报SSL问题:解决办法参考 http://blog.csdn.net/a506681571/article/details/78284589 # 设置未经允许验证的SSL方法,只需运行一次便可 ...

  2. C#项目版本号自定义位置自动向上增加小工具设计与实现

    自从毕了业,好久没更新了,今天突发奇想,过来更新一下,嘻嘻! 一般在做版本升级时,要锁定版本号进行对比,然后联网检索可用的升级包信息,在用VS做C#项目组件版本管理时,是一个很麻烦的事,每次Relea ...

  3. Solution -「LOCAL」Drainage System

    \(\mathcal{Description}\)   合并果子,初始果子的权值在 \(1\sim n\) 之间,权值为 \(i\) 的有 \(a_i\) 个.每次可以挑 \(x\in[L,R]\) ...

  4. suse 12 利用缓存创建本地源供内网服务使用

    文章目录 服务端获取 添加源 刷新源 清除缓存 安装软件 获取rpm包 客户端测试 zypper --help 前言: 其实,咱也不知道为啥写了这篇博客,咱就是想学一学suse,咱也不会,咱也只能学, ...

  5. python-利用xlrd模块读取excel数据,将excel数据转换成字典格式

    前言 excel测试案例数据 转换成这种格式 实现代码 import os import xlrd excel_path = '..\data\\test_case.xlsx' data_path = ...

  6. 深度学习分类问题中accuracy等评价指标的理解

    在处理深度学习分类问题时,会用到一些评价指标,如accuracy(准确率)等.刚开始接触时会感觉有点多有点绕,不太好理解.本文写出我的理解,同时以语音唤醒(唤醒词识别)来举例,希望能加深理解这些指标. ...

  7. jenkins配置节点服务器

    参考博文:https://www.cnblogs.com/derekchen/p/5892286.html 配置的时候,遇到了以下问题,记录一下. 1. 按照教程配置,节点服务器(比如你要部署的测试服 ...

  8. oj教程--栈

    栈(stack)又名堆栈,它是一种运算受限的线性表.其限制是仅允许在表的一端进行插入和删除运算.这一端被称为栈顶,相对地,把另一端称为栈底.向一个栈插入新元素又称作进栈.入栈或压栈,它是把新元素放到栈 ...

  9. 微信小程序里实现跑马灯效果

    在微信小程序 里实现跑马灯效果,类似滚动字幕或者滚动广告之类的,使用简单的CSS样式控制,没用到JS wxml: <!-- 复制的跑马灯效果 --> <view class=&quo ...

  10. Mac下的平铺式桌面 - Yabai

    Mac下的平铺式桌面 - Yabai 近来无事,凑着周末休息的时间,想折腾一下 Mac.很久之前就有朋友给我推荐过一款名为"Yabai"的平铺式桌面管理软件,今天,就折腾起来了. ...