Python编写坦克大战(新增无敌模式)-附赠源码

这篇博客介绍了如何使用Python开发一款坦克大战游戏,并新增了无敌模式、全屏、静音、暂停等特性。游戏允许玩家自定义地图和起始关卡,通过击败20个坦克进入下一关。提供了完整的代码注释和资源获取方式。
摘要由CSDN通过智能技术生成

新增功能:
1、q键直接退出游戏  -》  进行结算然后返回模式选择界面
2、 新增全屏按钮u ,并且切换的时候会造成3s的冻结效果
3、静音键m
4、暂停键p
5、无敌模式,进入下一关条件是只能打败20个坦克
6、代码注释完整
7、z键打印当前坦克的情况
8、可自定义地图
9、可自定义起始关卡

废话不多说,直接上源码,老规矩,需要打包好的软件关注小编,QQ群:810735403领取。

 

#!/usr/bin/python

# coding=utf-8

import datetime

import os, pygame, time, random, uuid, sys, psutil

from datetime import date

from time import ctime, sleep



# 矩阵的 长宽高

class myRect(pygame.Rect):

""" 添加类型属性 """


def __init__(self, left, top, width, height, type):

# 矩阵位置

pygame.Rect.__init__(self, left, top, width, height)

self.type = type


# 获得一个唯一的随机的uid

class Timer(object):

def __init__(self):

self.timers = []


# 获得一个唯一的随机的uid


def add(self, interval, f, repeat=-1):

options = {"interval": interval, "callback": f, "repeat": repeat, "times": 0, "time": 0, "uuid": uuid.uuid4()

# 生成一个随机的UUID

}

self.timers.append(options)


return options["uuid"]


# 销毁指定的uid(uuid_nr)


def destroy(self, uuid_nr):

for timer in self.timers:

if timer["uuid"] == uuid_nr:

self.timers.remove(timer)

return


# 定时器

def update(self, time_passed):

for timer in self.timers:

timer["time"] += time_passed

if timer["time"] > timer["interval"]:

timer["time"] -= timer["interval"]

timer["times"] += 1

if timer["repeat"] > -1 and timer["times"] == timer["repeat"]:

self.timers.remove(timer)

try:

timer["callback"]()

except:

try:

self.timers.remove(timer)

except:

pass



# 堡垒类

class Castle():

""" Player's castle/fortress 堡垒的位置"""


(STATE_STANDING, STATE_DESTROYED, STATE_EXPLODING) = range(3)


def __init__(self):


global sprites


# images ,subsurface()从父表面创建一个新表面

self.img_undamaged = sprites.subsurface(0, 15 * 2, 16 * 2, 16 * 2)

self.img_destroyed = sprites.subsurface(16 * 2, 15 * 2, 16 * 2, 16 * 2)


# init position 初始化位置

self.rect = pygame.Rect(12 * 16, 24 * 16, 32, 32)


# start w/ undamaged and shiny castle从未损坏和闪亮的城堡开始

self.rebuild()



def draw(self):

""" Draw castle 画一个城堡"""

global screen

# blit()对图片进行操作,将一个平面的一部分或全部图象整块从这个平面复制到另一个平面

screen.blit(self.image, self.rect.topleft)

# 判断城堡是否被扎破

if self.state == self.STATE_EXPLODING:

if not self.explosion.active:

self.state = self.STATE_DESTROYED

del self.explosion

else:

self.explosion.draw()


def rebuild(self):

""" Reset castle重置城堡 """

self.state = self.STATE_STANDING

self.image = self.img_undamaged # 城堡完整的图片

self.active = True


def destroy(self):

""" Destroy castle 销毁城堡"""

self.state = self.STATE_EXPLODING

self.explosion = Explosion(self.rect.topleft) # 图片的左上被销毁

self.image = self.img_destroyed # 城堡被销毁的图片

if god == False:

self.active = False


# 道具

class Bonus():

