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

简介: 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


image.png


目录
相关文章
|
定位技术 Python
【python】pygame实现植物大战僵尸小游戏(附源码 有注释)
【python】pygame实现植物大战僵尸小游戏(附源码 有注释)
3668 1
|
JSON 开发工具 git
基于Python和pygame的植物大战僵尸游戏设计源码
本项目是基于Python和pygame开发的植物大战僵尸游戏,包含125个文件,如PNG图像、Python源码等,提供丰富的游戏开发学习素材。游戏设计源码可从提供的链接下载。关键词:Python游戏开发、pygame、植物大战僵尸、源码分享。
945 6
【python绘图库turtle实战】使用python绘图库turtle绘制:太阳花、彩虹线与小黄人【含完整源码】
【python绘图库turtle实战】使用python绘图库turtle绘制:太阳花、彩虹线与小黄人【含完整源码】
|
存储 JSON 搜索推荐
Python植物大战僵尸源码分享
Python植物大战僵尸源码分享
1210 0
|
Python
python&pygame植物大战僵尸后续课程及项目资源
python&pygame植物大战僵尸后续课程及项目资源
549 0
|
开发框架 Python
手把手教你使用python开发植物大战僵尸游戏
手把手教你使用python开发植物大战僵尸游戏
980 0
|
数据采集 机器学习/深度学习 人工智能
Python:现代编程的首选语言
Python:现代编程的首选语言
1982 102
|
数据采集 机器学习/深度学习 算法框架/工具
Python:现代编程的瑞士军刀
Python:现代编程的瑞士军刀
698 104
|
人工智能 自然语言处理 算法框架/工具
Python:现代编程的首选语言
Python:现代编程的首选语言
558 103
|
机器学习/深度学习 人工智能 数据挖掘
Python:现代编程的首选语言
Python:现代编程的首选语言
610 82

推荐镜像

更多