python arcade库教程(2)

本文继续探讨Python的arcade库,详细解析Window类中的方法,包括鼠标移动、拖动、滚动以及键盘按键的事件处理。通过示例程序展示了如何监听并响应这些事件,同时介绍了ASCII码在事件处理中的作用。
摘要由CSDN通过智能技术生成

上次说了窗口的创建和框架创建的区别、on_draw方法、on_update方法和鼠标按下松开时调用的on_mouse_release,on_mouse_press。

本篇文章就再次讲解arcade库Window类里的方法。

对了,有一个事情上篇文章忘说了,在把窗口实例化的时候,可以填3个属性,分别是此窗口的宽度(x值)、长度(y值)和标题,窗口默认大小是800*600,默认的标题是‘arcade window’,这个上篇文章仔细的小伙伴应该看出来了。

还是一样编写一个程序来看一下

#导入库中大部分功能
from arcade import *
#子类继承父类Window
class MyWindow(Window):
    pass
#类的实例化,窗口长度400宽度1000,标题为‘随便一个标题’
w = MyWindow(1000,400,'随便一个标题')
 
#运行
run()

运行之后出现的窗口明显比之前的窗口更加扁一些,在此窗口中看到的图片大小肯定不是1000*400,但是把图片保存下来之后就可以看到它的大小了,仍然不是1000*400,因为要算上边框。

 好了话题有点偏了,我们进入今天的内容

接着上篇的,我们先讲鼠标的方法。

on_mouse_motion(self,x,y,dx,dy)

这个方法相信大家都能看出来,是在鼠标移动时自动按当前程序帧率执行。和其它的鼠标方法一样,前面的x,y是指当前鼠标的坐标,后面的是这个函数的“专属”参数

这个函数的“专属”参数dx和dy分别指鼠标在上一帧在x方向和y方向移动的值

dx>0,鼠标向右运动,dx<0,鼠标向左运动,dy<0,鼠标向下,反之则向上。

编写一个程序来具体的看一下。

#导入库中大部分功能
from arcade import *
#子类继承父类Window
class MyWindow(Window):
    #鼠标移动时自动执行 
    def on_mo
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Learn and use Python and PyGame to design and build cool arcade games. In Program Arcade Games: With Python and PyGame, Second Edition, Dr. Paul Vincent Craven teaches you how to create fun and simple quiz games; integrate and start using graphics; animate graphics; integrate and use game controllers; add sound and bit-mapped graphics; and build grid-based games. After reading and using this book, you'll be able to learn to program and build simple arcade game applications using one of today's most popular programming languages, Python. You can even deploy onto Steam and other Linux-based game systems as well as Android, one of today's most popular mobile and tablet platforms. You'll learn: How to create quiz games How to integrate and start using graphics How to animate graphics How to integrate and use game controllers How to add sound and bit-mapped graphics How to build grid-based games Audience This book assumes no prior programming knowledge. Table of Contents Chapter 1: Before Getting Started… Chapter 2: Create a Custom Calculator Chapter 3: What Is a Computer Language? Chapter 4: Quiz Games and If Statements Chapter 5: Guessing Games with Random Numbers and Loops Chapter 6: Introduction to Graphics Chapter 7: Back to Looping Chapter 8: Introduction to Lists Chapter 9: Introduction to Animation Chapter 10: Functions Chapter 11: Controllers and Graphics Chapter 12: Bitmapped Graphics and Sound Chapter 13: Introduction to Classes Chapter 14: Introduction to Sprites Chapter 15: Libraries and Modules Chapter 16: Searching Chapter 17: Array-Backed Grids Chapter 18: Sorting Chapter 19: Exceptions Chapter 20: Recursion Chapter 21: Formatting Chapter 22: Exercises
Arcade 灯光是 Arcade 游戏引擎中的一个重要特性,使得游戏更加生动和有趣。下面是一个简单的 Arcade 灯光教程,帮助你快速入门。 首先,在你的代码中导入以下: ```python import arcade import random ``` 接下来,定义一个 `Light` 类来创建灯光效果: ```python class Light: def __init__(self, x, y, color, radius): self.x = x self.y = y self.color = color self.radius = radius self.alpha = 255 self.alpha_decay = random.randint(5, 15) def draw(self): arcade.draw_circle_filled(self.x, self.y, self.radius, self.color + (self.alpha,)) def update(self): self.alpha -= self.alpha_decay if self.alpha <= 0: self.alpha = 0 ``` 这个类有一个 `__init__` 函数,用于初始化灯光的位置、颜色和半径。`alpha` 属性是灯光的不透明度,每次更新时会逐渐减少。`alpha_decay` 属性是每次更新时减少的不透明度值,可以随机生成。 `draw` 函数用于绘制灯光,使用 `arcade.draw_circle_filled` 函数来绘制实心圆。这里需要注意,颜色需要加上 `alpha` 值,以实现灯光逐渐消失的效果。 `update` 函数用于更新灯光的透明度,每次减少 `alpha_decay` 值,直到透明度为 0。 接下来,在你的游戏场景中创建一个灯光列表: ```python lights = [] ``` 然后,可以在 `on_draw` 函数中绘制这些灯光: ```python def on_draw(): arcade.start_render() for light in lights: light.draw() ``` 现在,你可以在 `on_update` 函数中更新每个灯光的透明度: ```python def on_update(delta_time): for light in lights: light.update() ``` 最后,你还需要添加一些代码来创建新的灯光。你可以在 `on_mouse_press` 函数中添加以下代码: ```python def on_mouse_press(x, y, button, modifiers): if button == arcade.MOUSE_BUTTON_LEFT: color = (255, 255, 0) # yellow radius = 50 lights.append(Light(x, y, color, radius)) ``` 这个代码会在鼠标左键按下时创建一个新的黄色灯光,半径为 50。 现在,你已经创建了一个简单的 Arcade 灯光效果!你可以根据自己的需要调整灯光的颜色、大小和透明度,来创造出更加生动、有趣的游戏场景。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值