python植物大战僵尸五之添加太阳

import pygame
from pygame.locals import *
import sys
from Peashooter import Peashooter
from Sun import Sun
from SunFlower import SunFlower
from WallNut import WallNut

# 初始化pygame


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()

myfont = pygame.font.SysFont('arial', 30)
txtImg = myfont.render("1000", True, (0, 0, 0))

peashooter = Peashooter()
sunFlower = SunFlower()
wallNut = WallNut()

sunList = []
index = 0
clock = pygame.time.Clock()
while True:
    if index > 100:
        index = 0

    clock.tick(15)
    # 启动消息队列,获取消息并处理
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    screen.blit(backgroundImg, (0, 0))
    screen.blit(sunbackImg, (250, 30))
    screen.blit(txtImg, (300, 33))

    screen.blit(peashooter.images[index % 13], peashooter.rect)
    screen.blit(sunFlower.images[index % 13], sunFlower.rect)
    screen.blit(wallNut.images[index % 13], wallNut.rect)

    if index % 30 == 0:
        sun = Sun(sunFlower.rect)
        sunList.append(sun)

    for sun in sunList:
        screen.blit(sun.images[index % 17], sun.rect)

    index += 1

    pygame.display.update()

import pygame
import random

class Sun:
    def __init__(self, rect):
        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

468490-2815ba8dbbb3e75c.png
image.png

重构太阳对象继承pygame.sprite.Sprite

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

    # 更新精灵的位置
    def update(self, *args):
        self.image = self.images[args[0] % len(self.images)]

新豌豆射手

import pygame


class Peashooter(pygame.sprite.Sprite):
    def __init__(self):
        super(Peashooter,self).__init__()
        self.image = pygame.image.load('material/images/Peashooter_00.png').convert_alpha()
        self.images = [pygame.image.load('material/images/Peashooter_{:02d}.png'.format(i)).convert_alpha() for i in
                       range(0, 13)]
        self.rect = self.images[0].get_rect()
        self.rect.top = 80
        self.rect.left = 250

    def update(self, *args):
        self.image = self.images[args[0] % len(self.images)]


新太阳花

import pygame


class SunFlower(pygame.sprite.Sprite):
    def __init__(self):
        super(SunFlower, self).__init__()
        self.image = pygame.image.load('material/images/SunFlower_00.png').convert_alpha()
        self.images = [pygame.image.load('material/images/SunFlower_{:02d}.png'.format(i)).convert_alpha() for i in
                       range(0, 13)]
        self.rect = self.images[0].get_rect()
        self.rect.top = 180
        self.rect.left = 250

    def update(self, *args):
        self.image = self.images[args[0] % len(self.images)]

新坚果

import pygame


class WallNut(pygame.sprite.Sprite):
    def __init__(self):
        super(WallNut, self).__init__()
        self.image = pygame.image.load('material/images/WallNut_00.png').convert_alpha()
        self.images = [pygame.image.load('material/images/WallNut_{:02d}.png'.format(i)).convert_alpha() for i in
                       range(0, 13)]
        self.rect = self.images[0].get_rect()
        self.rect.top = 280
        self.rect.left = 250

    def update(self, *args):
        self.image = self.images[args[0] % len(self.images)]

新主方法

import pygame
from pygame.locals import *
import sys
from Peashooter import Peashooter
from Sun import Sun
from SunFlower import SunFlower
from WallNut import WallNut

# 初始化pygame


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()

myfont = pygame.font.SysFont('arial', 30)
txtImg = myfont.render("1000", True, (0, 0, 0))

peashooter = Peashooter()
sunFlower = SunFlower()
wallNut = WallNut()

sunList = pygame.sprite.Group()
sunList.add(peashooter)
sunList.add(sunFlower)
sunList.add(wallNut)

index = 0
clock = pygame.time.Clock()
while True:
    if index > 100:
        index = 0

    clock.tick(15)
    # 启动消息队列,获取消息并处理
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    screen.blit(backgroundImg, (0, 0))
    screen.blit(sunbackImg, (250, 30))
    screen.blit(txtImg, (300, 33))


    if index % 30 == 0:
        sun = Sun(sunFlower.rect)
        sunList.add(sun)

    sunList.update(index)
    sunList.draw(screen)

    # screen.blit(peashooter.images[index % 13], peashooter.rect)
    # screen.blit(sunFlower.images[index % 13], sunFlower.rect)
    # screen.blit(wallNut.images[index % 13], wallNut.rect)


    index += 1

    pygame.display.update()

猜你喜欢

转载自blog.csdn.net/weixin_33695082/article/details/87582274