代码拉取完成,页面将自动刷新
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, interaction, soil_layer, toggle_shop):
super().__init__(group)
self.import_assets()
self.status = 'down_idle'
self.frame_index = 0
self.auto_watering = False
# general setup
self.image = self.animations[self.status][self.frame_index]
self.rect = self.image.get_rect(center = pos)
self.z = LAYERS['main']
# movement attributes
self.direction = pygame.math.Vector2()
self.pos = pygame.math.Vector2(self.rect.center)
self.speed = 200
# collision
self.hitbox = self.rect.copy().inflate((-126,-70))
self.collision_sprites = collision_sprites
# timers
self.timers = {
'tool use': Timer(350,self.use_tool),
'tool switch': Timer(200),
'seed use': Timer(350,self.use_seed),
'seed switch': Timer(200),
}
# tools
self.tools = ['hoe','axe','water']
self.tool_index = 0
self.selected_tool = self.tools[self.tool_index]
# seeds
self.seeds = ['corn', 'tomato']
self.seed_index = 0
self.selected_seed = self.seeds[self.seed_index]
# inventory
self.item_inventory = {
'wood': 20,
'apple': 20,
'corn': 20,
'tomato': 20
}
self.seed_inventory = {
'corn': 5,
'tomato': 5
}
self.money = 200
# interaction
self.tree_sprites = tree_sprites
self.interaction = interaction
self.sleep = False
self.soil_layer = soil_layer
self.toggle_shop = toggle_shop
# sound
self.watering = pygame.mixer.Sound('../audio/water.mp3')
self.watering.set_volume(0.2)
def use_tool(self):
if self.selected_tool == 'hoe':
self.soil_layer.get_hit(self.target_pos)
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':
self.soil_layer.water(self.target_pos)
self.watering.play()
def get_target_pos(self):
self.target_pos = self.rect.center + PLAYER_TOOL_OFFSET[self.status.split('_')[0]]
def use_seed(self):
if self.seed_inventory[self.selected_seed] > 0:
self.soil_layer.plant_seed(self.target_pos, self.selected_seed)
self.seed_inventory[self.selected_seed] -= 1
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):
self.frame_index += 4 * dt
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 and not self.sleep:
# directions
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_a]: # 按下 'a' 键
self.auto_watering = not self.auto_watering # 切换自动浇水状态
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
# tool use
if keys[pygame.K_SPACE]:
self.timers['tool use'].activate()
self.direction = pygame.math.Vector2()
self.frame_index = 0
# change tool
if keys[pygame.K_q] 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]
# seed use
if keys[pygame.K_LCTRL]:
self.timers['seed use'].activate()
self.direction = pygame.math.Vector2()
self.frame_index = 0
# change seed
if keys[pygame.K_e] 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]
if keys[pygame.K_RETURN]:
collided_interaction_sprite = pygame.sprite.spritecollide(self,self.interaction,False)
if collided_interaction_sprite:
if collided_interaction_sprite[0].name == 'Trader':
self.toggle_shop()
else:
self.status = 'left_idle'
self.sleep = True
def get_status(self):
# idle
if self.direction.magnitude() == 0:
self.status = self.status.split('_')[0] + '_idle'
# tool use
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():
if hasattr(sprite, 'hitbox'):
if sprite.hitbox.colliderect(self.hitbox):
if direction == 'horizontal':
if self.direction.x > 0: # moving right
self.hitbox.right = sprite.hitbox.left
if self.direction.x < 0: # moving left
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: # moving down
self.hitbox.bottom = sprite.hitbox.top
if self.direction.y < 0: # moving up
self.hitbox.top = sprite.hitbox.bottom
self.rect.centery = self.hitbox.centery
self.pos.y = self.hitbox.centery
def move(self,dt):
# normalizing a vector
if self.direction.magnitude() > 0:
self.direction = self.direction.normalize()
# horizontal movement
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')
# vertical movement
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()
if self.auto_watering and self.selected_tool == 'water':
self.soil_layer.water(self.rect.center)
self.watering.play()
self.move(dt)
self.animate(dt)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。