栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

自制python小游戏 (最后的英雄)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

自制python小游戏 (最后的英雄)

主程序__main__ 在这里开始run

# *-* coding:utf8 *-*
import pygame
from _tools_ import *


class Play(object):
    def __init__(self):
        print("游戏初始化")
        pygame.display.set_caption("最后的英雄")
        self.zjm = pygame.display.set_mode(ZJM_CL.size)
        self.__chu_ang()
        self.zh_en = pygame.time.Clock()
        pygame.time.set_timer(DJ_CL, 4000)

    def __chu_ang(self):
        bei = Bei()
        self.bei_z = pygame.sprite.Group(bei)

        self.di_z = pygame.sprite.Group()

        self.hero_j = Hero()
        self.hero_z = pygame.sprite.Group(self.hero_j)

    def play_game(self):
        while True:
            self.zh_en.tick(60)
            self.__ja_t()
            self.__pen_g()
            self.__dao()

            pygame.display.update()

    def __ja_t(self):
        for i in pygame.event.get():
            if i.type == pygame.QUIT:
                Play.__game_over()
            elif i.type == DJ_CL:
                self.di_j = Dji()
                self.di_z.add(self.di_j)
            # elif i.type == pygame.KEYDOWN and pygame.K_KP1:
            #     self.hero_j.yi_j()
        sws = pygame.key.get_pressed()
        if sws[pygame.K_UP]:
            self.hero_j.rect.y -= 5
            if self.hero_j.rect.bottom < 478:
                self.hero_j.rect.bottom = 478
        elif sws[pygame.K_DOWN]:
            self.hero_j.rect.y += 5
            if self.hero_j.rect.bottom > 682:
                self.hero_j.rect.bottom = 682
        elif sws[pygame.K_RIGHT]:
            self.hero_j.rect.x += 5
            if self.hero_j.rect.right > ZJM_CL.right:
                self.hero_j.rect.right = ZJM_CL.right
        elif sws[pygame.K_LEFT]:
            # self.hero_j.yi_j()
            self.hero_j.rect.x -= 5
            if self.hero_j.rect.left < ZJM_CL.left:
                self.hero_j.rect.x = 0
        elif sws[pygame.K_KP1]:
            self.hero_j.yi_j()
        elif sws[pygame.K_KP0]:
            self.hero_j.fei()
        elif sws[pygame.K_KP2]:
            self.hero_j.gj()

    def __pen_g(self):
        pygame.sprite.groupcollide(self.hero_j.fei_z, self.di_z, True, True)
        pygame.sprite.groupcollide(self.hero_j.fire_z, self.di_z, True, True)
        her = pygame.sprite.spritecollide(self.hero_j, self.di_z, True)
        # if len(her) > 0:
        #     Play.__game_over()

    def __dao(self):
        self.bei_z.update()
        self.bei_z.draw(self.zjm)

        self.di_z.update()
        self.di_z.draw(self.zjm)

        self.hero_z.update()
        self.hero_z.draw(self.zjm)

        self.hero_j.fire_z.update()
        self.hero_j.fire_z.draw(self.zjm)

        self.hero_j.fei_z.update()
        self.hero_j.fei_z.draw(self.zjm)

        self.hero_j.gj_z.update()
        self.hero_j.gj_z.draw(self.zjm)

    @staticmethod
    def __game_over():
        print("游戏结束...")
        pygame.quit()
        exit()


if __name__ == '__main__':
    run = Play()
    run.play_game()

# 精灵组  负责被主程序调用
# *-* coding:utf8 *-*
import random
import pygame
ZJM_CL = pygame.Rect(0, 0, 1280, 756)
DJ_CL = pygame.USEREVENT
# GJ_JSQ = pygame.USEREVENT + 1
# 击落敌方飞机得分
ENEMY_SCORE = 100


class Main(pygame.sprite.Sprite):
    def __init__(self, dz_t, spend=1):
        super().__init__()
        self.image = pygame.image.load(dz_t)
        self.rect = self.image.get_rect()
        self.sdu = spend
        # pygame.time.set_timer(GJ_JSQ, 1)

    def update(self):
        self.rect.x += self.sdu


class Bei(Main):
    def __init__(self):
# 图片地址
        super().__init__("E:/python/python_/mulumain/20.jpg")

    def update(self):
        self.sdu = 0


class Dji(Main):
    def __init__(self):
        super().__init__("E:/python/python_/mulumain/26.jpg")
        print("创建敌机...")
        wz_max = 682

        self.rect.bottom = random.randint(478, wz_max)
        self.rect.left = 1280
        # self.rect.x = 1280

    def update(self):

        self.rect.x -= 1
        if self.rect.right < 0:
            self.kill()

    def __del__(self):
        print("敌机销毁...")


class Hero(Main):
    def __init__(self):
        super().__init__("E:/python/python_/mulumain/22.jpg")
        wz_yx = 550
        self.rect.bottom = wz_yx
        self.rect.left = 200
        self.fire_z = pygame.sprite.Group()
        self.fei_z = pygame.sprite.Group()
        self.gj_z = pygame.sprite.Group()
        # if self.rect.bottom < 520:
        #     self.sdu = 0
        # elif self.rect.left < ZJM_CL.left:
        #     self.sdu = 0
        # if self.rect.bottom > 682:
        #     self.sdu = 0
        # elif self.rect.right > 1280:
        #     self.sdu = 0

    # def yi_j(self):
    #     fire = Fire()
    #     fire.rect.left = self.rect.right
    #     fire.rect.bottom = self.rect.bottom - 20
    #     self.fire_z.add(fire)

    def update(self):
        self.sdu = 0

    def yi_j(self):
        fire = Fire()
        fire.rect.left = self.rect.right
        fire.rect.bottom = self.rect.bottom - 20
        self.fire_z.add(fire)

    def fei(self):
        fei = Fei()
        fei.rect.left = self.rect.right
        fei.rect.bottom = self.rect.bottom - 20
        self.fei_z.add(fei)

    def gj(self):
        gj = Gj()
        gj.rect.left = self.rect.right
        gj.rect.bottom = self.rect.bottom - 45
        self.gj_z.add(gj)


class Fire(Main):
    def __init__(self):
        super().__init__("E:/python/python_/mulumain/27.png", 1)
        print("创建子弹...")

    def update(self):
        self.rect.x += 20
        if self.rect.left == ZJM_CL.right:
            self.kill()

    def __del__(self):
        print("一技能销毁...")


class Fei(Main):
    def __init__(self):
        super().__init__("E:/python/python_/mulumain/28.png")

    def update(self):
        self.rect.x += 30
        if self.rect.left == ZJM_CL.right:
            self.kill()

    def __del__(self):
        print("一技能销毁...")


class Gj(Main):
    def __init__(self):
        super().__init__("E:/python/python_/mulumain/29.png")

    def update(self):
        self.rect.x += 10
        if self.rect.left > ZJM_CL.left:
            self.kill()

    def __del__(self):
        print("攻击取消...")

#  图片可以自己上网找个  

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/840770.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号