Я пытаюсь построить свою первую игру, используя Python и Kivy.Я использовал Kivy: s Pong Game Tutorial в качестве основы для моей игры https://kivy.org/doc/stable/tutorials/pong.html, и я получил свой корабль (png-файл) на заднем плане.Затем я последовал руководству Sentex по кнопкам https://youtu.be/CYNWK2GpwgA и попытался перенести мою игру в систему меню.
Теперь моя игра не работает.Я получаю: 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...