博客信息

Python游戏开发之坦克大战 v1.x

发布时间:『 2019-07-03 11:13』  博客类别:Python  阅读(628)

坦克大战v1.x

游戏业务(面向对象的思想分析游戏)

    坦克大战

        主逻辑类:

 

        坦克类:(我方坦克,敌方坦克)

 

        子弹类:

 

        爆炸类:

 

        音效类:

 

v1.0:加载出游戏窗口

 

思路:

1、初始化显示窗口

2、在显示窗口中,初始化表面(画布)大小

3、设置窗口的标题

4、给表面填充颜色

5、更新显示窗口

......

 

v1.1 新增功能:

    1.解决点击关闭按钮,程序未响应的bug

    2.事件处理,方向键控制,发射按键控制

思路:

1、给窗体添加事件

上下左右、space、退出窗体

......

 

v1.2 新增功能:

    1.实现左上角剩余敌方坦克提示

        a、选一个字体

        b、使用指定的字体绘制文字

        c、将小画布贴到窗口中

......

 

v1.3 新增功能:

    1.引入精灵类

    2.完善坦克类的封装

    3.让坦克在窗口中展示

......

 

v1.4 新增功能:

    1.实现坦克的移动

......

 

v1.5 新增功能:

    1.优化坦克的移动方式

        按下方向键坦克持续移动

        松开方向键坦克停止移动

......

 

v1.6 新增功能:

    1. 敌方坦克类的实现

    2. 随机创建5个敌方坦克(坦克位置,随机)

......

 

v1.7 新增功能:

1. 实现敌方坦克的随机移动


1、加载游戏窗口

import pygame

# v1.0引入pygame中的显示类
_display = pygame.display
# v1.0引入pygame中的颜色类
_color = pygame.Color

class MainGame:
    window = None
    screen_width=800
    screen_height=600
    title = '坦克大战v1.0 powered by 【www.javaxl.com】'
    def start(self):
        _display.init()
        MainGame.window=_display.set_mode([MainGame.screen_width,MainGame.screen_height])
        _display.set_caption(MainGame.title)
        while True:
            MainGame.window.fill(_color(0,0,0))
            _display.update()

    def gameOver(self):
        exit()


class Tank:
    pass
class MyTank(Tank):
    pass
class EnemyTank(Tank):
    pass
class Bullet:
    pass
class Explode:
    pass
class Music:
    pass

game=MainGame()
game.start()


2、游戏窗口添加事件

import pygame

# v1.0引入pygame中的显示类
_display = pygame.display
# v1.0引入pygame中的颜色类
_color = pygame.Color
# v1.1引入pygame中的事件类
_event=pygame.event

class MainGame:
    window = None
    screen_width = 800
    screen_height = 600
    title = '坦克大战v1.2 powered by 【www.javaxl.com】'

    def start(self):
        _display.init()
        MainGame.window = _display.set_mode([MainGame.screen_width, MainGame.screen_height])
        _display.set_caption(MainGame.title)
        while True:
            MainGame.window.fill(_color(0, 0, 0))

            # v1.1引入的操作自己坦克的方法
            self.get_event()

            _display.update()

    # v1.1引入的结束游戏窗口的方法
    def game_over(self):
        exit()

    # v1.1引入的操作自己坦克的方法
    def get_event(self):
        event_list=_event.get()
        # 获取所有的事件
        for e in event_list:
            # 外层判断的事件的类别
            if e.type == pygame.QUIT:
                print('退出游戏')
                self.game_over()
            elif e.type == pygame.KEYDOWN:
                # 内层判断的具体事件(按下的是哪个键)
                if e.key == pygame.K_UP:
                    print('向上移动')
                elif e.key == pygame.K_DOWN:
                    print('向下移动')
                elif e.key == pygame.K_LEFT:
                    print('向左移动')
                elif e.key == pygame.K_RIGHT:
                    print('向右移动')
                elif e.key == pygame.K_SPACE:
                    print('子弹发射 biu biu biu  - - -')

class Tank:
    pass


class MyTank(Tank):
    pass


class EnemyTank(Tank):
    pass


class Bullet:
    pass


class Explode:
    pass


class Music:
    pass


game = MainGame()
game.start()


