赞
踩
接上篇文章继续解绍arcade游戏编程的基本知识。游戏的灯光阴影效果
蓝色效果
白色效果
#创建一个灯光来跟随玩家。稍后,当玩家移动时,我们将对其进行定位。
#我们只会在玩家打开灯光时将其添加到灯光层打开。我们从熄灯开始。
radius = 150
mode = 'soft'
color = arcade.csscolor.WHITE
self.player_light = Light(0, 0, radius, color, mode)
# 白色灯光
x = 100
y = 200
radius = 100
mode = 'soft'
color = arcade.csscolor.WHITE
light = Light(x, y, radius, color, mode)
self.light_layer.add(light)
# 创建三个重叠的RGB灯光,但不会褪色的“硬”灯。
x = 650
y = 150
radius = 100
mode = 'hard'
color = arcade.csscolor.RED
light = Light(x, y, radius, color, mode)
self.light_layer.add(light)
def on_key_press(self, key, _):
if key == arcade.key.UP:
self.player_sprite.change_y = MOVEMENT_speed
elif key == arcade.key.DOWN:
self.player_sprite.change_y = -MOVEMENT_speed
elif key == arcade.key.LEFT:
self.player_sprite.change_x = -MOVEMENT_speed
elif key == arcade.key.RIGHT:
self.player_sprite.change_x = MOVEMENT_speed
elif key == arcade.key.SPACE:
# 灯光开关
if self.player_light in self.light_layer:
self.light_layer.remove(self.player_light)
else:
self.light_layer.add(self.player_light)
"""
应用灯光示例
"""
import arcade
from arcade.experimental.lights import Light, LightLayer
SCREEN_width = 1024
SCREEN_height = 768
VIEWPORT_margin = 200
MOVEMENT_speed = 5
# 这是用于“环境光”的颜色。如果你不想要环境光,设置为黑色。
COLOR_ambient = (10, 10, 10)
class MyGame(arcade.Window):
""" Main Game Window """
def __init__(self, width, height, title='灯光示例'):
super().__init__(width, height,title, resizable=True)
# 精灵列表
self.background_sprite_list = None
self.player_list = None
self.wall_list = None
self.player_sprite = None
#引擎
self.physics_engine = None
# 用于滚动
self.view_left = 0
self.view_bottom = 0
# --- 灯光相关 ---
# 灯光层
self.light_layer = None
# 与玩家一起移动并打开/关闭单个灯光
self.player_light = None
def setup(self):
""" Create everything """
# 精灵列表
self.background_sprite_list = arcade.SpriteList()
self.player_list = arcade.SpriteList()
self.wall_list = arcade.SpriteList()
# 玩家
self.player_sprite = arcade.Sprite(":resources:images/animated_characters/female_person/"
"femalePerson_idle.png",
0.4)
self.player_sprite.center_x = 64
self.player_sprite.center_y = 270
self.player_list.append(self.player_sprite)
# --- 灯光 ---
# 灯光一定照在什么东西上。如果没有背景子画面或颜色,
# #你只会看到黑色。因此,我们使用一个循环来创建一堆砖砖背景。
for x in range(-128, 2000, 128):
for y in range(-128, 1000, 128):
sprite = arcade.Sprite(":resources:images/tiles/brickTextureWhite.png")
sprite.position = x, y
self.background_sprite_list.append(sprite)
#创建一个灯光层,用于渲染对象,然后后期处理和添加灯光。这必须与屏幕大小匹配。
self.light_layer = LightLayer(SCREEN_width, SCREEN_height)
# 背景色
self.light_layer.set_background_color(arcade.color.BLACK)
# 白色灯光
x = 100
y = 200
radius = 100
mode = 'soft'
color = arcade.csscolor.WHITE
light = Light(x, y, radius, color, mode)
self.light_layer.add(light)
# 创建重叠的大白光
x = 300
y = 150
radius = 200
color = arcade.csscolor.WHITE
mode = 'soft'
light = Light(x, y, radius, color, mode)
self.light_layer.add(light)
# 红光
x = 50
y = 450
radius = 100
mode = 'soft'
color = arcade.csscolor.RED
light = Light(x, y, radius, color, mode)
self.light_layer.add(light)
x = 250
y = 450
radius = 100
mode = 'soft'
color = arcade.csscolor.GREEN
light = Light(x, y, radius, color, mode)
self.light_layer.add(light)
x = 450
y = 450
radius = 100
mode = 'soft'
color = arcade.csscolor.BLUE
light = Light(x, y, radius, color, mode)
self.light_layer.add(light)
# 创建三个重叠的RGB灯光
x = 650
y = 450
radius = 100
mode = 'soft'
color = arcade.csscolor.RED
light = Light(x, y, radius, color, mode)
self.light_layer.add(light)
x = 750
y = 450
radius = 100
mode = 'soft'
color = arcade.csscolor.GREEN
light = Light(x, y, radius, color, mode)
self.light_layer.add(light)
x = 850
y = 450
radius = 100
mode = 'soft'
color = arcade.csscolor.BLUE
light = Light(x, y, radius, color, mode)
self.light_layer.add(light)
# 创建三个重叠的RGB灯光,但不会褪色的“硬”灯。
x = 650
y = 150
radius = 100
mode = 'hard'
color = arcade.csscolor.RED
light = Light(x, y, radius, color, mode)
self.light_layer.add(light)
x = 750
y = 150
radius = 100
mode = 'hard'
color = arcade.csscolor.GREEN
light = Light(x, y, radius, color, mode)
self.light_layer.add(light)
x = 850
y = 150
radius = 100
mode = 'hard'
color = arcade.csscolor.BLUE
light = Light(x, y, radius, color, mode)
self.light_layer.add(light)
#创建一个灯光来跟随玩家。稍后,当玩家移动时,我们将对其进行定位。
#我们只会在玩家打开灯光时将其添加到灯光层打开。我们从熄灯开始。
radius = 150
mode = 'soft'
color = arcade.csscolor.WHITE
self.player_light = Light(0, 0, radius, color, mode)
self.physics_engine = arcade.PhysicsEngineSimple(self.player_sprite, self.wall_list)
# 设置
self.view_left = 0
self.view_bottom = 0
def on_draw(self):
self.clear()
# 应该受到灯光影响的所有内容都会在该窗口中渲染“with”语句。屏幕上还没有渲染任何内容,只有灯光层。
with self.light_layer:
self.background_sprite_list.draw()
self.player_list.draw()
# 将灯光层绘制到屏幕上。这将使整个屏幕充满点亮的内容,即我们在上面的光层中绘制的内容
self.light_layer.draw(ambient_color=COLOR_ambient)
arcade.draw_text("按space转换显示与否灯光",
10 + self.view_left, 10 + self.view_bottom,
arcade.color.WHITE, 20)
def on_resize(self, width, height):
"""缩放 """
# 缩放光层
self.light_layer.resize(width, height)
# 滚动屏幕
self.scroll_screen()
def on_key_press(self, key, _):
if key == arcade.key.UP:
self.player_sprite.change_y = MOVEMENT_speed
elif key == arcade.key.DOWN:
self.player_sprite.change_y = -MOVEMENT_speed
elif key == arcade.key.LEFT:
self.player_sprite.change_x = -MOVEMENT_speed
elif key == arcade.key.RIGHT:
self.player_sprite.change_x = MOVEMENT_speed
elif key == arcade.key.SPACE:
# 灯光开关
if self.player_light in self.light_layer:
self.light_layer.remove(self.player_light)
else:
self.light_layer.add(self.player_light)
def on_key_release(self, key, _):
if key == arcade.key.UP or key == arcade.key.DOWN:
self.player_sprite.change_y = 0
elif key == arcade.key.LEFT or key == arcade.key.RIGHT:
self.player_sprite.change_x = 0
def scroll_screen(self):
""" 管理滚动 """
# 左
left_boundary = self.view_left + VIEWPORT_margin
if self.player_sprite.left < left_boundary:
self.view_left -= left_boundary - self.player_sprite.left
# 右
right_boundary = self.view_left + self.width - VIEWPORT_margin
if self.player_sprite.right > right_boundary:
self.view_left += self.player_sprite.right - right_boundary
# 上
top_boundary = self.view_bottom + self.height - VIEWPORT_margin
if self.player_sprite.top > top_boundary:
self.view_bottom += self.player_sprite.top - top_boundary
# 下
bottom_boundary = self.view_bottom + VIEWPORT_margin
if self.player_sprite.bottom < bottom_boundary:
self.view_bottom -= bottom_boundary - self.player_sprite.bottom
# 确保我们的边界是整数值。当视口执行此操作时支持浮点数,对于这个应用程序,我们需要每个像素
# 以直接映射到屏幕上的像素。我们不想任何舍入误差。
self.view_left = int(self.view_left)
self.view_bottom = int(self.view_bottom)
arcade.set_viewport(self.view_left,
self.width + self.view_left,
self.view_bottom,
self.height + self.view_bottom)
def on_update(self, delta_time):
# 更新所有精灵
self.physics_engine.update()
# 我们可以通过设置位置轻松移动灯光
self.player_light.position = self.player_sprite.position
# 滚动屏幕
self.scroll_screen()
if __name__ == "__main__":
window = MyGame(SCREEN_width, SCREEN_height)
window.setup()
arcade.run()
可关注博主后,私聊博主免费获取
需要技术指导,育娃新思考,企业软件合作等更多服务请联系博主
今天是以此模板持续更新此育儿专栏的第 40/50次。
可以关注我,点赞我、评论我、收藏我啦。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。