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来说,要到真正理解面 ...
随机推荐
- Java中md5摘要算法的几种方法
public class MD5_Test { public static String md5_1(String oldStr) { char hexDigits[] = { '0', '1', ' ...
- PHP的加密方法汇总
PHP的加密主要有4种方法,除此之外还有一种是URL的加密和解密.希望可以对你们开发有用. 顺带,我会在后面把我整理的一整套CSS3,PHP,MYSQL的开发的笔记打包放到百度云,有需要可以直接去百度 ...
- 显式锁之ReentrantLock实现
下图是Lock接口清单,定义了一些抽象的锁操作.Java本身提供了内部锁机制,那么还需要显示Lock,何用?与内部加锁机制不同,Lock提供了无条件.可轮询.定时.可中断的锁获取操作:所有加锁和解锁的 ...
- JMM之Java线程间通讯——等待通知机制及其经典范式
在并发编程中,实际处理涉及两个关键问题:线程之间如何通信及线程之间如何同步(这里的线程是指并发执行的活动实体). 通信是指线程之间以何种机制来交换信息.在共享内存的并发模型里,线程之间共享程序的公共状 ...
- ASP.NET Core 6框架揭秘-实例演示版[持续更新中…]
作为<ASP.NET Core 3框架揭秘>的升级版,<ASP.NET Core 6框架揭秘>提供了很多新的章节,同时对现有的内容进行大量的修改.虽然本书旨在对ASP.NET ...
- dart系列之:集合使用最佳实践
目录 简介 使用字面量创建集合 不要使用.length来判断集合是否为空 可遍历对象的遍历 List.from和iterable.toList where和whereType 避免使用cast 总结 ...
- 给博客加入链接安全跳转页(添加一个和CSDN一样的链接跳转页)
本文首发于青云工作室 原文链接为 https://qystudio.ltd/posts/25250.html 前言 或是出于优化 SEO,或是出于加强网站体验,很多博客都给文章中的外部链接加上了个二次 ...
- [Unity] 在软件标题栏显示工作路径
(一)问题 项目开发中常会有开多个分支,同时启动多个 Unity 程序的情况,来回切换的时候就容易混淆,有时候还需要用 Show In Explorer 或者其他标志来确认当前使用的是哪个分支. 于是 ...
- RENIX 软件如何进行IP地址管理——网络测试仪实操
本文主要介绍了BIGTAO网络测试仪如何通过RENIX软件进行IP地址管理.文章分为五部分内容,第一部分介绍了如何通过机框显示屏查看IP地址,之后几部分分别介绍了机框按钮修改.机框接显示器/键盘修改. ...
- linux /etc/fstab 文件详细说明(整理版)
转至:https://blog.csdn.net/xiaolongwang2010/article/details/8657332?utm_medium=distribute.pc_relevant. ...