显示模块中的类及方法,可以在idea中进行设置

小李飞刀_Python


3、敌方坦克数量提示

import pygame

# v1.0引入pygame中的显示类
_display = pygame.display
# v1.0引入pygame中的颜色类
_color = pygame.Color
# v1.1引入pygame中的事件类
_event=pygame.event
# v1.2引入pygame中的事件类
_font=pygame.font

class MainGame:
    window = None
    screen_width = 800
    screen_height = 600
    title = '坦克大战v1.2 powered by 【www.javaxl.com】'

    def start(self):
        _display.init()
        MainGame.window = _display.set_mode([MainGame.screen_width, MainGame.screen_height])
        _display.set_caption(MainGame.title)
        while True:
            MainGame.window.fill(_color(0, 0, 0))
            # v1.2初始化左上角字体表面(剩余坦克数量,距离左上角(10,10)的位置显示)
            MainGame.window.blit(self.draw_text('敌方坦克剩余%d个'%6),(10,10))
            # v1.1引入的操作自己坦克的方法
            self.get_event()
            # v1.2初始化左上角字体表面(剩余坦克数量)

            _display.update()

    # v1.1引入的结束游戏窗口的方法
    def game_over(self):
        exit()

    # v1.1引入的操作自己坦克的方法
    def get_event(self):
        event_list=_event.get()
        # 获取所有的事件
        for e in event_list:
            # 外层判断的事件的类别
            if e.type == pygame.QUIT:
                print('退出游戏')
                self.game_over()
            elif e.type == pygame.KEYDOWN:
                # 内层判断的具体事件(按下的是哪个键)
                if e.key == pygame.K_UP:
                    print('向上移动')
                elif e.key == pygame.K_DOWN:
                    print('向下移动')
                elif e.key == pygame.K_LEFT:
                    print('向左移动')
                elif e.key == pygame.K_RIGHT:
                    print('向右移动')
                elif e.key == pygame.K_SPACE:
                    print('子弹发射 biu biu biu  - - -')

    # v1.2初始化左上角字体表面(剩余坦克数量)
    def draw_text(self,content):
        _font.init()
        font = _font.SysFont('kaiti',20)
        text_surface=font.render(content,True,_color(0,0,255))
        return text_surface

class Tank:
    pass


class MyTank(Tank):
    pass


class EnemyTank(Tank):
    pass


class Bullet:
    pass


class Explode:
    pass


class Music:
    pass


game = MainGame()
game.start()


4、我方坦克登场

import pygame

# v1.0引入pygame中的显示类
_display = pygame.display
# v1.0引入pygame中的颜色类
_color = pygame.Color
# v1.1引入pygame中的事件类
_event = pygame.event
# v1.2引入pygame中的事件类
_font = pygame.font


class MainGame:
    window = None
    screen_width = 800
    screen_height = 600
    title = '坦克大战v1.3 powered by 【www.javaxl.com】'
    my_tank=None

    def start(self):
        _display.init()
        MainGame.window = _display.set_mode([MainGame.screen_width, MainGame.screen_height])
        _display.set_caption(MainGame.title)
        # v1.3生成我方坦克
        MainGame.my_tank=MyTank(200,450,3)
        while True:
            MainGame.window.fill(_color(0, 0, 0))
            # v1.2初始化左上角字体表面(剩余坦克数量,距离左上角(10,10)的位置显示)
            MainGame.window.blit(self.draw_text('敌方坦克剩余%d个' % 6), (10, 10))
            # v1.1引入的操作自己坦克的方法
            self.get_event()
            # v1.2初始化左上角字体表面(剩余坦克数量)
            # v1.3展示我方坦克
            MainGame.my_tank.display()

            _display.update()

    # v1.1引入的结束游戏窗口的方法
    def game_over(self):
        exit()

    # v1.1引入的操作自己坦克的方法
    def get_event(self):
        event_list = _event.get()
        # 获取所有的事件
        for e in event_list:
            # 外层判断的事件的类别
            if e.type == pygame.QUIT:
                print('退出游戏')
                self.game_over()
            elif e.type == pygame.KEYDOWN:
                # 内层判断的具体事件(按下的是哪个键)
                if e.key == pygame.K_UP:
                    print('向上移动')
                elif e.key == pygame.K_DOWN:
                    print('向下移动')
                elif e.key == pygame.K_LEFT:
                    print('向左移动')
                elif e.key == pygame.K_RIGHT:
                    print('向右移动')
                elif e.key == pygame.K_SPACE:
                    print('子弹发射 biu biu biu  - - -')

    # v1.2初始化左上角字体表面(剩余坦克数量)
    def draw_text(self, content):
        _font.init()
        font = _font.SysFont('kaiti', 20)
        text_surface = font.render(content, True, _color(0, 0, 255))
        return text_surface


