day-21 坦克大战,完成碰撞消除5行代码

碰撞消除实现关键是遍历玩家坦克和敌军子弹和敌军坦克列表的坐标相交判定 以及 敌军坦克和玩家子弹列表的坐标相较判定
鉴于子弹和坦克都是正方形的图片,使用圆的相交判定来确定是否相交

# tank.py
class Tank:

    def __init__(self, life, speed_x, speed_y, screen):
        self.life = life
        self.speed_x = speed_x
        self.speed_y = speed_y
        self.screen = screen

    def show_tank(self, image, x, y):
        self.screen.blit(image, (x, y))

    def hit(self):
        pass

    def move(self):
        pass

    def dead(self):
        pass

# mytank.py
import tank
import pygame
import bullet


class MyTank(tank.Tank):

    hero_up = './mytank/1.gif'
    hero_down = './mytank/3.gif'
    hero_left = './mytank/4.gif'
    hero_right = './mytank/2.gif'
    bullets = []

    def __init__(self, screen, pos_x=0, pos_y=0):
        super().__init__(1, 0, 0, screen)
        self.pos_x = pos_x
        self.pos_y = pos_y

    def move(self, move_dic):
        if move_dic == 'left':
            surface = pygame.image.load(self.hero_left)
            self.speed_x = -5
            self.speed_y = 0
        elif move_dic == 'right':
            surface = pygame.image.load(self.hero_right)
            self.speed_x = 5
            self.speed_y = 0
        elif move_dic == 'up':
            surface = pygame.image.load(self.hero_up)
            self.speed_y = -5
            self.speed_x = 0
        elif move_dic == 'down':
            surface = pygame.image.load(self.hero_down)
            self.speed_y = 5
            self.speed_x = 0
        return surface, move_dic

    def stop(self, stop_dic):
        if stop_dic == 'left':
            surface = pygame.image.load(self.hero_left)
            self.speed_x = 0
            self.speed_y = 0
        elif stop_dic == 'right':
            surface = pygame.image.load(self.hero_right)
            self.speed_x = 0
            self.speed_y = 0
        elif stop_dic == 'up':
            surface = pygame.image.load(self.hero_up)
            self.speed_y = 0
            self.speed_x = 0
        elif stop_dic == 'down':
            surface = pygame.image.load(self.hero_down)
            self.speed_y = 0
            self.speed_x = 0
        return surface, stop_dic

    def change_position(self):
        self.pos_x += self.speed_x
        self.pos_y += self.speed_y

    def hit(self, dic):
        global bullet1
        tank_w, tank_h = pygame.image.load(self.hero_up).get_size()
        bullet_w, bullet_h = bullet.Bullet.surface.get_size()
        if dic == 'left':
            bullet1 = bullet.Bullet(self.pos_x - bullet_w, self.pos_y + (tank_h - bullet_h) / 2)
            bullet1.move(dic)
        elif dic == 'right':
            bullet1 = bullet.Bullet(self.pos_x + tank_w, self.pos_y + (tank_h - bullet_h) / 2)
            bullet1.move(dic)
        elif dic == 'up':
            bullet1 = bullet.Bullet(self.pos_x + (tank_w - bullet_w) / 2, self.pos_y - bullet_h)
            bullet1.move(dic)
        elif dic == 'down':
            bullet1 = bullet.Bullet(self.pos_x + (tank_w - bullet_w) / 2, self.pos_y + tank_h)
            bullet1.move(dic)
        self.bullets.append(bullet1)

    def bullet_change(self):
        for b in self.bullets:
            b.pos_x += b.speed_x
            b.pos_y += b.speed_y

    def bullet_show(self):
        for b in self.bullets:
            self.screen.blit(bullet.Bullet.surface, (b.pos_x, b.pos_y))

    def bullet_dead(self):
        for b in self.bullets[:]:
            if b.pos_x <= 0 or b.pos_x >= 60*20:
                self.bullets.remove(b)
            if b.pos_y <= 0 or b.pos_y >= 60*14:
                self.bullets.remove(b)
