美文网首页
python植物大战僵尸八之太阳花收集功能

python植物大战僵尸八之太阳花收集功能

作者: __豆约翰__ | 来源:发表于2019-01-14 16:07 被阅读30次
    import pygame
    from pygame.locals import *
    import sys
    
    from Bullet import Bullet
    from Peashooter import Peashooter
    from Sun import Sun
    from SunFlower import SunFlower
    from WallNut import WallNut
    
    # 初始化pygame
    from Zombie import Zombie
    
    pygame.init()
    
    size = (1200, 600)
    
    # 设置屏幕宽高
    screen = pygame.display.set_mode(size)
    # 设置屏幕标题
    pygame.display.set_caption("植物大战僵尸")
    
    backgroundImg = pygame.image.load('material/images/background1.jpg').convert_alpha()
    sunbackImg = pygame.image.load('material/images/SunBack.png').convert_alpha()
    
    text = '1000'
    myfont = pygame.font.SysFont('arial', 30)
    txtImg = myfont.render(text, True, (0, 0, 0))
    
    peashooter = Peashooter()
    sunFlower = SunFlower()
    wallNut = WallNut()
    zombie = Zombie()
    
    spriteList = pygame.sprite.Group()
    spriteList.add(peashooter)
    spriteList.add(sunFlower)
    spriteList.add(wallNut)
    spriteList.add(zombie)
    
    sunList = pygame.sprite.Group()
    
    #定义生成太阳的事件
    GENERATORSUNEVNET = pygame.USEREVENT + 1
    pygame.time.set_timer(GENERATORSUNEVNET, 2000)
    
    index = 0
    clock = pygame.time.Clock()
    while True:
    
        if index > 100:
            index = 0
    
        clock.tick(20)
        # 启动消息队列,获取消息并处理
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            if event.type == GENERATORSUNEVNET:
                    sun = Sun(sunFlower.rect)
                    sunList.add(sun)
                    # print(suns.__len__())
            if event.type == pygame.MOUSEBUTTONDOWN:
                    pressed_array = pygame.mouse.get_pressed()
                    print(pressed_array)
                    if pressed_array[0]:
                        pos = pygame.mouse.get_pos()
                        print(pos)
                        for sun in sunList:
                            if sun.rect.collidepoint(pos):
                                sunList.remove(sun)
                                text = str(int(text) + 50)
                                myfont = pygame.font.SysFont('arial', 30)
                                txtImg = myfont.render(text, True, (0, 0, 0))
    
        screen.blit(backgroundImg, (0, 0))
        screen.blit(sunbackImg, (250, 30))
        screen.blit(txtImg, (300, 33))
    
        if index % 30 == 0:
            bul = Bullet(peashooter.rect, size)
            spriteList.add(bul)
    
        spriteList.update(index)
        spriteList.draw(screen)
        sunList.update(index)
        sunList.draw(screen)
    
        index += 1
    
        pygame.display.update()
    
    
    
    image.png

    太阳花收集动画

    import pygame
    import random
    
    
    class Sun(pygame.sprite.Sprite):
        def __init__(self, rect):
            super(Sun, self).__init__()
            self.image = pygame.image.load('material/images/Sun_1.png').convert_alpha()
            self.images = [pygame.image.load('material/images/Sun_{}.png'.format(i)).convert_alpha() for i in
                           range(1, 18)]
            self.rect = self.images[0].get_rect()
            offsetTop = random.randint(-50, 50)
            offsetLeft = random.randint(-50, 50)
    
            self.rect.top = rect.top + offsetTop
            self.rect.left = rect.left + offsetLeft
    
            self.is_click = False
            self.times = 4
    
        # 更新精灵的位置
        def update(self, *args):
            self.image = self.images[args[0] % len(self.images)]
            if self.is_click:
                self.rect.left -= (self.rect.left - 250) / self.times
                self.rect.top -= (self.rect.top - 30) / self.times
    
                print(self.rect.left)
                if self.rect.left <= 250 and self.rect.top <= 30:
                    self.kill()
    
    
    import pygame
    from pygame.locals import *
    import sys
    
    from Bullet import Bullet
    from Peashooter import Peashooter
    from Sun import Sun
    from SunFlower import SunFlower
    from WallNut import WallNut
    
    # 初始化pygame
    from Zombie import Zombie
    
    pygame.init()
    
    size = (1200, 600)
    
    # 设置屏幕宽高
    screen = pygame.display.set_mode(size)
    # 设置屏幕标题
    pygame.display.set_caption("植物大战僵尸")
    
    backgroundImg = pygame.image.load('material/images/background1.jpg').convert_alpha()
    sunbackImg = pygame.image.load('material/images/SunBack.png').convert_alpha()
    
    score = '1000'
    myfont = pygame.font.SysFont('arial', 30)
    txtImg = myfont.render(score, True, (0, 0, 0))
    
    peashooter = Peashooter()
    sunFlower = SunFlower()
    wallNut = WallNut()
    zombie = Zombie()
    
    spriteList = pygame.sprite.Group()
    
    spriteList.add(peashooter)
    spriteList.add(sunFlower)
    spriteList.add(wallNut)
    spriteList.add(zombie)
    
    sunList = pygame.sprite.Group()
    
    index = 0
    clock = pygame.time.Clock()
    
    GENERATOR_SUN_EVENT = pygame.USEREVENT + 1
    pygame.time.set_timer(GENERATOR_SUN_EVENT, 2000)
    
    while True:
    
        clock.tick(15)
        # 启动消息队列,获取消息并处理
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            if event.type == GENERATOR_SUN_EVENT:
                sun = Sun(sunFlower.rect)
                sunList.add(sun)
            if event.type == MOUSEBUTTONDOWN:
                mouse_pressed = pygame.mouse.get_pressed()
                # 判断是否按下的事鼠标左键
                if mouse_pressed[0]:
                    pos = pygame.mouse.get_pos()
    
                    for sun in sunList:
                        if sun.rect.collidepoint(pos):
                            # sunList.remove(sun)
                            sun.is_click = True
                            score = int(score) + 50
                            myfont = pygame.font.SysFont('arial', 30)
                            txtImg = myfont.render(str(score), True, (0, 0, 0))
    
        screen.blit(backgroundImg, (0, 0))
        screen.blit(sunbackImg, (250, 30))
        screen.blit(txtImg, (300, 33))
    
        if index % 10 == 0:
            bullet = Bullet(peashooter.rect, size)
            spriteList.add(bullet)
    
        spriteList.update(index)
        spriteList.draw(screen)
        sunList.update(index)
        sunList.draw(screen)
    
        index += 1
    
        pygame.display.update()
    
    

    太阳花奔向目标的同时,变小

    import pygame
    import random
    
    
    class Sun(pygame.sprite.Sprite):
        def __init__(self, rect):
            super(Sun, self).__init__()
            self.image = pygame.image.load('material/images/Sun_1.png').convert_alpha()
            self.images = [pygame.image.load('material/images/Sun_{}.png'.format(i)).convert_alpha() for i in
                           range(1, 18)]
            self.rect = self.images[0].get_rect()
            offsetTop = random.randint(-50, 50)
            offsetLeft = random.randint(-50, 50)
    
            self.rect.top = rect.top + offsetTop
            self.rect.left = rect.left + offsetLeft
    
            self.is_click = False
            self.times = 5
            self.scale = 10
    
        # 更新精灵的位置
        def update(self, *args):
            self.image = self.images[args[0] % len(self.images)]
            if self.is_click:
                self.rect.left -= (self.rect.left - 250) / self.times
                self.rect.top -= (self.rect.top - 30) / self.times
    
                self.image = pygame.transform.smoothscale(self.image, (self.rect.width // self.times * self.scale, self.rect.height // self.times * self.scale))
                if self.scale > 1:
                    self.scale -= 1
    
                print(self.rect.left)
                if self.rect.left <= 250 and self.rect.top <= 30:
                    self.kill()
    
    

    僵尸头顶显示文字

    import pygame
    from pygame.locals import *
    import sys
    
    from Bullet import Bullet
    from Peashooter import Peashooter
    from Sun import Sun
    from SunFlower import SunFlower
    from WallNut import WallNut
    
    # 初始化pygame
    from Zombie import Zombie
    
    pygame.init()
    
    for font in pygame.font.get_fonts():
        print(font)
    
    size = (1200, 600)
    
    # 设置屏幕宽高
    screen = pygame.display.set_mode(size)
    # 设置屏幕标题
    pygame.display.set_caption("植物大战僵尸")
    
    backgroundImg = pygame.image.load('material/images/background1.jpg').convert_alpha()
    sunbackImg = pygame.image.load('material/images/SunBack.png').convert_alpha()
    
    score = '1000'
    myfont = pygame.font.SysFont('arial', 30)
    txtImg = myfont.render(score, True, (0, 0, 0))
    
    peashooter = Peashooter()
    sunFlower = SunFlower()
    wallNut = WallNut()
    # zombie = Zombie()
    
    spriteList = pygame.sprite.Group()
    
    spriteList.add(peashooter)
    spriteList.add(sunFlower)
    spriteList.add(wallNut)
    # spriteList.add(zombie)
    
    sunList = pygame.sprite.Group()
    zombieList = pygame.sprite.Group()
    
    index = 0
    clock = pygame.time.Clock()
    
    GENERATOR_SUN_EVENT = pygame.USEREVENT + 1
    pygame.time.set_timer(GENERATOR_SUN_EVENT, 2000)
    
    GENERATOR_ZOMBIE_EVENT = pygame.USEREVENT + 2
    pygame.time.set_timer(GENERATOR_ZOMBIE_EVENT, 5000)
    
    while True:
    
        clock.tick(15)
        # 启动消息队列,获取消息并处理
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            if event.type == GENERATOR_SUN_EVENT:
                sun = Sun(sunFlower.rect)
                sunList.add(sun)
    
            if event.type == GENERATOR_ZOMBIE_EVENT:
                zombie = Zombie()
                zombieList.add(zombie)
    
            if event.type == MOUSEBUTTONDOWN:
                mouse_pressed = pygame.mouse.get_pressed()
                # 判断是否按下的事鼠标左键
                if mouse_pressed[0]:
                    pos = pygame.mouse.get_pos()
    
                    for sun in sunList:
                        if sun.rect.collidepoint(pos):
                            # sunList.remove(sun)
                            sun.is_click = True
                            score = int(score) + 50
                            myfont = pygame.font.SysFont('arial', 30)
                            txtImg = myfont.render(str(score), True, (0, 0, 0))
    
        screen.blit(backgroundImg, (0, 0))
        screen.blit(sunbackImg, (250, 30))
        screen.blit(txtImg, (300, 33))
    
        if index % 10 == 0:
            bullet = Bullet(peashooter.rect, size)
            spriteList.add(bullet)
    
        spriteList.update(index)
        spriteList.draw(screen)
        sunList.update(index)
        sunList.draw(screen)
        zombieList.update(index)
        zombieList.draw(screen)
    
        for zombie in zombieList:
            headStr = '刘无敌'
            yourfont = pygame.font.SysFont('simsunnsimsun', 30)
            headpic = yourfont.render(headStr, True, (0, 0, 0))
            screen.blit(headpic, (zombie.rect.left + 60, zombie.rect.top - 20))
    
        index += 1
    
        pygame.display.update()
    
    

    相关文章

      网友评论

          本文标题:python植物大战僵尸八之太阳花收集功能

          本文链接:https://www.haomeiwen.com/subject/kxhjdqtx.html