# v1.3作为一个精灵类的基类
class BaseItem(pygame.sprite.Sprite):
    def __init__(self, color, width, height):
        pygame.sprite.Sprite.__init__(self)


# v1.3描述坦克基类(基于我方坦克,先进行描述)
class Tank(BaseItem):
    def __init__(self,left,top,speed):
        self.images={
            'U':pygame.image.load('img/p1tankU.gif'),
            'D':pygame.image.load('img/p1tankD.gif'),
            'L':pygame.image.load('img/p1tankL.gif'),
            'R':pygame.image.load('img/p1tankR.gif')
        }
        # 坦克默认初始化方向
        self.direction='U'
        self.image=self.images[self.direction]

        # 坦克的区域(left,top,width,heigt) 坦克位置及坦克大小
        self.rect = self.image.get_rect()
        self.rect.left=left
        self.rect.top=top
        self.speed=speed

    def display(self):
        self.image=self.images[self.direction]
        MainGame.window.blit(self.image,self.rect)


class MyTank(Tank):
    pass


class EnemyTank(Tank):
    pass


class Bullet:
    pass


class Explode:
    pass


class Music:
    pass


game = MainGame()
game.start()


5、我方坦克移动

import pygame

# v1.0引入pygame中的显示类
_display = pygame.display
# v1.0引入pygame中的颜色类
_color = pygame.Color
# v1.1引入pygame中的事件类
_event = pygame.event
# v1.2引入pygame中的事件类
_font = pygame.font


class MainGame:
    window = None
    screen_width = 800
    screen_height = 600
    title = '坦克大战v1.4 powered by 【www.javaxl.com】'
    my_tank=None

    def start(self):
        _display.init()
        MainGame.window = _display.set_mode([MainGame.screen_width, MainGame.screen_height])
        _display.set_caption(MainGame.title)
        # v1.3生成我方坦克
        MainGame.my_tank=MyTank(200,450,3)
        while True:
            MainGame.window.fill(_color(0, 0, 0))
            # v1.2初始化左上角字体表面(剩余坦克数量,距离左上角(10,10)的位置显示)
            MainGame.window.blit(self.draw_text('敌方坦克剩余%d个' % 6), (10, 10))
            # v1.1引入的操作自己坦克的方法
            self.get_event()
            # v1.2初始化左上角字体表面(剩余坦克数量)
            # v1.3展示我方坦克
            MainGame.my_tank.display()

            _display.update()

    # v1.1引入的结束游戏窗口的方法
    def game_over(self):
        exit()

    # v1.1引入的操作自己坦克的方法
    def get_event(self):
        event_list = _event.get()
        # 获取所有的事件
        for e in event_list:
            # 外层判断的事件的类别
            if e.type == pygame.QUIT:
                print('退出游戏')
                self.game_over()
            elif e.type == pygame.KEYDOWN:
                # 内层判断的具体事件(按下的是哪个键)
                if e.key == pygame.K_UP:
                    print('向上移动')
                    MainGame.my_tank.direction='U'
                    if MainGame.my_tank.rect.top >0:
                        MainGame.my_tank.rect.top -= MainGame.my_tank.speed
                elif e.key == pygame.K_DOWN:
                    print('向下移动')
                    MainGame.my_tank.direction='D'
                    if MainGame.my_tank.rect.top<(MainGame.screen_height-MainGame.my_tank.rect.height):
                        MainGame.my_tank.rect.top += MainGame.my_tank.speed
                elif e.key == pygame.K_LEFT:
                    print('向左移动')
                    MainGame.my_tank.direction='L'
                    if MainGame.my_tank.rect.left>0:
                        MainGame.my_tank.rect.left -= MainGame.my_tank.speed
                elif e.key == pygame.K_RIGHT:
                    print('向右移动')
                    MainGame.my_tank.direction='R'
                    if MainGame.my_tank.rect.left<(MainGame.screen_width-MainGame.my_tank.rect.width):
                        MainGame.my_tank.rect.left += MainGame.my_tank.speed
                elif e.key == pygame.K_SPACE:
                    print('子弹发射 biu biu biu  - - -')

    # v1.2初始化左上角字体表面(剩余坦克数量)
    def draw_text(self, content):
        _font.init()
        font = _font.SysFont('kaiti', 20)
        text_surface = font.render(content, True, _color(0, 0, 255))
        return text_surface


