你的位置:首页 > 信息动态 > 新闻中心
信息动态
联系我们

植物大战僵尸(初始化音乐,画面)

2021-11-21 7:49:33
#植物大战僵尸(初始化音乐,画面)

import pygame,sys
from pygame import surface
from pygame.locals import *

pygame.init()
mainClock=pygame.time.Clock()
 
#参数前置
WINDOWWIDTH=1024
WINDOWHEIGHT=600
FPS=20

#Set up the colors颜色设置,可用于背景颜色
BLACK=(0,0,0)
GREEN=(0,255,0)
WHITE=(255,255,255)
RED=(255,0,0)


TEXTCOLOR=WHITE

#函数
def drawText(text,font,surface,x,y):
    textObj=font.render(text,1,TEXTCOLOR)
    textrect=textObj.get_rect()
    textrect.topleft=(x,y)
    surface.blit(textObj,textrect)


#初始化pygame所有模块

windowSurface=pygame.display.set_mode((WINDOWWIDTH,WINDOWHEIGHT))
pygame.display.set_caption("植物大战僵尸")
pygame.mouse.set_visible(False)

zombieImage=pygame.image.load("zombie.png")
zombieStretchedImage=pygame.transform.scale(zombieImage,(80,80))
destination=pygame.Rect(944,300,80,80)

playerImage=pygame.image.load('SnowPea.gif')

backgroundImage=pygame.image.load('background.png')
rescaledBackground=pygame.transform.scale(backgroundImage,((WINDOWWIDTH,WINDOWHEIGHT)))

pygame.mixer.music.load('grasswalk.mp3')
pygame.mixer.music.play(-1,0,0)

font=pygame.font.SysFont(None,48)


while True:
    for event in pygame.event.get():
        if event.type==QUIT:
            pygame.quit()
            sys.exit()
    
    #在游戏窗口上绘制对象
    windowSurface.blit(rescaledBackground,(0,0))
    windowSurface.blit(zombieStretchedImage,destination)
    windowSurface.blit(playerImage,(0,WINDOWHEIGHT/2))
    
    #添加字体
    drawText('Zombie VS Plants',font,windowSurface,(WINDOWWIDTH/3),(WINDOWHEIGHT/4))
    drawText('Press Enter to start',font,windowSurface,(WINDOWWIDTH/3)-10,(WINDOWHEIGHT/4)+50)
    #将绘制出来的东西显示在显示屏上
    pygame.display.update()
    mainClock.tick(FPS)