一.目的
需要做出如下图所示效果的物品展示框,展示当前玩家所使用的工具和种子
二.代码的编写
首先创建一个名为overlay.py的文件,用于书写Overlay类,该类的作用是,将玩家可能手持的所有工具或者种子的图片导入进来,并且根据玩家实际手持的工具,将其展示在屏幕的左下角
import pygame
from settings import *
class Overlay:
def __init__(self,player):
#获得屏幕
self.display_surface = pygame.display.get_surface()
#由于需要知道玩家正在手持什么工具或者种子,所以需要调用player里面的变量,所以把player当成参数传进来
self.player = player
#用于存放所有的工具的图片的字典,其key值是工具名,value值是图片
self.tools_surf = {}
#用于存放所有种子的图片的字典,其key值是种子名,value值是图片
self.seeds_surf = {}
#存放种子和工具的文件夹路径
overlay_path = '../graphics/overlay/'
#利用for循环分别把每一个工具和种子,利用image.load函数导入进来,然后再加入到字典中
for tool in player.tools:
#把图片加载进来
img = pygame.image.load(overlay_path+'/'+tool+'.png').convert_alpha()
self.tools_surf[tool] = img
for seed in player.seeds:
#把图片加载进来
img = pygame.image.load(overlay_path+'/'+seed+'.png').convert_alpha()
self.seeds_surf[seed] = img
def display(self):
#获取玩家正在使用的工具的图像
tool_surf = self.tools_surf[self.player.selected_tool]
#设置图像要显示的位置,其中midbottom 是图片下边的中间,OVERLAY_POSITIONS[]是在settings.py内设置的常量
tool_rect = tool_surf.get_rect(midbottom = OVERLAY_POSITIONS['tool'])
#绘制工具图像
#.blit(要绘制的图像,要绘制的坐标)
self.display_surface.blit(tool_surf,tool_rect)
seed_surf = self.seeds_surf[self.player.selected_seed]
seed_rect = seed_surf.get_rect(midbottom=OVERLAY_POSITIONS['seed'])
self.display_surface.blit(seed_surf, seed_rect)
具体的细节都写在注释里了,其中 新接触到的东西只有 self.display_surface.blit()这个函数,这是将图片绘制到屏幕的指定位置的函数,它的两个参数第一个为图片,第二个为一个坐标,比如self.display_surface.blit(img,(0,0))就是把图片img绘制到坐标为(0,0)的地方
首先将Overlay这个类导进来,然后创建对象,因为需要传入player这个参数,所以创建对象需要在setup()函数之后写,最后是在update函数里调用overlay的display函数,因为每一帧都需要绘制物品栏,所以写在update函数里
三.完整代码
import pygame
from settings import *
class Overlay:
def __init__(self,player):
#获得屏幕
self.display_surface = pygame.display.get_surface()
#由于需要知道玩家正在手持什么工具或者种子,所以需要调用player里面的变量,所以把player当成参数传进来
self.player = player
#用于存放所有的工具的图片的字典,其key值是工具名,value值是图片
self.tools_surf = {}
#用于存放所有种子的图片的字典,其key值是种子名,value值是图片
self.seeds_surf = {}
#存放种子和工具的文件夹路径
overlay_path = '../graphics/overlay/'
#利用for循环分别把每一个工具和种子,利用image.load函数导入进来,然后再加入到字典中
for tool in player.tools:
#把图片加载进来
img = pygame.image.load(overlay_path+'/'+tool+'.png').convert_alpha()
self.tools_surf[tool] = img
for seed in player.seeds:
#把图片加载进来
img = pygame.image.load(overlay_path+'/'+seed+'.png').convert_alpha()
self.seeds_surf[seed] = img
def display(self):
#获取玩家正在使用的工具的图像
tool_surf = self.tools_surf[self.player.selected_tool]
#设置图像要显示的位置,其中midbottom 是图片下边的中间,OVERLAY_POSITIONS[]是在settings.py内设置的常量
tool_rect = tool_surf.get_rect(midbottom = OVERLAY_POSITIONS['tool'])
#绘制工具图像
#.blit(要绘制的图像,要绘制的坐标)
self.display_surface.blit(tool_surf,tool_rect)
seed_surf = self.seeds_surf[self.player.selected_seed]
seed_rect = seed_surf.get_rect(midbottom=OVERLAY_POSITIONS['seed'])
self.display_surface.blit(seed_surf, seed_rect)
import pygame
from settings import *
from player import Player
from overlay import Overlay
class Level():
def __init__(self):
#得到屏幕的画面,得到的这个画面与main.py中的screen相同
self.display_surface = pygame.display.get_surface()
#创建精灵组
self.all_sprites = pygame.sprite.Group()
#调用setup方法
self.setup()
#创建工具和种子显示图层
self.overlay = Overlay(self.player)
def setup(self):
self.player = Player((640,360),self.all_sprites)
def run(self,dt):
#窗口的背景设为黑色
self.display_surface.fill('black')
#调用精灵组的draw方法
self.all_sprites.draw(self.display_surface)
#调用精灵组的update方法
self.all_sprites.update(dt)
self.overlay.display()