# v1.3作为一个精灵类的基类
class BaseItem(pygame.sprite.Sprite):
    def __init__(self, color, width, height):
        pygame.sprite.Sprite.__init__(self)


# v1.3描述坦克基类(基于我方坦克,先进行描述)
class Tank(BaseItem):
    def __init__(self,left,top,speed):
        self.images={
            'U':pygame.image.load('img/p1tankU.gif'),
            'D':pygame.image.load('img/p1tankD.gif'),
            'L':pygame.image.load('img/p1tankL.gif'),
            'R':pygame.image.load('img/p1tankR.gif')
        }
        # 坦克默认初始化方向
        self.direction='U'
        self.image=self.images[self.direction]

        # 坦克的区域(left,top,width,heigt) 坦克位置及坦克大小
        self.rect = self.image.get_rect()
        self.rect.left=left
        self.rect.top=top
        self.speed=speed

    def display(self):
        self.image=self.images[self.direction]
        MainGame.window.blit(self.image,self.rect)

    def move(self):
        pass


class MyTank(Tank):
    pass


class EnemyTank(Tank):
    pass


class Bullet:
    pass


class Explode:
    pass


class Music:
    pass


game = MainGame()
game.start()


6、优化我方坦克移动方式(长按方向键移动)

import pygame
import time

# v1.0引入pygame中的显示类
_display = pygame.display
# v1.0引入pygame中的颜色类
_color = pygame.Color
# v1.1引入pygame中的事件类
_event = pygame.event
# v1.2引入pygame中的事件类
_font = pygame.font


class MainGame:
    window = None
    screen_width = 800
    screen_height = 600
    title = '坦克大战v1.5 powered by 【www.javaxl.com】'
    my_tank=None

    def start(self):
        _display.init()
        MainGame.window = _display.set_mode([MainGame.screen_width, MainGame.screen_height])
        _display.set_caption(MainGame.title)
        # v1.3生成我方坦克
        MainGame.my_tank=MyTank(200,450,3)
        while True:
            MainGame.window.fill(_color(0, 0, 0))
            # v1.2初始化左上角字体表面(剩余坦克数量,距离左上角(10,10)的位置显示)
            MainGame.window.blit(self.draw_text('敌方坦克剩余%d个' % 6), (10, 10))
            # v1.1引入的操作自己坦克的方法
            self.get_event()
            # v1.2初始化左上角字体表面(剩余坦克数量)
            # v1.3展示我方坦克
            MainGame.my_tank.display()
            # v1.5我方坦克移动
            if not MainGame.my_tank.stop:
                MainGame.my_tank.move()

            _display.update()
            # 为了方便看坦克的移动效果,系统休眠0.015s
            time.sleep(0.015)

    # v1.1引入的结束游戏窗口的方法
    def game_over(self):
        exit()

    # v1.1引入的操作自己坦克的方法
    def get_event(self):
        event_list = _event.get()
        # 获取所有的事件
        for e in event_list:
            # 外层判断的事件的类别
            if e.type == pygame.QUIT:
                print('退出游戏')
                self.game_over()
            elif e.type == pygame.KEYUP:
                MainGame.my_tank.stop=True
            elif e.type == pygame.KEYDOWN:
                # 内层判断的具体事件(按下的是哪个键)
                if e.key == pygame.K_UP:
                    print('向上移动')
                    MainGame.my_tank.direction='U'
                    # MainGame.my_tank.move()
                    MainGame.my_tank.stop=False
                elif e.key == pygame.K_DOWN:
                    print('向下移动')
                    MainGame.my_tank.direction='D'
                    # MainGame.my_tank.move()
                    MainGame.my_tank.stop=False
                elif e.key == pygame.K_LEFT:
                    print('向左移动')
                    MainGame.my_tank.direction='L'
                    # MainGame.my_tank.move()
                    MainGame.my_tank.stop=False
                elif e.key == pygame.K_RIGHT:
                    print('向右移动')
                    MainGame.my_tank.direction='R'
                    # MainGame.my_tank.move()
                    MainGame.my_tank.stop=False
                elif e.key == pygame.K_SPACE:
                    print('子弹发射 biu biu biu  - - -')

    # v1.2初始化左上角字体表面(剩余坦克数量)
    def draw_text(self, content):
        _font.init()
        font = _font.SysFont('kaiti', 20)
        text_surface = font.render(content, True, _color(0, 0, 255))
        return text_surface