# enemy
import pygame
import tank
from random import randint
import bullet


class Enemy(tank.Tank):

    def __init__(self, life, surface, speed_x=0, speed_y=0, screen=0, pos_x=0, pos_y=0, enemy_bullet=[]):
        self.life = life
        self.speed_x = speed_x
        self.speed_y = speed_y
        self.screen = screen
        self.pos_x = pos_x
        self.pos_y = pos_y
        self.type_e = randint(1,3)
        self.enemy_bullet = enemy_bullet
        self.surface = surface

    def move(self):
        r_dic = randint(-50, 50)
        if r_dic == 1:
            surface = pygame.image.load('./enemy/enemy'+str(self.type_e)+'/u.gif')
            self.speed_y = self.type_e * (-1)
            self.speed_x = 0
            self.hit(r_dic)
            self.surface = surface
        elif r_dic == 2:
            surface = pygame.image.load('./enemy/enemy'+str(self.type_e)+'/d.gif')
            self.speed_y = self.type_e * 1
            self.speed_x = 0
            self.hit(r_dic)
            self.surface = surface
        elif r_dic == 3:
            surface = pygame.image.load('./enemy/enemy'+str(self.type_e)+'/l.gif')
            self.speed_x = self.type_e * (-1)
            self.speed_y = 0
            self.hit(r_dic)
            self.surface = surface
        elif r_dic == 4:
            surface = pygame.image.load('./enemy/enemy'+str(self.type_e)+'/r.gif')
            self.speed_x = self.type_e * 1
            self.speed_y = 0
            self.hit(r_dic)
            self.surface = surface

    def hit(self, r_dic):
        tank_w, tank_h = pygame.image.load('./enemy/enemy'+str(self.type_e)+'/u.gif').get_size()
        bullet_w, bullet_h = bullet.Bullet.surface.get_size()
        if r_dic == 3:
            bullet1 = bullet.Bullet(self.pos_x - bullet_w, self.pos_y + (tank_h - bullet_h) / 2)
            bullet1.move('left')
            self.enemy_bullet.append(bullet1)
        elif r_dic == 4:
            bullet1 = bullet.Bullet(self.pos_x + tank_w, self.pos_y + (tank_h - bullet_h) / 2)
            bullet1.move('right')
            self.enemy_bullet.append(bullet1)
        elif r_dic == 1:
            bullet1 = bullet.Bullet(self.pos_x + (tank_w - bullet_w) / 2, self.pos_y - bullet_h)
            bullet1.move('up')
            self.enemy_bullet.append(bullet1)
        elif r_dic == 2:
            bullet1 = bullet.Bullet(self.pos_x + (tank_w - bullet_w) / 2, self.pos_y + tank_h)
            bullet1.move('down')
            self.enemy_bullet.append(bullet1)

    def bullet_change(self):
        for b in self.enemy_bullet:
            b.pos_x += b.speed_x
            b.pos_y += b.speed_y

    def bullet_show(self):
        for b in self.enemy_bullet:
            self.screen.blit(bullet.Bullet.surface, (b.pos_x, b.pos_y))

    def bullet_dead(self):
        for b in self.enemy_bullet:
            if b.pos_x <= 0 or b.pos_x >= 60*20:
                self.enemy_bullet.remove(b)
            if b.pos_y <= 0 or b.pos_y >= 60*14:
                self.enemy_bullet.remove(b)

    def change_position(self):
        self.pos_x += self.speed_x
        self.pos_y += self.speed_y
# bullet.py
class Bullet:

    surface = pygame.image.load('./bullet/10.gif')
    speed_x = 0
    speed_y = 0

    def __init__(self, pos_x, pos_y):
        self.pos_x = pos_x
        self.pos_y = pos_y

    def move(self, move_dic):
        if move_dic == 'left':
            self.speed_x = -8
        elif move_dic == 'right':
            self.speed_x = 8
        elif move_dic == 'up':
            self.speed_y = -8
        elif move_dic == 'down':
            self.speed_y = 8
# main.py
"""new python"""
import pygame
import mytank
from enemy import Enemy
from random import randint


