python.pygame.坦克大战源代码

  • v9_413086
    了解作者
  • 292.4KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-04-17 05:26
    上传日期
这是我分享的著名的坦克大战python版,python游戏的进阶版。很好的游戏参考资源。 有兴趣的同学可以加群634295212 一起讨论学习。参考分享或改进代码请注明原作者信息。 及新作者信息
battle-city-tanks.zip
内容介绍
#!/usr/bin/python # coding=utf-8 import os, pygame, time, random, uuid, sys class myRect(pygame.Rect): """ Add type property """ def __init__(self, left, top, width, height, type): pygame.Rect.__init__(self, left, top, width, height) self.type = type class Timer(object): def __init__(self): self.timers = [] def add(self, interval, f, repeat = -1): options = { "interval" : interval, "callback" : f, "repeat" : repeat, "times" : 0, "time" : 0, "uuid" : uuid.uuid4() } self.timers.append(options) return options["uuid"] 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 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 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 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. """ # 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 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) 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 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.transform.rotate(self.image, 90) self.rect = pygame.Rect(position[0] - 8 , position[1] + 11, 8, 6) self.explosion_images = [ sprites.subsurface(0, 80*2, 32*2, 32*2), sprites.subsurface(32*2, 80*2, 32*2, 32*2), ] self.speed = speed self.state = self.STATE_ACTIVE def draw(self): """ draw bullet """ global screen if self.state == self.STATE_ACTIVE: screen.blit(self.image, self.rect.topleft) elif self.state == self.STATE_EXPLODING: self.explosion.draw() def update(self): global castle, players, enemies, bullets if self.state == self.STATE_EXPLODING: if not self.explosion.active: self.destroy() del self.explosion if self.state != self.STATE_ACTIVE: return """ move bullet """ if self.direction == self.DIR_UP: self.rect.topleft = [self.rect.left, self.rect.top - self.speed] if self.rect.top < 0: if play_sounds and self.owner == self.OWNER_PLAYER: sounds["steel"].play() self.explode() return elif self.direction == self.DIR_RIGHT: self.rect.topleft = [self.rect.left + self.speed, self.rect.top] if self.rect.left > (416 - self.rect.width): if play_sounds and self.owner == self.OWNER_PLAYER: sounds["steel"].play() self.explode() return elif self.direction == self.DIR_DOWN: self.rect.topleft = [self.rect.left, self.rect.top + self.speed] if self.rect.top > (416 - self.rect.height): if play_sounds and self.owner == self.OWNER_PLAYER: sounds["steel"].play() self.explode() return elif self.direction == self.DIR_LEFT: self.rect.topleft = [self.rect.left - self.speed, self.rect.top] if self.rect.left < 0: if play_sounds and self.owner == self.OWNER_PLAYER: sounds["steel"].play() self.explode() return has_collided = False # check for collisions with walls. one bullet can destroy several (1 or 2) # tiles but explosion remains 1 rects = self.level.obstacle_rects collisions = self.rect.collidelistall(rects) if collisions != []: for i in collisions: if self.level.hitTile(rects[i].topleft, self.power, self.owner == self.OWNER_PLAYER): has_collided = True if has_collided: self.explode() return # check for collisions with other bullets for bullet in bullets: if self.state == self.STATE_ACTIVE and bullet.owner != self
评论
    相关推荐
    • python pygame 飞船资源图片
      飞机大战 图片资源下载
    • pygame python
      python游戏开发工具,需要配合32位python使用,64位的不要下载
    • aim-practice:使用pygame python库创建的瞄准游戏
      使用pygame python库创建的瞄准游戏 游戏规则 运行游戏时,您会被加载到开始屏幕上,在该屏幕上您需要单击按钮来开始游戏。 目的是单击目标。 单击目标时,您将获得一个分数,并且目标应在其他位置重新生成。 如果在...
    • python pygame 贪吃蛇
      自己写的贪吃蛇,解压后直接运行贪吃.py即可(q退出,p暂停)(没有安装pygame可以运行exe文件)
    • python-pygame-snake
      python-pygame-snake
    • pygame python
      安装media的时候其中的一个工具包
    • python_pygame
      Repositorio destinado ao curso decriaçãode jogos em 2d ultilizado modulo pygame
    • hxpyg:pygame Python库的Haxe externs
      ## HxPyg ###用于Python目标###的Pygame库的Haxe Externs和与Tannus库接口的有用类 目前已实施: pygame.Pygamepygame全局变量的外部 pygame.Display:主要pygame.display模块的外部 pygame.Color:pygame.Color...
    • python3.4可用pygame
      python3.4可用pygame
    • PythonPygame、pyinstaller
      python环境搭建三个配套软件,python-3.7.2中自带pip,不用单独安装pip。安装顺序: 1、安装python(按向导),配置环境变量(此电脑右键属性-高级系统设置-环境变量-“系统变量”中添加python的安装地址,cmd中输入...