# v1.3作为一个精灵类的基类
class BaseItem(pygame.sprite.Sprite):
    def __init__(self, color, width, height):
        pygame.sprite.Sprite.__init__(self)


# v1.3描述坦克基类(基于我方坦克,先进行描述)
class Tank(BaseItem):
    def __init__(self,left,top,speed):
        self.images={
            'U':pygame.image.load('img/p1tankU.gif'),
            'D':pygame.image.load('img/p1tankD.gif'),
            'L':pygame.image.load('img/p1tankL.gif'),
            'R':pygame.image.load('img/p1tankR.gif')
        }
        # 坦克默认初始化方向
        self.direction='U'
        self.image=self.images[self.direction]

        # 坦克的区域(left,top,width,heigt) 坦克位置及坦克大小
        self.rect = self.image.get_rect()
        self.rect.left=left
        self.rect.top=top
        self.speed=speed
        # v1.5按下方向键坦克持续移动,松开方向键坦克停止移动
        self.stop=True

    # v1.3展现坦克对象
    def display(self):
        self.image=self.images[self.direction]
        MainGame.window.blit(self.image,self.rect)

    # v1.5优化坦克移动
    def move(self):
        if self.direction=='U':
            if self.rect.top >0:
                self.rect.top -= self.speed
        elif self.direction=='D':
            if self.rect.top<(MainGame.screen_height-self.rect.height):
                self.rect.top += self.speed
        elif self.direction=='L':
            if self.rect.left>0:
                self.rect.left -= self.speed
        elif self.direction=='R':
            if self.rect.left<(MainGame.screen_width-self.rect.width):
                self.rect.left += self.speed


class MyTank(Tank):
    pass


class EnemyTank(Tank):
    pass


class Bullet:
    pass


class Explode:
    pass


class Music:
    pass


game = MainGame()
game.start()


7、随机产生6个敌方坦克

import pygame
import time
import random

# v1.0引入pygame中的显示类
_display = pygame.display
# v1.0引入pygame中的颜色类
_color = pygame.Color
# v1.1引入pygame中的事件类
_event = pygame.event
# v1.2引入pygame中的事件类
_font = pygame.font


