Reputation: 1
I´m trying to build my first game using Python and Kivy. I have used Kivy:s Pong Game Tutorial as an base for my game https://kivy.org/doc/stable/tutorials/pong.html and I did get my ship (png-file) move on a background. Then I followed Sentex guide on buttons https://youtu.be/CYNWK2GpwgA and tried to move my game in to a menu system.
Now my game does not work. I get: AttributeError: 'NoneType' object has no attribute 'center_x' when I try to move my ship.
I have tried to trace back the problem and understand what´s happening. At first I thought the error was because of the ship was an NoneType object but when I use .self. I use the class SpaceGame_TheGame? How can that be a NoneType when I use it in the class it self?
(I´m new to programming and new to the forum, I'm trying my best but I guess both code and post looks like crap to you ppl...)
Thankful for any help!
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...
Upvotes: 0
Views: 327
Reputation: 16041
self.player.center_x = touch.x
AttributeError: 'NoneType' object has no attribute 'center_x'
player
is defined as an ObjectProperty and initialized to None
inside the class SpaceGame_TheGame(Widget)
. This property is NOT updated to hold the instance of the SpaceShip
referenced by the id: space_ship
.
Therefore, when your app tried to retrieve center_x
via player
, it threw an error i.e. NoneType
object.
In your Kivy app, there are two unique ObjectProperty with the name, player
. One is declared in class SpaceGame_TheGame(Widget)
. The other one is automatically created by Kivy and added to the Screen
widget, SpaceGame
because the widget doesn’t have a property with that given name, player
.
player: space_ship
from class rule, <SpaceGame>:
to under instantiated object, 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()
method, replace self.player.center_x = touch.x
and self.player.center_y = touch.y
with self.player.pos = touch.pos
class SpaceGame_TheGame(Widget):
player = ObjectProperty(None)
def on_touch_move(self, touch):
self.player.pos = touch.pos
Upvotes: 0