Kivy 无法通过单击更改图像位置
Kivy can't change image position with click
我的代码有一个大问题。我想在单击按钮时更改屏幕背景图像的 x 位置。实际上我认为我正在正确地改变位置。因为我正在用其他按钮检查图像的当前位置。但是屏幕上什么也没有。图像没有移动到任何地方。我的错是什么或者我不应该使用小部件在屏幕上显示图像?这个小游戏有什么办法吗
我的.py文件:
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen, NoTransition , FadeTransition
from kivy.properties import ObjectProperty, NumericProperty
from kivy.lang import Builder
from kivy.uix.gridlayout import GridLayout
from kivy.uix.layout import Layout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.image import Image
from kivy.core.window import Window
Builder.load_file('jumpergame.kv')
class MainMenu(Screen):
pass
class Dark(Screen):
pass
class SettingsMenu(Screen):
pass
class Game(Screen):
first_image_py = ObjectProperty(None)
def test(self):
print(self.first_image_py.pos)
def swipe(self):
print(self.first_image_py.pos[0])
self.first_image_py.pos[0] -= 100
print(self.first_image_py.pos[0])
def show_bg_first_pos(self):
print('*******************')
print('First Background [X] Position ==>',self.first_image_py.pos[0])
print('First Background [Y] Position ==>',self.first_image_py.pos[1])
return self.bg_first_pos_x,self.bg_first_pos_y
def show_screen_size(self):
print('*******************')
print('Screen [X] Size ==>',self.width)
print('Screen [Y] Size ==>',self.height)
def show_bg_second_pos(self):
print('*******************')
sm = ScreenManager()
sm.add_widget(MainMenu(name='mainmenu'))
sm.add_widget(SettingsMenu(name='settings'))
sm.add_widget(Game(name='game'))
sm.add_widget(Dark(name='dark'))
class JumperGame(App):
def build(self):
return sm
if __name__ == "__main__":
JumperGame().run()
我的 .kv 文件:
<MainMenu>:
BoxLayout:
orientation : 'vertical'
Label:
text: 'JumperGame v1.00 \n by 320V'
Button:
text: 'Start'
on_press: root.manager.current = 'game'
Button:
text: 'Settings'
on_press: root.manager.current = 'settings'
<SettingsMenu>:
Button:
text: 'BACK to MENU'
on_press: root.manager.current = 'mainmenu'
<Game>:
first_image_py : first_image_kv
Widget:
id: first_image_kv
pos: 0,0
canvas.before:
Color:
rgb: (1,1,1)
Rectangle:
source: 'bg.png'
size: self.width , self.height
AnchorLayout:
anchor_x : 'right'
anchor_y : 'top'
Button:
text: 'Check image 1 pos'
size_hint: .25,.1
on_press: root.show_bg_first_pos()
AnchorLayout:
anchor_x : 'left'
anchor_y : 'top'
Button:
text: 'Check Image 2 Pos'
size_hint:.25,.1
on_press: root.show_bg_second_pos()
AnchorLayout:
anchor_x : 'center'
anchor_y : 'bottom'
Button:
text: 'Check Window Size'
size_hint: .25 , .1
on_press: root.show_screen_size()
FloatLayout:
canvas:
Color:
rgb: (1,1,1)
Rectangle:
id: bg_Image_Second
source: 'bg.png'
size: self.width , self.height
pos: root.width,root.height
Button:
text: 'Swipe Images'
size_hint: .1,.1
on_press: root.swipe()
Button:
text: 'TEST'
size_hint: .1 , .1
on_press: root.test()
pos: 50,0
您的 swipe
方法正在改变您的小部件的位置,但您的图像在 Rectangle
canvas
指令中。由于您没有将 Rectangle
的位置绑定到 Widget
的位置,图像永远不会移动。您只需要通过在 kv
文件中添加 pos: self.pos
来添加该绑定:
<Game>:
first_image_py : first_image_kv
Widget:
id: first_image_kv
pos: 0,0
canvas.before:
Color:
rgb: (1,1,1)
Rectangle:
source: 'bg.png'
size: self.width , self.height
pos: self.pos
我的代码有一个大问题。我想在单击按钮时更改屏幕背景图像的 x 位置。实际上我认为我正在正确地改变位置。因为我正在用其他按钮检查图像的当前位置。但是屏幕上什么也没有。图像没有移动到任何地方。我的错是什么或者我不应该使用小部件在屏幕上显示图像?这个小游戏有什么办法吗
我的.py文件:
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen, NoTransition , FadeTransition
from kivy.properties import ObjectProperty, NumericProperty
from kivy.lang import Builder
from kivy.uix.gridlayout import GridLayout
from kivy.uix.layout import Layout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.image import Image
from kivy.core.window import Window
Builder.load_file('jumpergame.kv')
class MainMenu(Screen):
pass
class Dark(Screen):
pass
class SettingsMenu(Screen):
pass
class Game(Screen):
first_image_py = ObjectProperty(None)
def test(self):
print(self.first_image_py.pos)
def swipe(self):
print(self.first_image_py.pos[0])
self.first_image_py.pos[0] -= 100
print(self.first_image_py.pos[0])
def show_bg_first_pos(self):
print('*******************')
print('First Background [X] Position ==>',self.first_image_py.pos[0])
print('First Background [Y] Position ==>',self.first_image_py.pos[1])
return self.bg_first_pos_x,self.bg_first_pos_y
def show_screen_size(self):
print('*******************')
print('Screen [X] Size ==>',self.width)
print('Screen [Y] Size ==>',self.height)
def show_bg_second_pos(self):
print('*******************')
sm = ScreenManager()
sm.add_widget(MainMenu(name='mainmenu'))
sm.add_widget(SettingsMenu(name='settings'))
sm.add_widget(Game(name='game'))
sm.add_widget(Dark(name='dark'))
class JumperGame(App):
def build(self):
return sm
if __name__ == "__main__":
JumperGame().run()
我的 .kv 文件:
<MainMenu>:
BoxLayout:
orientation : 'vertical'
Label:
text: 'JumperGame v1.00 \n by 320V'
Button:
text: 'Start'
on_press: root.manager.current = 'game'
Button:
text: 'Settings'
on_press: root.manager.current = 'settings'
<SettingsMenu>:
Button:
text: 'BACK to MENU'
on_press: root.manager.current = 'mainmenu'
<Game>:
first_image_py : first_image_kv
Widget:
id: first_image_kv
pos: 0,0
canvas.before:
Color:
rgb: (1,1,1)
Rectangle:
source: 'bg.png'
size: self.width , self.height
AnchorLayout:
anchor_x : 'right'
anchor_y : 'top'
Button:
text: 'Check image 1 pos'
size_hint: .25,.1
on_press: root.show_bg_first_pos()
AnchorLayout:
anchor_x : 'left'
anchor_y : 'top'
Button:
text: 'Check Image 2 Pos'
size_hint:.25,.1
on_press: root.show_bg_second_pos()
AnchorLayout:
anchor_x : 'center'
anchor_y : 'bottom'
Button:
text: 'Check Window Size'
size_hint: .25 , .1
on_press: root.show_screen_size()
FloatLayout:
canvas:
Color:
rgb: (1,1,1)
Rectangle:
id: bg_Image_Second
source: 'bg.png'
size: self.width , self.height
pos: root.width,root.height
Button:
text: 'Swipe Images'
size_hint: .1,.1
on_press: root.swipe()
Button:
text: 'TEST'
size_hint: .1 , .1
on_press: root.test()
pos: 50,0
您的 swipe
方法正在改变您的小部件的位置,但您的图像在 Rectangle
canvas
指令中。由于您没有将 Rectangle
的位置绑定到 Widget
的位置,图像永远不会移动。您只需要通过在 kv
文件中添加 pos: self.pos
来添加该绑定:
<Game>:
first_image_py : first_image_kv
Widget:
id: first_image_kv
pos: 0,0
canvas.before:
Color:
rgb: (1,1,1)
Rectangle:
source: 'bg.png'
size: self.width , self.height
pos: self.pos