class MainGame:
    window = None
    screen_width = 800
    screen_height = 600
    title = '坦克大战v1.6 powered by 【www.javaxl.com】'
    # 我方坦克
    my_tank = None
    # 敌方坦克列表
    enemy_tanks = []
    # 敌方坦克默认生成数量
    enemy_tank_count = 6

    def start(self):
        _display.init()
        MainGame.window = _display.set_mode([MainGame.screen_width, MainGame.screen_height])
        _display.set_caption(MainGame.title)
        # v1.3生成我方坦克
        MainGame.my_tank = MyTank(200, 450, 3)
        # v1.6生成敌方坦克
        self.create_enemy_tanks()
        while True:
            MainGame.window.fill(_color(0, 0, 0))
            # v1.2初始化左上角字体表面(剩余坦克数量,距离左上角(10,10)的位置显示)
            MainGame.window.blit(self.draw_text('敌方坦克剩余%d个' % 6), (10, 10))
            # v1.1引入的操作自己坦克的方法
            self.get_event()
            # v1.2初始化左上角字体表面(剩余坦克数量)
            # v1.3展示我方坦克
            MainGame.my_tank.display()
            # v1.6展示敌方坦克
            for e_tank in MainGame.enemy_tanks:
                e_tank.display()

            # v1.5我方坦克移动
            if not MainGame.my_tank.stop:
                MainGame.my_tank.move()

            _display.update()
            # 为了方便看坦克的移动效果,系统休眠0.015s
            time.sleep(0.015)

    # v1.1引入的结束游戏窗口的方法
    def game_over(self):
        exit()

    # v1.1引入的操作自己坦克的方法
    def get_event(self):
        event_list = _event.get()
        # 获取所有的事件
        for e in event_list:
            # 外层判断的事件的类别
            if e.type == pygame.QUIT:
                print('退出游戏')
                self.game_over()
            elif e.type == pygame.KEYUP:
                MainGame.my_tank.stop = True
            elif e.type == pygame.KEYDOWN:
                # 内层判断的具体事件(按下的是哪个键)
                if e.key == pygame.K_UP:
                    print('向上移动')
                    MainGame.my_tank.direction = 'U'
                    # MainGame.my_tank.move()
                    MainGame.my_tank.stop = False
                elif e.key == pygame.K_DOWN:
                    print('向下移动')
                    MainGame.my_tank.direction = 'D'
                    # MainGame.my_tank.move()
                    MainGame.my_tank.stop = False
                elif e.key == pygame.K_LEFT:
                    print('向左移动')
                    MainGame.my_tank.direction = 'L'
                    # MainGame.my_tank.move()
                    MainGame.my_tank.stop = False
                elif e.key == pygame.K_RIGHT:
                    print('向右移动')
                    MainGame.my_tank.direction = 'R'
                    # MainGame.my_tank.move()
                    MainGame.my_tank.stop = False
                elif e.key == pygame.K_SPACE:
                    print('子弹发射 biu biu biu  - - -')

    # v1.2初始化左上角字体表面(剩余坦克数量)
    def draw_text(self, content):
        _font.init()
        font = _font.SysFont('kaiti', 20)
        text_surface = font.render(content, True, _color(0, 0, 255))
        return text_surface

    # v1.6创建敌方坦克
    def create_enemy_tanks(self):
        for i in range(MainGame.enemy_tank_count):
            MainGame.enemy_tanks.append(EnemyTank())


# v1.3作为一个精灵类的基类
class BaseItem(pygame.sprite.Sprite):
    def __init__(self, color, width, height):
        pygame.sprite.Sprite.__init__(self)


# v1.3描述坦克基类(基于我方坦克,先进行描述)
class Tank(BaseItem):
    def __init__(self, left, top, speed):
        self.images = {
            'U': pygame.image.load('img/p1tankU.gif'),
            'D': pygame.image.load('img/p1tankD.gif'),
            'L': pygame.image.load('img/p1tankL.gif'),
            'R': pygame.image.load('img/p1tankR.gif')
        }
        # 坦克默认初始化方向
        self.direction = 'U'
        self.image = self.images[self.direction]

        # 坦克的区域(left,top,width,heigt) 坦克位置及坦克大小
        self.rect = self.image.get_rect()
        self.rect.left = left
        self.rect.top = top
        self.speed = speed
        # v1.5按下方向键坦克持续移动,松开方向键坦克停止移动
        self.stop = True

    # v1.3展现坦克对象
    def display(self):
        self.image = self.images[self.direction]
        MainGame.window.blit(self.image, self.rect)

    # v1.5优化坦克移动
    def move(self):
        if self.direction == 'U':
            if self.rect.top > 0:
                self.rect.top -= self.speed
        elif self.direction == 'D':
            if self.rect.top < (MainGame.screen_height - self.rect.height):
                self.rect.top += self.speed
        elif self.direction == 'L':
            if self.rect.left > 0:
                self.rect.left -= self.speed
        elif self.direction == 'R':
            if self.rect.left < (MainGame.screen_width - self.rect.width):
                self.rect.left += self.speed


class MyTank(Tank):
    pass


