我正在尝试使用Python和Kivy构建我的第一款游戏。我已经使用Kivy:s Pong游戏教程作为我的游戏https://kivy.org/doc/stable/tutorials/pong.html的基础,但确实使我的飞船(png文件)在背景上移动了。然后,我按照按钮https://youtu.be/CYNWK2GpwgA上的Sentex指南进行操作,并尝试将我的游戏移至菜单系统。
现在我的游戏无法正常工作。我得到:AttributeError:当我尝试移动船舶时,“ NoneType”对象没有属性“ center_x”。
我试图追溯问题并了解正在发生的事情。起初我以为错误是因为该船是一个NoneType对象,但是当我使用.self时。我使用类SpaceGame_TheGame吗?当我在本身的类中使用它时,怎么可能是NoneType?
(我是编程新手,还是论坛新手,我正在尽力而为,但我想代码和帖子对您来说都是废话……)
感谢您的帮助!
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty #Want my ship to be an object (right?)
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition #For menu system
from kivy.lang import Builder
#The different screens
class MainScreen(Screen):
pass
class SpaceGame(Screen):
pass
class Installningar(Screen):
pass
class ScreenManagement(ScreenManager):
pass
# The game
class SpaceShip(Widget): #The ship is a class
pass
class SpaceGame_TheGame(Widget):
player = ObjectProperty(None) #I want the ship to be an object that can get hit and stuff
def on_touch_move(self,touch): #For movement
pass
self.player.center_x = touch.x
self.player.center_y = touch.y
#Start up stuff
presentation = Builder.load_file("space.kv") #Presentation är bara ett namn på detta som han valde
class MainApp(App):
def build(self):
return presentation
if __name__ == '__main__':
MainApp().run()
#: kivy 1.10.1
#: import FadeTransition kivy.uix.screenmanager.FadeTransition
#: import Window kivy.core.window
ScreenManagement:
transition: FadeTransition()
MainScreen:
SpaceGame:
Installningar:
<Button>:
font_size: 20
color: 0,1,0,1
size_hint: 0.2, 0.15
<MainScreen>:
name: "main"
Button:
on_release: app.root.current = "spacegame"
text: "Start the game!"
font_size: 20
pos_hint: {"center_x": 0.5, 'center_y':0.7}
Button:
on_release: app.root.current = "installningar"
text: "Settings"
font_size: 20
pos_hint: {"center_x": 0.5, 'center_y':0.5}
Button:
on_release: quit()
text: "Quit"
font_size: 20
pos_hint: {"center_x": 0.5, 'center_y':0.3}
<SpaceShip>:
canvas:
Rectangle:
size: 45,45
pos: self.pos
source: 'rymdskepp.png'
<SpaceGame>:
name: "spacegame"
player: space_ship
canvas:
Rectangle:
pos: self.pos
size: self.size
source: 'bakgrund.jpg'
FloatLayout:
SpaceShip:
id: space_ship
x: root.x
center_y: root.center_y
SpaceGame_TheGame
id: spacegame_thegame # an id for referring to this widget
Button:
text: "Back"
pos_hint: {"right": 1, 'top':1}
on_release: app.root.current = "main"
<Installningar>:
name: "installningar"
FloatLayout:
Button:
text: "Back"
pos_hint: {"right": 1, 'top':1}
on_release: app.root.current = "main"
[INFO ] [Logger ] Record log in C:\Users\fredr\.kivy\logs\kivy_19-06-13_28.txt
[INFO ] [Kivy ] v1.10.1
[INFO ] [Python ] v3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)]
[INFO ] [Factory ] 194 symbols loaded
[INFO ] [Image ] Providers: img_tex, img_dds, img_sdl2, img_gif (img_pil, img_ffpyplayer ignored)
[INFO ] [Window ] Provider: sdl2
[INFO ] [GL ] Using the "OpenGL" graphics system
[INFO ] [GL ] GLEW initialization succeeded
[INFO ] [GL ] Backend used <glew>
[INFO ] [GL ] OpenGL version <b'4.6.0 NVIDIA 388.13'>
[INFO ] [GL ] OpenGL vendor <b'NVIDIA Corporation'>
[INFO ] [GL ] OpenGL renderer <b'GeForce GTX 980 Ti/PCIe/SSE2'>
[INFO ] [GL ] OpenGL parsed version: 4, 6
[INFO ] [GL ] Shading version <b'4.60 NVIDIA'>
[INFO ] [GL ] Texture max size <16384>
[INFO ] [GL ] Texture max units <32>
[INFO ] [Window ] auto add sdl2 input provider
[INFO ] [Window ] virtual keyboard not allowed, single mode, not docked
[INFO ] [Text ] Provider: sdl2
[INFO ] [GL ] NPOT texture support is available
[INFO ] [Base ] Start application main loop
[INFO ] [Base ] Leaving application in progress...
Traceback (most recent call last):
File "C:/Users/fredr/OneDrive - Nyköpings Enskilda Gymnasium/Matematik/Programmering/Blandat/Kivy/Mitt spel 2/main.py", line 42, in <module>
MainApp().run()
File "C:\Users\fredr\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\app.py", line 826, in run
runTouchApp()
File "C:\Users\fredr\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\base.py", line 502, in runTouchApp
EventLoop.window.mainloop()
File "C:\Users\fredr\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\core\window\window_sdl2.py", line 727, in mainloop
self._mainloop()
File "C:\Users\fredr\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\core\window\window_sdl2.py", line 460, in _mainloop
EventLoop.idle()
File "C:\Users\fredr\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\base.py", line 340, in idle
self.dispatch_input()
File "C:\Users\fredr\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\base.py", line 325, in dispatch_input
post_dispatch_input(*pop(0))
File "C:\Users\fredr\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\base.py", line 231, in post_dispatch_input
listener.dispatch('on_motion', etype, me)
File "kivy\_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
File "C:\Users\fredr\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\core\window\__init__.py", line 1362, in on_motion
self.dispatch('on_touch_move', me)
File "kivy\_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
File "C:\Users\fredr\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\core\window\__init__.py", line 1388, in on_touch_move
if w.dispatch('on_touch_move', touch):
File "kivy\_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
File "C:\Users\fredr\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\uix\screenmanager.py", line 1196, in on_touch_move
return super(ScreenManager, self).on_touch_move(touch)
File "C:\Users\fredr\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\uix\widget.py", line 471, in on_touch_move
if child.dispatch('on_touch_move', touch):
File "kivy\_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
File "C:\Users\fredr\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\uix\relativelayout.py", line 296, in on_touch_move
ret = super(RelativeLayout, self).on_touch_move(touch)
File "C:\Users\fredr\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\uix\widget.py", line 471, in on_touch_move
if child.dispatch('on_touch_move', touch):
File "kivy\_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
File "C:\Users\fredr\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\uix\widget.py", line 471, in on_touch_move
if child.dispatch('on_touch_move', touch):
File "kivy\_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
File "C:/Users/fredr/OneDrive - Nyköpings Enskilda Gymnasium/Matematik/Programmering/Blandat/Kivy/Mitt spel 2/main.py", line 29, in on_touch_move
self.player.center_x = touch.x
AttributeError: 'NoneType' object has no attribute 'center_x'
Process finished with exit code 1
I want my ship to move and don't get the error message...
答案 0 :(得分:0)
self.player.center_x = touch.x
AttributeError: 'NoneType' object has no attribute 'center_x'
player
被定义为ObjectProperty,并在类None
中初始化为SpaceGame_TheGame(Widget)
。此属性 NOT 已更新,以保存SpaceShip
引用的id: space_ship
的实例。
因此,当您的应用尝试通过center_x
检索player
时,它引发了错误,即NoneType
对象。
在您的Kivy应用中,有两个唯一的ObjectProperty,名称为player
。在类SpaceGame_TheGame(Widget)
中声明了一个。另一个由Kivy自动创建,并添加到Screen
小部件SpaceGame
中,因为该小部件没有具有给定名称player
的属性。>
player: space_ship
从类规则<SpaceGame>:
移至实例化对象SpaceGame_TheGame:
<SpaceGame>:
name: "spacegame"
canvas:
Rectangle:
pos: self.pos
size: self.size
source: 'bakgrund.jpg'
...
SpaceGame_TheGame:
id: spacegame_thegame # an id for referring to this widget
player: space_ship
on_touch_move()
方法中,将self.player.center_x = touch.x
和self.player.center_y = touch.y
替换为self.player.pos = touch.pos
class SpaceGame_TheGame(Widget):
player = ObjectProperty(None)
def on_touch_move(self, touch):
self.player.pos = touch.pos