python植物大战僵尸七之豌豆发射子弹

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

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

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

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

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:
        bul = Bullet(peashooter.rect, size)
        sunList.add(bul)

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

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

    index += 1

    pygame.display.update()

'''子弹'''
import pygame


class Bullet(pygame.sprite.Sprite):
    def __init__(self, rect, bg_size):
        super(Bullet, self).__init__()
        self.image = pygame.image.load("material/images/Bullet_1.png")
        self.rect = self.image.get_rect()
        # 定义子弹的初始化位置
        self.rect.left, self.rect.top = rect[0] + 45, rect[1]
        self.width, self.height = bg_size[0], bg_size[1]
        self.speed = 5

    def update(self, *args):
        if self.rect.right < self.width:
            self.rect.left += self.speed
        else:
            self.kill()





image.png

你可能感兴趣的:(python植物大战僵尸七之豌豆发射子弹)