坦克大战的皱型

前言

本文教你如何写坦克大战!但是是一个皱型的坦克大战。

一、准备两个库,自己创建的!

from pygame.sprite import  Sprite
import  pygame
class Bullet1(Sprite):
    def __init__(self,screen,tank,pos):
        super().__init__()
        self.screen = screen
        self.tank = tank
        self.color = (0,0,0)
        self.pos = int(pos)
        self.rect = pygame.Rect(0,0,2,2)
        self.rect.center = self.tank.rect.center
    def update(self):
        if self.pos == 1:
            self.rect.centery -= 1
        elif self.pos == 2:
            self.rect.centerx += 1
        elif self.pos == 3:
            self.rect.centery += 1
        elif self.pos == 4:
            self.rect.centerx -= 1
    def draw_bullet(self):
        pygame.draw.rect(self.screen,self.color,self.rect)
class Bullet2(Sprite):
    def __init__(self,screen,tank,pos):
        super().__init__()
        self.screen = screen
        self.color = (0,0,0)
        self.tank = tank
        self.pos = int(pos)
        self.rect = pygame.Rect(0,0,2,2)
        self.rect.center = self.tank.rect.center
    def update(self):
        if self.pos == 1:
            self.rect.centery -= 1
        elif self.pos == 2:
            self.rect.centerx += 1
        elif self.pos == 3:
            self.rect.centery += 1
        elif self.pos == 4:
            self.rect.centerx -= 1
    def draw_bullet(self):
        pygame.draw.rect(self.screen,self.color,self.rect)
import pygame
class Tank1():
    def __init__(self,screen):
        self.screen = screen
        self.img = pygame.image.load("p1tankL.gif")
        self.rect = self.img.get_rect()
        self.rect.center = (600,400)
        self.rect.centerx = 600
        self.rect.centery = 400
    def draw_tank(self):
        self.screen.blit(self.img,self.rect)
class Tank2():
    def __init__(self, screen):
        self.screen = screen
        self.img = pygame.image.load("enemy1L.gif")
        self.rect = self.img.get_rect()
        self.rect.center = (600, 400)
        self.rect.centerx = 600
        self.rect.centery = 400
    def draw_tank(self):
        self.screen.blit(self.img,self.rect)

二、使用步骤

1.引入库

代码如下(示例):

import pygame
import sys
import Tank
import Bullet
from pygame.sprite import Group

2.读入数据

代码如下(示例):

def score_change(bullets1,bullets2,tank1,tank2):     #双方坦克攻击
    for i in bullets1.copy():
        if tank2 .rect.collidepoint(i.rect.center):
            bullets1.remove(i)
            print("tank1击中了tank2")
            break
    for i in bullets2.copy():
        if tank1 .rect.collidepoint(i.rect.center):
            bullets2.remove(i)
            print("tank2击中了tank1")
            break
def rungame():  #定义运行代码
    pygame.init()
    screen = pygame.display.set_mode((800,600))   #展示屏幕
    pygame.display.set_caption("xx游戏")  #创造游戏名称
    pos1 = 1
    pos2 = 1   #
    tank1 = Tank.Tank1(screen)
    tank2 = Tank.Tank2(screen)
    tank1_bullts = Group()
    tank2_bullts = Group()
    screen.fill((230,230,230))
    while True:
        screen.fill((230,230,230))
        tank1.draw_tank()
        tank2.draw_tank()
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
        mykeylist = pygame.key.get_pressed()
        if mykeylist[pygame.K_UP]:
            pos1 = 1
            tank1.rect.centery -= 1
            tank1.img = pygame.image.load("p1tankU.gif")
        elif mykeylist[pygame.K_DOWN]:
            pos1 = 3
            tank1.rect.centery += 1
            tank1.img = pygame.image.load("p1tankD.gif")
        elif mykeylist[pygame.K_RIGHT]:
            pos1 = 2
            tank1.rect.centerx += 1
            tank1.img = pygame.image.load("p1tankR.gif")
        elif mykeylist[pygame.K_LEFT]:
            pos1 = 4
            tank1.rect.centerx -= 1
            tank1.img = pygame.image.load("p1tankL.gif")
        elif mykeylist[pygame.K_SPACE]:
            if tank1_bullts.__len__() < 5:
                newbullet = Bullet.Bullet1(screen,tank1,pos1)
                tank1_bullts.add(newbullet)


        if mykeylist[pygame.K_w]:
            pos2 = 1
            tank2.rect.centery -= 1
            tank2.img = pygame.image.load("enemy1U.gif")
        elif mykeylist[pygame.K_s]:
            pos2 = 3
            tank2.rect.centery += 1
            tank2.img = pygame.image.load("enemy1D.gif")
        elif mykeylist[pygame.K_d]:
            pos2 = 2
            tank2.rect.centerx += 1
            tank2.img = pygame.image.load("enemy1R.gif")
        elif mykeylist[pygame.K_a]:
            pos2 = 4
            tank2.rect.centerx -= 1
            tank2.img = pygame.image.load("enemy1L.gif")
        elif mykeylist[pygame.K_j]:
            if tank2_bullts.__len__() < 5:
                newbullet = Bullet.Bullet2(screen,tank2,pos2)
                tank2_bullts.add(newbullet)

        if tank1.rect.centery<0:
            tank1.rect.centery = 0
        if tank1.rect.centery>600:
            tank1.rect.centery = 600
        if tank1.rect.centerx<0:
            tank1.rect.centerx = 0
        if tank1.rect.centerx>800:
            tank1.rect.centerx = 800
        tank1_bullts.update()
        for i in tank1_bullts.sprites():
            i.draw_bullet()
        for i in tank1_bullts.copy():
            if i.rect.centerx <= 0 or i.rect.centerx >= 800:
                tank1_bullts.remove(i)
            elif i.rect.centery >= 600 or i.rect.centery <=0:
                tank1_bullts.remove(i)

        if tank2.rect.centery < 0:
            tank2.rect.centery = 0
        if tank2.rect.centery > 600:
            tank2.rect.centery = 600
        if tank2.rect.centerx < 0:
            tank2.rect.centerx = 0
        if tank2.rect.centerx > 800:
            tank2.rect.centerx = 800
        tank2_bullts.update()
        for i in tank2_bullts.sprites():
            i.draw_bullet()
        for i in tank2_bullts.copy():
            if i.rect.centerx <= 0 or i.rect.centerx >= 800:
                tank2_bullts.remove(i)
            elif i.rect.centery >= 600 or i.rect.centery <= 0:
                tank2_bullts.remove(i)
        score_change(tank1_bullts, tank2_bullts, tank1, tank2)
        pygame.display.flip()
rungame()

总结

提示:这里对文章进行总结:
坦克大战吧!其实内容很长 一下子讲不玩,当然我想说的是:劳烦各位大佬指点一下,把后面的内容补充补充就好!

你可能感兴趣的:(坦克大战的皱型)