# 1.6敌方坦克出场
class EnemyTank(Tank):
    def __init__(self):
        self.images = {
            'U': pygame.image.load('img/enemy1U.gif'),
            'D': pygame.image.load('img/enemy1D.gif'),
            'L': pygame.image.load('img/enemy1L.gif'),
            'R': pygame.image.load('img/enemy1R.gif')
        }
        # 坦克默认初始化方向
        self.direction = 'U'
        self.random_direction()
        self.image = self.images[self.direction]

        # 坦克的区域(left,top,width,heigt) 坦克位置及坦克大小
        self.rect = self.image.get_rect()
        self.rect.left = 100 * random.randint(1, 8)
        self.rect.top = 80 * random.randint(1, 6)
        self.speed = random.randint(1, 3)
        # v1.5按下方向键坦克持续移动,松开方向键坦克停止移动
        self.stop = True

    # 1.6敌方坦克出场方向随机
    def random_direction(self):
        num = random.randint(1, 4)
        if num == 1:
            self.direction = 'U'
        elif num == 2:
            self.direction = 'D'
        elif num == 3:
            self.direction = 'L'
        elif num == 4:
            self.direction = 'R'


class Bullet:
    pass


class Explode:
    pass


class Music:
    pass


game = MainGame()
game.start()


8、敌方坦克随机移动

import pygame
import time
import random

# v1.0引入pygame中的显示类
_display = pygame.display
# v1.0引入pygame中的颜色类
_color = pygame.Color
# v1.1引入pygame中的事件类
_event = pygame.event
# v1.2引入pygame中的事件类
_font = pygame.font


class MainGame:
    window = None
    screen_width = 800
    screen_height = 600
    title = '坦克大战v1.7 powered by 【www.javaxl.com】'
    # 我方坦克
    my_tank = None
    # 敌方坦克列表
    enemy_tanks = []
    # 敌方坦克默认生成数量
    enemy_tank_count = 6

    def start(self):
        _display.init()
        MainGame.window = _display.set_mode([MainGame.screen_width, MainGame.screen_height])
        _display.set_caption(MainGame.title)
        # v1.3生成我方坦克
        MainGame.my_tank = MyTank(200, 450, 3)
        # v1.6生成敌方坦克
        self.create_enemy_tanks()
        while True:
            MainGame.window.fill(_color(0, 0, 0))
            # v1.2初始化左上角字体表面(剩余坦克数量,距离左上角(10,10)的位置显示)
            MainGame.window.blit(self.draw_text('敌方坦克剩余%d个' % 6), (10, 10))
            # v1.1引入的操作自己坦克的方法
            self.get_event()
            # v1.2初始化左上角字体表面(剩余坦克数量)
            # v1.3展示我方坦克
            MainGame.my_tank.display()
            # v1.6展示敌方坦克
            for e_tank in MainGame.enemy_tanks:
                e_tank.display()
                # v1.7敌方坦克移动
                e_tank.move()
            # v1.5我方坦克移动
            if not MainGame.my_tank.stop:
                MainGame.my_tank.move()

            _display.update()
            # 为了方便看坦克的移动效果,系统休眠0.015s
            time.sleep(0.015)

    # v1.1引入的结束游戏窗口的方法
    def game_over(self):
        exit()

    # v1.1引入的操作自己坦克的方法
    def get_event(self):
        event_list = _event.get()
        # 获取所有的事件
        for e in event_list:
            # 外层判断的事件的类别
            if e.type == pygame.QUIT:
                print('退出游戏')
                self.game_over()
            elif e.type == pygame.KEYUP:
                MainGame.my_tank.stop = True
            elif e.type == pygame.KEYDOWN:
                # 内层判断的具体事件(按下的是哪个键)
                if e.key == pygame.K_UP:
                    print('向上移动')
                    MainGame.my_tank.direction = 'U'
                    # MainGame.my_tank.move()
                    MainGame.my_tank.stop = False
                elif e.key == pygame.K_DOWN:
                    print('向下移动')
                    MainGame.my_tank.direction = 'D'
                    # MainGame.my_tank.move()
                    MainGame.my_tank.stop = False
                elif e.key == pygame.K_LEFT:
                    print('向左移动')
                    MainGame.my_tank.direction = 'L'
                    # MainGame.my_tank.move()
                    MainGame.my_tank.stop = False
                elif e.key == pygame.K_RIGHT:
                    print('向右移动')
                    MainGame.my_tank.direction = 'R'
                    # MainGame.my_tank.move()
                    MainGame.my_tank.stop = False
                elif e.key == pygame.K_SPACE:
                    print('子弹发射 biu biu biu  - - -')

    # v1.2初始化左上角字体表面(剩余坦克数量)
    def draw_text(self, content):
        _font.init()
        font = _font.SysFont('kaiti', 20)
        text_surface = font.render(content, True, _color(0, 0, 255))
        return text_surface

    # v1.6创建敌方坦克
    def create_enemy_tanks(self):
        for i in range(MainGame.enemy_tank_count):
            MainGame.enemy_tanks.append(EnemyTank())


