pygame坦克大战前夕
最近想自己写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坦克大战前夕的更多相关文章
- pygame小游戏之坦克大战
以前在学校的时候无聊的学了会pygame.看了大概一周的教学视频,做出来个坦克大战的小游戏 Python3.5 pycharm import pygame,sys,time from random ...
- Python3+pygame实现的90坦克大战 代码完整 有演示效果
我是一个典型的80后,年轻时玩过了特别多的游戏,所以这几天用Python3+pygame实现了一个另外小游戏"坦克大战"(其他的游戏,请翻阅我的博客) 本实例代码量有些多,完整的版 ...
- Python3——坦克大战
# coding=utf-8 # Version:python3.6.1 __date__ = '2018/9/20 18:51' __author__ = 'Lgsp_Harold' import ...
- 简易坦克大战python版
#! /usr/bin/env python # -*- coding:utf8 -*- ''' *author:wasua *purpose:学习python语言,其中的类以及pygame应用 ...
- Python开发坦克大战
Python不仅能开发网站,爬虫数据分析等,他其实也可以写游戏,接下来就给大家分享下坦克大战的代码: PS:很多人在学习Python的过程中,往往因为遇问题解决不了或者没好的教程从而导致自己放弃,为此 ...
- 手把手教你用Python实现“坦克大战”,附详细代码!
小时候玩的“坦克大战”,你还记得吗? 满满的回忆 ! 今天,我们使用Python以及强大的第三方库来实现一个简单的坦克大战游戏. 整体效果 环境依赖 python3.7 pygame1.9.6 ...
- 用 Python 写个坦克大战
坦克大战是一款策略类的平面射击游戏,于 1985 年由 Namco 游戏公司发布,尽管时至今日已经有了很多衍生类的游戏,但这款游戏仍然受到了相当一部分人的欢迎,本文我们看一下如何使用 Python 来 ...
- 3D坦克大战游戏源码
3D坦克大战游戏源码,该游戏是基于xcode 4.3,ios sdk 5.1开发.在xcode4.3.3上完美无报错.兼容ios4.3-ios6.0 ,一款ios平台上难得的3D坦克大战游戏源码,有2 ...
- 【blade04】用面向对象的方法写javascript坦克大战
前言 javascript与程序的语言比如C#或者java不一样,他并没有“类”的概念,虽然最新的ECMAScript提出了Class的概念,我们却没有怎么用 就单以C#与Java来说,要到真正理解面 ...
随机推荐
- 帆软报表(finereport)鼠标悬停背景变色
在报表中,为了突出鼠标所在单元格,当鼠标悬浮时突出背景色(字体),鼠标离开后恢复原有的背景色(字体). 在设计器 模板>模板 Web 属性>填报页面设置,去除填报当前编辑行背景设置的勾选, ...
- 期中架构&防火墙¥四表五链
今日内容 架构图 包过滤防火墙 Iptables 新建虚拟机 内容详细 一.架构图 用户通过域名访问一个网站类比开车去饭店用餐 访问网站的流程 1.浏览器输入网站的域名(www.baidu.com), ...
- Solution -「ARC 125E」Snack
\(\mathcal{Description}\) Link. 把 \(n\) 种零食分给 \(m\) 个人,第 \(i\) 种零食有 \(a_i\) 个:第 \(i\) 个人得到同种零食数量 ...
- NVDA、争渡读屏语音开放API接口
什么是读屏软件? 读屏软件是一种专为视力障碍人士设计的,能够辅助视障人士操作计算机的工具,它可以将屏幕上显示的内容用语音朗读出来,这样视障人士就可以正常使用电脑了. 知名的屏幕阅读软件国内有争渡读屏. ...
- design PLL
PLL PLL主要是NCO中通过相位比较器,控制压控振荡器,控制频率.
- MySQL中的严格模式
很多集成的PHP环境(PHPnow WAMP Appserv等)自带的MySQL貌似都没有开启MySQL的严格模式,何为MySQL的严格模式,简单来说就是MySQL自身对数据进行严格的校验(格式.长度 ...
- 40多个丰富的QQ特效代码,非常实用哦!
Hi,我们好,我是GG!微信和QQ是现在干流的两款社交东西.有人说微信现已完全替代了QQ,现已没有人玩QQ了.可是小雨却不这么以为,毕竟微信和QQ的用户集体是不一样的,它们在功能上的定位也是不一样的. ...
- 从.net开发做到云原生运维(二)——.net core生态
1. 新的开始从.net 6.0开始 .net 6.0作为一个长期支持版,具有里程碑的意义.从.net5.0将.net framework和.net core合并以后,在.net5.0的功能上再次完善 ...
- 『德不孤』Pytest框架 — 8、Pytest断言
目录 1.什么是断言 2.Pytest断言 3.Pytest的断言方式及应用场景 (1)使用assert语句 (2)断言预期的异常 (3)拓展 4.优化断言 5.使用标记检查异常 1.什么是断言 对于 ...
- Java8新特性系列-默认方法
Java8 Interface Default and Static Methods 原文连接:Java8新特性系列-默认方法 – 微爱博客 在 Java 8 之前,接口只能有公共抽象方法. 如果不强 ...