一.目的
创建一个玩家的背包,里面记录玩家背包里的苹果数,木头数,小麦数,马铃薯数
二.代码实现
这个函数写在哪里,比较灵活,这里按照原作者的写法写到level.py里
接着,我们要到tree这个类里面,每当苹果掉落或者树木消失的时候,就调用一次这个函数,由于level与sprites这两个文件是不互通的,所以要在创建tree对象的时候把player_add这个函数当作参数传递进去
接下来只需要在苹果消失的时候调用sef.player_add("apple"),树木消失的时候调用self.player_add("wood")即可
三.完整代码
import pygame
from settings import *
from support import *
from timer import Timer
class Player(pygame.sprite.Sprite):
def __init__(self,pos,group,collision_sprites,tree_sprites):
#这个参数可以传一个精灵组,这样就会自动把该精灵加入到该精灵组中
#也可以为空,这样需要在外面手动调用精灵组的add函数来将这个精灵加入到精灵组中
super().__init__(group)
self.import_assets()
self.status = 'down_idle'
self.frame_index = 0
#这里的变量名一定要叫image,因为这是它父类Sprite规定的
self.image = self.animations[self.status][self.frame_index]
#这个get_rect()也是父类中设置的方法
#返回值是有很多,大概有x,y,centerx,centery,center,width,height这几类
#大概就是image的x坐标,y坐标,中心的x坐标,中心的y坐标,中心点的坐标,宽度,高度等
#参数可以不填,那么位置就默认是(0,0),也可以填一个列表,比如(100,100),那么初始的位置就是(100,100)
#也可以是center = 一个坐标,这表示设置该图像的中心在这个坐标上
#同样的这里的变量名也一定要叫rect,这是父类规定的
self.rect = self.image.get_rect(center = pos)
#z轴
self.z = LAYERS['main']
#创建一个二维的向量,参数不填默认是(0,0)
self.direction = pygame.math.Vector2()#速度的方向
self.pos = pygame.math.Vector2(self.rect.center)#位置
self.speed = 200#速度
#碰撞箱
self.hitbox = self.rect.copy().inflate((-126,-70))
self.collision_sprites = collision_sprites
self.timers = {
'tool use':Timer(350,self.use_tool),
'tool switch': Timer(200),
'seed use': Timer(350, self.use_seed),
'seed switch': Timer(200),
}
self.tools = ['hoe', 'axe', 'water']
self.tool_index = 0
self.selected_tool = self.tools[self.tool_index]
self.seeds = ['corn', 'tomato']
self.seed_index = 0
self.selected_seed = self.seeds[self.seed_index]
#玩家的背包
self.item_inventory = {
'wood': 0,
'apple': 0,
'corn': 0,
'tomato': 0
}
#接收树木精灵组
self.tree_sprites = tree_sprites
def use_tool(self):
if self.selected_tool == 'hoe':
pass
if self.selected_tool == 'axe':
for tree in self.tree_sprites.sprites():
if tree.rect.collidepoint(self.target_pos):
tree.damage()
if self.selected_tool == 'water':
pass
def get_target_pos(self):
# 获取工具的作用区域坐标
self.target_pos = self.rect.center + PLAYER_TOOL_OFFSET[self.status.split('_')[0]]
def use_seed(self):
pass
def import_assets(self):
self.animations = {'up': [], 'down': [], 'left': [], 'right': [],
'right_idle': [], 'left_idle': [], 'up_idle': [], 'down_idle': [],
'right_hoe': [], 'left_hoe': [], 'up_hoe': [], 'down_hoe': [],
'right_axe': [], 'left_axe': [], 'up_axe': [], 'down_axe': [],
'right_water': [], 'left_water': [], 'up_water': [], 'down_water': []}
for animation in self.animations.keys():
full_path = '../graphics/character/' + animation
self.animations[animation] = import_folder(full_path)
def animate(self, dt):
# 4 是比较合适的数字
# 数字 决定做动作的快慢
# 做一个动作需要 1/4秒
# fream_index += dt 的话,要经过1s,才能变成下一个整数,做下一个动作
# fream_index += 4*dt,那么增加的速度就快了四倍,经过1/4秒就能做下一个动作了
self.frame_index += 4 * dt
# print(self.frame_index)
if self.frame_index >= len(self.animations[self.status]):
self.frame_index = 0
self.image = self.animations[self.status][int(self.frame_index)]
def input(self):
keys = pygame.key.get_pressed()
#已经在使用工具的时候,停止对按键的检测
if not self.timers['tool use'].active:
if keys[pygame.K_UP]:
self.direction.y = -1
self.status = 'up'
elif keys[pygame.K_DOWN]:
self.direction.y = 1
self.status = 'down'
else:
self.direction.y = 0#不要忘记加上这一句,不然按下键盘后再松开也不会停
if keys[pygame.K_RIGHT]:
self.direction.x = 1
self.status = 'right'
elif keys[pygame.K_LEFT]:
self.direction.x = -1
self.status = 'left'
else:
self.direction.x = 0
if keys[pygame.K_SPACE]:
#启动计时器
self.timers['tool use'].activate()
#实用工具的时候是不能移动的
self.direction = pygame.math.Vector2()
#要从第一张图片开始绘制
self.frame_index = 0
#按下左边的shift键更换工具
if keys[pygame.K_LSHIFT] and not self.timers['tool switch'].active:
self.timers['tool switch'].activate()
self.tool_index += 1
self.tool_index = self.tool_index if self.tool_index < len(self.tools) else 0
self.selected_tool = self.tools[self.tool_index]
#按下左边的ctrl键,使用种子
if keys[pygame.K_RCTRL]:
self.timers['seed use'].activate()
self.direction = pygame.math.Vector2()
self.frame_index = 0
#按下e键,切换种子
if keys[pygame.K_RSHIFT] and not self.timers['seed switch'].active:
self.timers['seed switch'].activate()
self.seed_index += 1
self.seed_index = self.seed_index if self.seed_index < len(self.seeds) else 0
self.selected_seed = self.seeds[self.seed_index]
def get_status(self):
# idle
if self.direction.magnitude() == 0:
# self.status += '_idle'
# 上面这种方法不可取因为每一帧都会在字符串后面加上一个_idel
# 所以status会变成 xxx_idle_idle_idle
# 实际上当出现两个_idle的时候就已经报错了
# 下面这种方法
# split('_')是把一个字符串 以 '_' 为分节符分开
# 他返回的是一个列表
# 比如 a_b_c_d
# 返回的就是[a,b,c,d]
# 所以下面的【0】获取的就是_之前的内容
self.status = self.status.split('_')[0] + '_idle'
if self.timers['tool use'].active:
self.status = self.status.split('_')[0] + '_' + self.selected_tool
def update_timers(self):
for timer in self.timers.values():
timer.update()
def collision(self,direction):
# 遍历碰撞箱精灵组的所有精灵
for sprite in self.collision_sprites.sprites():
# 如果该精灵有一个叫hitbox的属性
#实际上collision_sprites精灵组内的所有精灵都应该有hitbox这个属性,这句话属实多余
if hasattr(sprite,'hitbox'):
#如果该精灵的hitbox与玩家的hitbox有重叠
if sprite.hitbox.colliderect(self.hitbox):
#如果此时正在水平方向移动
if direction == 'horizontal':
if self.direction.x > 0: #玩家正在向右移动
self.hitbox.right = sprite.hitbox.left
if self.direction.x < 0: # 玩家正在向左移动
self.hitbox.left = sprite.hitbox.right
self.rect.centerx = self.hitbox.centerx
self.pos.x = self.hitbox.centerx
#如果此时正在竖直方向移动
if direction == 'vertical':
if self.direction.y > 0: # 玩家正在向下移动
self.hitbox.bottom = sprite.hitbox.top
if self.direction.y < 0: # 玩家正在向上移动
self.hitbox.top = sprite.hitbox.bottom
self.rect.centery = self.hitbox.centery
self.pos.y = self.hitbox.centery
def move(self,dt):
#向量归一化,比如一个n维向量为(x1,x2,x3...,xn)
#那么向量归一化的操作就是等比例缩放x1到xn的大小让 根号下(x1的平方+x2的平方+...+xn的平方) 等于1
#归一化的目的是如果同时按右键和上,那么direction就会变成(1,1),他的速度向量就是一个大小为根2,方向右上的向量了
#magnitude()返回的是一个float类型的数据,他的大小为根号下(x1的平方+x2的平方+...+xn的平方)
if self.direction.magnitude() > 0:#这表示如果有按键按下,如果向量里面全是0,会使归一化中的数学计算出现错误
self.direction = self.direction.normalize()
#位移 = 方向 * 速度 * 变化的时间()
#水平方向
self.pos.x += self.direction.x * self.speed * dt
self.hitbox.centerx = round(self.pos.x)
self.rect.centerx = self.hitbox.centerx
self.collision('horizontal')
#竖直方向
self.pos.y += self.direction.y * self.speed * dt
self.hitbox.centery = round(self.pos.y)
self.rect.centery = self.hitbox.centery
self.collision('vertical')
def update(self,dt):
self.input()
self.get_status()
self.update_timers()
self.get_target_pos()
self.move(dt)
self.animate(dt)
import pygame
from settings import *
from player import Player
from overlay import Overlay
from sprites import *
from pytmx.util_pygame import load_pygame
from support import *
class Level():
def __init__(self):
#得到屏幕的画面,得到的这个画面与main.py中的screen相同
self.display_surface = pygame.display.get_surface()
#创建精灵组
self.all_sprites = CameraGroup()
#具有碰撞箱的精灵组
self.collision_sprites = pygame.sprite.Group()
#树木精灵组
self.tree_sprites = pygame.sprite.Group()
#调用setup方法
self.setup()
#创建工具和种子显示图层
self.overlay = Overlay(self.player)
def setup(self):
#载入.tmx文件
tmx_data = load_pygame('../data/map.tmx')
#绘制房子与栅栏,他们都属于Generic类
for layer in ['HouseFloor', 'HouseFurnitureBottom']:
for x, y, surf in tmx_data.get_layer_by_name(layer).tiles():
Generic((x * TILE_SIZE, y * TILE_SIZE), surf, self.all_sprites, LAYERS['house bottom'])
for layer in ['HouseWalls', 'HouseFurnitureTop','Fence']:
for x, y, surf in tmx_data.get_layer_by_name(layer).tiles():
Generic((x * TILE_SIZE, y * TILE_SIZE), surf, [self.all_sprites, self.collision_sprites])
#水流
water_frames = import_folder('../graphics/water')
for x, y, surf in tmx_data.get_layer_by_name('Water').tiles():
Water((x * TILE_SIZE, y * TILE_SIZE), water_frames, self.all_sprites)
#树木
for obj in tmx_data.get_layer_by_name('Trees'):
Tree(
pos=(obj.x, obj.y),
surf=obj.image,
groups=[self.all_sprites, self.collision_sprites, self.tree_sprites],
name=obj.name,
player_add=self.player_add)
#野花
for obj in tmx_data.get_layer_by_name('Decoration'):
WildFlower((obj.x, obj.y), obj.image,[self.all_sprites, self.collision_sprites])
#空气墙
for x, y, surf in tmx_data.get_layer_by_name('Collision').tiles():
Generic((x * TILE_SIZE, y * TILE_SIZE), pygame.Surface((TILE_SIZE, TILE_SIZE)), self.collision_sprites)
#玩家
for obj in tmx_data.get_layer_by_name('Player'):
if obj.name == 'Start':
self.player = Player(
pos=(obj.x, obj.y),
group=self.all_sprites,
collision_sprites=self.collision_sprites,
tree_sprites=self.tree_sprites)
Generic(
pos = (0,0),
surf = pygame.image.load('../graphics/world/ground.png').convert_alpha(),
groups = self.all_sprites,
z = LAYERS['ground']
)
def run(self,dt):
#窗口的背景设为黑色
self.display_surface.fill('black')
#调用精灵组的draw方法
self.all_sprites.custom_draw(self.player)
#调用精灵组的update方法
self.all_sprites.update(dt)
self.overlay.display()
def player_add(self, item):
#item是一个str类型的数据,代表要对哪一种物品加一
self.player.item_inventory[item] += 1
class CameraGroup(pygame.sprite.Group):
def __init__(self):
super().__init__()
#获取窗口
self.display_surface = pygame.display.get_surface()
#这是一个偏移量,代表的是玩家的实际位置与屏幕中间的矢量
self.offset = pygame.math.Vector2()
def custom_draw(self,player):
self.offset.x = player.rect.centerx - SCREEN_WIDTH / 2
self.offset.y = player.rect.centery - SCREEN_HEIGHT / 2
for layer in LAYERS.values():#按照z轴从小到达绘制
for sprite in sorted(self.sprites(),key = lambda sprite: sprite.rect.centery):
if sprite.z == layer:#如果该精灵的z值等于当前要绘制的z值,才绘制
offset_rect = sprite.rect.copy()
offset_rect.center -= self.offset
#if sprite == player:
#print("player.rect.center为:(" +
# str(player.rect.centerx)+"," + str(player.rect.centery)+")")
#print("offset_rect为:(" + str(offset_rect.x)
# +"," +str(offset_rect.y)+")")
self.display_surface.blit(sprite.image,offset_rect)
import pygame
from settings import *
from random import *
from timer import Timer
class Generic(pygame.sprite.Sprite):
def __init__(self,pos,surf,groups,z = LAYERS['main']):
super().__init__(groups)
self.image = surf
self.rect = self.image.get_rect(topleft = pos)
self.z = z
self.hitbox = self.rect.copy().inflate(-self.rect.width * 0.2, -self.rect.height * 0.75)
class Water(Generic):
def __init__(self, pos, frames, groups):
#传入的参数,fream是一个列表,存放了水流不同动作的五张图片
self.frames = frames
#fream_index是正在播放第几张图片
self.frame_index = 0
super().__init__(
pos = pos,
surf = self.frames[self.frame_index],
groups = groups,
z = LAYERS['water'])
def animate(self,dt):
self.frame_index += 5 * dt
if self.frame_index >= len(self.frames):
self.frame_index = 0
self.image = self.frames[int(self.frame_index)]
def update(self,dt):
self.animate(dt)
class WildFlower(Generic):
def __init__(self, pos, surf, groups):
super().__init__(pos, surf, groups)
self.hitbox = self.rect.copy().inflate(-20,-self.rect.height)
class Particle(Generic):
def __init__(self, pos, surf, groups, z, duration = 200):
#其中duration是持续时间,默认为200ms
super().__init__(pos, surf, groups, z)
self.start_time = pygame.time.get_ticks()
self.duration = duration
#将图片透明的地方设为黑色(1),不透明的地方设为白色,来获取掩码
mask_surf = pygame.mask.from_surface(self.image)
#根据掩码生成图片,该图片只有黑白两种颜色
new_surf = mask_surf.to_surface()
#set_colorkey是在绘制图片的时候只绘制指定的颜色
new_surf.set_colorkey((0,0,0))
self.image = new_surf
def update(self,dt):
#经过一定的时间之后,这个精灵会被删除
current_time = pygame.time.get_ticks()
if current_time - self.start_time > self.duration:
self.kill()
class Tree(Generic):
def __init__(self, pos, surf, groups, name,player_add):
super().__init__(pos, surf, groups)
self.player_add = player_add
#创建苹果用到的变量
#导入苹果的图像素材
self.apple_surf = pygame.image.load('../graphics/fruit/apple.png')
#苹果的坐标,是一个常量
self.apple_pos = APPLE_POS[name]
#创建苹果精灵组,这个精灵组会在砍树的时候用到
self.apple_sprites = pygame.sprite.Group()
self.groups = groups
self.create_fruit()
#为砍树用到的变量
self.health = 5#树的生命值
self.alive = True#树是否存活
stump_path = f'../graphics/stumps/{"small" if name == "Small" else "large"}.png'#树状的图片路径
self.stump_surf = pygame.image.load(stump_path).convert_alpha()#把树状图片导入进来
self.invul_timer = Timer(200)#创建一个计时器
def create_fruit(self):
for pos in self.apple_pos:
if randint(0,10) < 2:
#x坐标 = 偏移坐标 + 树的左边缘坐标
x = pos[0] + self.rect.left
#y坐标 = 偏移坐标 + 树的顶部边缘坐标
y = pos[1] + self.rect.top
Generic(
pos = (x,y),
surf = self.apple_surf,
#把它加入苹果精灵组和all_sprites精灵组
#all_sprites是传入的参数的self.group的第一个元素,所以是【0】
groups = [self.apple_sprites,self.groups[0]],
z = LAYERS['fruit'])
def damage(self):
#生命值减一
self.health -= 1
#看看生命值是否小于0了,如果小于0就把该改的东西都改一下就行可
if self.health <= 0:
if self.alive:
Particle(self.rect.topleft, self.image, self.groups[0], LAYERS['fruit'], 300)
self.image = self.stump_surf
self.rect = self.image.get_rect(midbottom = self.rect.midbottom)
self.hitbox = self.rect.copy().inflate(-10,-self.rect.height * 0.6)
self.player_add('wood')
self.alive = False
#如果树上还有苹果,随机选一个去除
if len(self.apple_sprites.sprites()) > 0:
random_apple = choice(self.apple_sprites.sprites())
Particle(
pos=random_apple.rect.topleft,
surf=random_apple.image,
groups=self.groups[0],
z=LAYERS['fruit'],
)
self.player_add('apple')
random_apple.kill()#Sprite.kill()就是将该精灵删除