if __name__ == '__main__':
    pygame.init()
    width = 60 * 20
    height = 60 * 14
    screen = pygame.display.set_mode((width, height))
    bullet_wid, bullet_hig = bullet.Bullet.surface.get_size()
    pos_x = 660
    pos_y = 780
    hero = mytank.MyTank(screen, pos_x, pos_y)
    surface = pygame.image.load(mytank.MyTank.hero_up)
    dic = ''
    surface_e = pygame.image.load('./enemy/enemy1/u.gif')
    enemy_wid, enemy_hig = surface_e.get_size()
    enemys = []
    while True:
        rr = randint(1, 3)
        while len(enemys) < 6:
            enemy = Enemy(randint(1, 6), surface_e, 0, 0, screen, randint(100, 1000), randint(100, 500), [])
            enemys.append(enemy)
        screen.fill((0, 0, 0))
        hero.bullet_dead()
        hero.show_tank(surface, hero.pos_x, hero.pos_y)
        hero.bullet_show()
        for enemy in enemys:
            if (enemy.pos_x >= 60 * 19 and enemy.speed_x > 0) or (enemy.pos_x <= 0 and enemy.speed_x < 0):
                enemy.speed_x = 0 - enemy.speed_x
            if (enemy.pos_y >= 60 * 13 and enemy.speed_y > 0) or (enemy.pos_y <= 0 and enemy.speed_y < 0):
                enemy.speed_y = 0 - enemy.speed_y
            enemy.change_position()
            enemy.bullet_change()
            enemy.bullet_dead()
            enemy.show_tank(enemy.surface, enemy.pos_x, enemy.pos_y)
            enemy.bullet_show()
            enemy.move()
        pygame.display.flip()
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RIGHT:
                    surface, dic = hero.move('right')
                elif event.key == pygame.K_LEFT:
                    surface, dic = hero.move('left')
                elif event.key == pygame.K_UP:
                    surface, dic = hero.move('up')
                elif event.key == pygame.K_DOWN:
                    surface, dic = hero.move('down')
                if event.key == pygame.K_SPACE:
                    hero.hit(dic)
            elif event.type == pygame.KEYUP:
                if event.key == pygame.K_RIGHT:
                    surface, dic = hero.stop('right')
                elif event.key == pygame.K_LEFT:
                    surface, dic = hero.stop('left')
                elif event.key == pygame.K_UP:
                    surface, dic = hero.stop('up')
                elif event.key == pygame.K_DOWN:
                    surface, dic = hero.stop('down')
        if (hero.pos_x >= 60*19 and hero.speed_x > 0) or (hero.pos_x <= 0 and hero.speed_x < 0):
            hero.speed_x = 0
        if (hero.pos_y >= 60*13 and hero.speed_y > 0) or (hero.pos_y <= 0 and hero.speed_y < 0):
            hero.speed_y = 0
        hero.change_position()
        hero.bullet_change()
        pygame.time.delay(10)

碰撞消除在各个类的dead方法中实现

        for e in enemys:
            for my_bullet in mytank.MyTank.bullets:
                if math.sqrt((e.pos_x+enemy_wid/2-my_bullet.pos_x-bullet_wid/2)**2+(e.pos_y+enemy_hig/2-my_bullet.pos_y-bullet_hig/2)**2) < (enemy_wid/2+bullet_wid/2):
                    enemys.remove(e)
                    mytank.MyTank.bullets.remove(my_bullet)

放在main.py中 pygame.time.delay(10)前即可完成敌军和我方子弹的碰撞消除,自己和敌方子弹的碰撞消除同理即可,想了一下还是给个运行图吧(随机类型敌军坦克,并且少于6个,自动生成到6个)


image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 211,290评论 6 491
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,107评论 2 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 156,872评论 0 347
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,415评论 1 283
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,453评论 6 385
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,784评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,927评论 3 406
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,691评论 0 266
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,137评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,472评论 2 326
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,622评论 1 340
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,289评论 4 329
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,887评论 3 312
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,741评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,977评论 1 265
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,316评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,490评论 2 348