这个只是初步的坦克大战,只有一个坦克,进行刷新的动作。线程类的操作不是太懂。唯一知道的就是不能再主线程里面使用sleep ,这样子的话,主线程就进入休眠的状态,不能进行时间的监听。 

 

import pygame, sys
from pygame.locals import *
import threading
import time

font = ("华文行楷", 15)
RedColor = pygame.Color(255, 0, 0)
blackColor = pygame.Color(0, 0, 0)
whiteColor = pygame.Color(255, 255, 255)
greyColor = pygame.Color(150, 150, 150)
lightColor = pygame.Color(220, 220, 220)

screen = pygame.display.set_mode((660, 660), 0, 32)


# 位置初始化
class pos:
    def __init__(self, x, y):
        self.x = x
        self.y = y


# 坦克的类所属
class Tank(object):
    def __init__(self, Pos, Direction):
        self.pos = Pos
        self.direction = Direction


# 画画 ,打印坦克
def pr():
    global direction

    # 根据方向,然后修改坦克位置,加一
    if direction == "right":
        NowPos.x += 1
        for item in Position:
            item.x += 1

    elif direction == "up":
        NowPos.y -= 1
        for item in Position:
            item.y -= 1
    elif direction == "down":
        NowPos.y += 1
        for item in Position:
            item.y += 1
    else:
        NowPos.x -= 1
        for item in Position:
            item.x -= 1
    t1 = threading.Timer(0.3, pr)
    t1.start()
    if NowPos.x > 30 or NowPos.x < 0:
        t1.cancel()
        gameover(screen)
    if NowPos.y > 30 or NowPos.y < 0:
        t1.cancel()
        gameover(screen)


# 定义四种坦克的位置
Position = [
    pos(0, 0), pos(0, 1), pos(1, 0), pos(1, 1), pos(0, 2), pos(1, 2), pos(2, 1)
]
# 像右跑
RightPos = [
    pos(1, 1), pos(0, 0), pos(0, 1), pos(0, 2), pos(1, 0), pos(1, 2), pos(2, 1)
]
# 向左跑
LeftPos = [
    pos(1, 1), pos(0, 1), pos(1, 0), pos(1, 2), pos(2, 0), pos(2, 2), pos(2, 1)
]
# 向上跑
UpPos = [
    pos(1, 1), pos(0, 1), pos(1, 0), pos(2, 1), pos(0, 2), pos(1, 2), pos(2, 2)
]
# 向下跑
DownPos = [
    pos(1, 1), pos(0, 0), pos(1, 0), pos(2, 0), pos(0, 1), pos(1, 2), pos(2, 1)
]

# 坦克运动的方向
direction = "right"

# 坦克的现在所属位置
NowPos = pos(0, 0)


# 修改坦克的方向
def change():
    i = 0
    if direction == "right":
        for item in Position:
            Position[i].x = RightPos[i].x + NowPos.x
            Position[i].y = RightPos[i].y + NowPos.y
            i += 1
    elif direction == "up":
        for item in Position:
            Position[i].x = UpPos[i].x + NowPos.x
            Position[i].y = UpPos[i].y + NowPos.y
            i += 1
    elif direction == "down":
        for item in Position:
            Position[i].x = DownPos[i].x + NowPos.x
            Position[i].y = DownPos[i].y + NowPos.y
            i += 1
    else:
        for item in Position:
            Position[i].x = LeftPos[i].x + NowPos.x
            Position[i].y = LeftPos[i].y + NowPos.y
            i += 1


#  子弹的布局
# 子弹类的所属
class Bullet(object):
    def __init__(self, Pos, Direction):
        self.pos = Pos
        self.direction = Direction

    def prbullet(self):

        if self.direction == "right":
            self.pos.x += 1
        elif self.direction == "up":
            self.pos.y -= 1
        elif self.direction == "down":
            self.pos.y += 1
        else:
            self.pos.x -= 1

        btn1 = threading.Timer(0.2, self.prbullet)
        btn1.start()
        # 当子弹已经超过边框范围时,线程就自动结束
        if self.pos.x > 32 or self.pos.x < 0:
            btn1.cancel()
        if self.pos.y > 32 or self.pos.y < 0:
            btn1.cancel()


# 子弹的数组
bullet = []


# 游戏结束的场景设置
def gameover(playSurface):
    screen.fill(blackColor)
    gameOverFont = pygame.font.SysFont('arial', 72)
    gameOverSurf = gameOverFont.render('Game Over', True, greyColor)
    gameOverRect = gameOverSurf.get_rect()
    gameOverRect.midtop = (320, 125)
    playSurface.blit(gameOverSurf, gameOverRect)
    pygame.display.flip()
    time.sleep(5)
    pygame.quit()
    sys.exit()


# 调控帧数的
def update():
    print("大家好")
    screen.fill(blackColor)
    for item in Position:
        pygame.draw.rect(screen, RedColor, Rect(item.x * 20, item.y * 20, 20, 20))

    for item in bullet:
        pygame.draw.rect(screen, RedColor, Rect(item.pos.x * 20, item.pos.y * 20, 20, 20))

    pygame.display.flip()

    # 每 0.05秒刷新页面
    t = threading.Timer(0.005, update)
    t.start()
    # 当坦克超过范围时,就游戏结束
    if NowPos.x > 30 or NowPos.x < 0:
        t.cancel()
        gameover(screen)
    if NowPos.y > 30 or NowPos.y < 0:
        t.cancel()
        gameover(screen)


# 窗口的初始化
def main():
    global direction
    pygame.init()
    pygame.display.set_caption("坦克大战")
    pygame.display.flip()

    # 刷新坦克的信息
    pr()
    update()
    while True:
        for event in pygame.event.get():

            # 事件类的判断

            if event.type == QUIT:
                pygame.quit()
                sys.exit()

            #     判断是否按下了方向键
            elif event.type == KEYDOWN:
                if event.key == K_w or event.key == K_UP:
                    if direction != "down":
                        direction = "up"
                        print("按下了w键")

                elif event.key == K_a or event.key == K_LEFT:
                    if direction != "right":
                        direction = "left"
                        print("按下了a键")

                elif event.key == K_s or event.key == K_DOWN:
                    if direction != "up":
                        direction = "down"
                        print("按下了s键")
                elif event.key == K_d or event.key == K_RIGHT:
                    if direction != "left":
                        direction = "right"
                        print("按下了d键")
                elif event.key == K_j:
                    if direction == "right":
                        b1 = Bullet(pos(NowPos.x + 2, NowPos.y + 1), direction)
                    elif direction == "left":
                        b1 = Bullet(pos(NowPos.x, NowPos.y + 1), direction)
                    elif direction == "up":
                        b1 = Bullet(pos(NowPos.x + 1, NowPos.y), direction)
                    elif direction == "down":
                        b1 = Bullet(pos(NowPos.x + 1, NowPos.y + 2), direction)
                    b1.prbullet()
                    bullet.append(b1)

            # 每次按键后,修改坦克的方向
            change()


# 主函数入口
if __name__ == "__main__":
    main()