""" Various power-ups 各种道具

When bonus is spawned, it begins flashing and after some time dissapears当奖励产生时,它开始闪烁,一段时间后消失

Available bonusses: 可用奖励列表

手雷grenade : Picking up the grenade power up instantly wipes out ever enemy presently on the screen, including Armor Tanks regardless of how many times you've hit them. You do not, however, get credit for destroying them during the end-stage bonus points.

头盔helmet : The helmet power up grants you a temporary force field that makes you invulnerable to enemy shots, just like the one you begin every stage with.

铲子shovel : The shovel power up turns the walls around your fortress from brick to stone. This makes it impossible for the enemy to penetrate the wall and destroy your fortress, ending the game prematurely. The effect, however, is only temporary, and will wear off eventually.

星星star : The star power up grants your tank with new offensive power each time you pick one up, up to three times. The first star allows you to fire your bullets as fast as the power tanks can. The second star allows you to fire up to two bullets on the screen at one time. And the third star allows your bullets to destroy the otherwise unbreakable steel walls. You carry this power with you to each new stage until you lose a life.

坦克tank : The tank power up grants you one extra life. The only other way to get an extra life is to score 20000 points.

定时器timer : The timer power up temporarily freezes time, allowing you to harmlessly approach every tank and destroy them until the time freeze wears off.

手雷:拿起手雷的电源,立即清除屏幕上的任何敌人,包括装甲坦克,无论你击中他们多少次。但是,你不会因为在最后阶段摧毁它们而得到积分。

头盔:头盔能量提升给你一个临时力场,使你对敌人的射击无坚不摧,就像你在每个阶段开始的时候一样。

铲子:铲子的力量把堡垒周围的墙从砖变成石头。这使得敌人不可能穿透墙壁并摧毁你的堡垒,从而提前结束游戏。然而,这种影响只是暂时的,最终会逐渐消失。

星型:每次你拿起一辆星型坦克,它就会给你的坦克提供新的进攻能力,最多三次。第一颗星可以让你发射子弹的速度与动力坦克一样快。

第二颗星允许你在屏幕上同时发射两颗子弹。第三颗星可以让你的子弹摧毁原本坚不可摧的钢铁墙。你带着这种力量进入每一个新的阶段,直到你失去一条生命。

坦克:坦克的能量增加可以让你多活一次。获得额外生命的唯一方法是获得20000分。

定时器:定时器暂时冻结时间,允许你无害地接近每一辆坦克并摧毁它们,直到冻结时间消失。

"""


# bonus types 道具类型

(BONUS_GRENADE, BONUS_HELMET, BONUS_SHOVEL, BONUS_STAR, BONUS_TANK, BONUS_TIMER) = range(6)


def __init__(self, level):

global sprites


# to know where to place 在哪个地图(关卡)

self.level = level


# bonus lives only for a limited period of time 奖励只在有限的一段时间内有效

self.active = True


# blinking state 闪烁状态

self.visible = True


# 道具随机出现的地点

self.rect = pygame.Rect(random.randint(0, 416 - 32), random.randint(0, 416 - 32), 32, 32)


# 随机选一个道具

self.bonus = random.choice(

[self.BONUS_GRENADE, self.BONUS_HELMET, self.BONUS_SHOVEL, self.BONUS_STAR, self.BONUS_TANK,

self.BONUS_TIMER])


self.image = sprites.subsurface(16 * 2 * self.bonus, 32 * 2, 16 * 2, 15 * 2)


def draw(self):

""" draw bonus 画道具"""

global screen

if self.visible:

screen.blit(self.image, self.rect.topleft)


def toggleVisibility(self):

""" Toggle bonus visibility 道具开关可见??"""

self.visible = not self.visible # self.visible = self.visible



# 子弹

class Bullet():

# direction constants 子弹的方向的常量

(DIR_UP, DIR_RIGHT, DIR_DOWN, DIR_LEFT) = range(4)


# bullet's stated 声明子弹

(STATE_REMOVED, STATE_ACTIVE, STATE_EXPLODING) = range(3)


(OWNER_PLAYER, OWNER_ENEMY) = range(2)


# 子弹初始化是:damage = 100, speed = 5

def __init__(self, level, position, direction, damage=100, speed=5):


global sprites


self.level = level

self.direction = direction

self.damage = damage

self.owner = None

self.owner_class = None


# 1-regular everyday normal bullet常规子弹

# 2-can destroy steel能摧毁钢铁的子弹

self.power = 1 # 1/2


self.image = sprites.subsurface(75 * 2, 74 * 2, 3 * 2, 4 * 2)


# position is player's top left corner, so we'll need to子弹位置在坦克的左上角

# recalculate a bit. also rotate image itself.旋转图像本身

if direction == self.DIR_UP:

self.rect = pygame.Rect(position[0] + 11, position[1] - 8, 6, 8)

elif direction == self.DIR_RIGHT:

self.image = pygame.transform.rotate(self.image, 270)

self.rect = pygame.Rect(position[0] + 26, position[1] + 11, 8, 6)

elif direction == self.DIR_DOWN:

self.image = pygame.transform.rotate(self.image, 180)

self.rect = pygame.Rect(position[0] + 11, position[1] + 26, 6, 8)

elif direction == self.DIR_LEFT:

self.image = pygame
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值