Sam*_*mmy 5 python module button arcade
我正在尝试使用 python 在街机中创建一个交互式按钮。我正在使用操作系统导入图像,想知道是否可以将单击功能转换为图像。我希望通过点击图片打开一个新窗口。我知道 tkinter 主要用于此目的,但我正在创建一个游戏并且不想使用 tkinter。我也不喜欢使用 PyGame。我也不知道如何在从操作系统导入的图像上应用边界。
我在 arcade.academy 上查找了有关如何在街机中使用按钮的教程,但它需要使用街机形状手动绘制形状和输入文本。我还尝试了一个教程,将坐标输出到 python shell,同时单击画布上的随机点,尝试将其合并到图像中。下面的示例代码尚未完成,但我已经尝试这样做。
import arcade
import os
SCREEN_WIDTH = 700
SCREEN_HEIGHT = 700
class MyGame(arcade.Window):
def __init__(self):
super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, "sprites")
file_path = os.path.dirname(os.path.abspath(__file__))
os.chdir(file_path)
self.player_list = None
self.player_sprite = None
self.set_mouse_visible(True)
self.pressed = (False)
arcade.set_background_color(arcade.color.AMAZON)
def Button(self, center_x, center_y, action_function):
self.player_list = arcade.SpriteList()
self.player_sprite = arcade.Sprite("osss.png", 0.5)
self.player_sprite.center_x = center_x
center_x = 50
self.player_sprite.center_y = center_y
center_y = 50
self.player_list.append(self.player_sprite)
self.action_function = action_function
def on_draw(self):
arcade.start_render()
self.player_list.draw()
def on_mouse_motion(self, x, y, dx, dy):
self.player_sprite.center_x = x
self.player_sprite.center_y = y
def update(self, delta_time):
x = self.center_x
y = self.center_y
if not self.pressed:
x -= self.Button_height
y += self.Button_height
def on_press(self):
self.pressed = True
def on_release(self):
self.pressed = False
def check_mouse_press_for_buttons(x, y, self): #Button.on_press()
for button in self.player_list:
if x > button.center_x + button.width / 2:
continue
if x < button.center_x - button.width / 2:
continue
if y > button.center_y + button.height / 2:
continue
if y < button.center_y - button.height / 2:
continue
def check_mouse_release_for_buttons(x, y, dx, dy, button_list): #make the action happen
for button in button_list:
if button.pressed:
button.on_release()
arcade.draw_randomrect_filled(x, y, dx, dy, arcade.color.PINK)
def on_release(self):
super().on_release()
self.action_function()
def main():
window = MyGame()
window.Button(center_x, center_y, action_function)
arcade.run()
if __name__ == "__main__":
main()
Run Code Online (Sandbox Code Playgroud)
我确实收到了很多“未定义”的错误消息。我还没有真正很好地分配我的价值观。我也在期待被拒绝,很多人告诉我这是不可能的。
您可以使用arcade.gui.UITextureButton类。例子:
import arcade
import arcade.gui
class App(arcade.Window):
def __init__(self):
super().__init__(400, 300, 'Button')
self.manager = arcade.gui.UIManager()
self.manager.enable()
box = arcade.gui.UIBoxLayout()
button = arcade.gui.UITextureButton(x=0, y=0, texture=arcade.load_texture('btn.png'), texture_hovered=arcade.load_texture('btn_hovered.png'), texture_pressed=arcade.load_texture('btn_pressed.png'))
box.add(button.with_space_around(bottom=20))
button.on_click = self.on_click_button
self.manager.add(arcade.gui.UIAnchorWidget(anchor_x='center_x', anchor_y='center_y', child=box))
@staticmethod
def on_click_button(event):
print('Button clicked!')
def on_draw(self):
arcade.start_render()
self.manager.draw()
App()
arcade.run()
Run Code Online (Sandbox Code Playgroud)
输出:
| 归档时间: |
|
| 查看次数: |
1148 次 |
| 最近记录: |