# v1.3作为一个精灵类的基类
class BaseItem(pygame.sprite.Sprite):
    def __init__(self, color, width, height):
        pygame.sprite.Sprite.__init__(self)


# v1.3描述坦克基类(基于我方坦克,先进行描述)
class Tank(BaseItem):
    def __init__(self, left, top, speed):
        self.images = {
            'U': pygame.image.load('img/p1tankU.gif'),
            'D': pygame.image.load('img/p1tankD.gif'),
            'L': pygame.image.load('img/p1tankL.gif'),
            'R': pygame.image.load('img/p1tankR.gif')
        }
        # 坦克默认初始化方向
        self.direction = 'U'
        self.image = self.images[self.direction]

        # 坦克的区域(left,top,width,heigt) 坦克位置及坦克大小
        self.rect = self.image.get_rect()
        self.rect.left = left
        self.rect.top = top
        self.speed = speed
        # v1.5按下方向键坦克持续移动,松开方向键坦克停止移动
        self.stop = True

    # v1.3展现坦克对象
    def display(self):
        self.image = self.images[self.direction]
        MainGame.window.blit(self.image, self.rect)

    # v1.5优化坦克移动
    def move(self):
        if self.direction == 'U':
            if self.rect.top > 0:
                self.rect.top -= self.speed
        elif self.direction == 'D':
            if self.rect.top < (MainGame.screen_height - self.rect.height):
                self.rect.top += self.speed
        elif self.direction == 'L':
            if self.rect.left > 0:
                self.rect.left -= self.speed
        elif self.direction == 'R':
            if self.rect.left < (MainGame.screen_width - self.rect.width):
                self.rect.left += self.speed


class MyTank(Tank):
    pass


# 1.6敌方坦克出场
class EnemyTank(Tank):
    def __init__(self):
        self.images = {
            'U': pygame.image.load('img/enemy1U.gif'),
            'D': pygame.image.load('img/enemy1D.gif'),
            'L': pygame.image.load('img/enemy1L.gif'),
            'R': pygame.image.load('img/enemy1R.gif')
        }
        # 坦克默认初始化方向
        self.direction = 'U'
        self.random_direction()
        self.image = self.images[self.direction]

        # 坦克的区域(left,top,width,heigt) 坦克位置及坦克大小
        self.rect = self.image.get_rect()
        self.rect.left = 100 * random.randint(1, 8)
        self.rect.top = 80 * random.randint(1, 6)
        self.speed = random.randint(1, 3)
        # v1.5按下方向键坦克持续移动,松开方向键坦克停止移动
        self.stop = True
        # 1.7敌方坦克沿着一个方向行走60步,改变一次方向
        self.step=60

    # 1.6敌方坦克出场方向随机
    def random_direction(self):
        num = random.randint(1, 4)
        if num == 1:
            self.direction = 'U'
        elif num == 2:
            self.direction = 'D'
        elif num == 3:
            self.direction = 'L'
        elif num == 4:
            self.direction = 'R'

    # 1.7敌方坦克随机移动
    def move(self):
        if self.step==0:
            self.random_direction()
            self.step=60
        else:
            super().move()
            self.step-=1


class Bullet:
    pass


class Explode:
    pass


class Music:
    pass


game = MainGame()
game.start()


项目所需gif图片

tank_gif.zip 


坦克大战v1.x版本的最终效果图

小李飞刀_Python


over......


关键字:     Python       游戏开发  

备案号:湘ICP备19000029号

Copyright © 2018-2019 javaxl晓码阁 版权所有