kivy图像对撞机尺寸方式太大了

时间:2018-05-31 14:25:33

标签: python python-3.x kivy

我尝试做的事情:

我希望我的程序在与中间的sprite碰撞时打印一条消息。

问题:

它不会停止打印它碰撞。我用pdb调查它,它说播放器精灵是整个屏幕的大小,这很奇怪,因为我将图像大小设置为32px×32px。

它看起来像什么

enter image description here

提及的代码和精灵

链接到提到的块

enter image description here

链接到提到的精灵

enter image description here

from kivy.app import App
from kivy.lang import Builder
from kivy.clock import Clock
import random
import time
from kivy.uix.image import Image
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.core.window import Window
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.screenmanager import ScreenManager, Screen


#############################################################################

class Collider(Image):
    pass

#############################################################################

root = FloatLayout()

class GameScreen(Screen):
    def __init__(self, **kwargs):
        super(GameScreen, self).__init__(**kwargs)
        self.wimg = Basic(size = (32,32), source='sprite.png', pos=(270,-120))
        self.block = Collider(size = (32,32), source = 'block.png')


    def playy(self):
        root.add_widget(self.wimg)
        Clock.schedule_interval(self.wimg.update, 1.0 / 60.0)

    def worldgen1(self):
        self.add_widget(root)
        root.add_widget(self.block)

    def on_enter(self):
        self.worldgen1()
        self.playy()

#############################################################################

class Basic(Image):

    pFrame = 0

    def __init__(self, **kwargs):
        super(Basic, self).__init__(**kwargs)
        self._keyboard = Window.request_keyboard(None, self)
        if not self._keyboard:
            return
        self._keyboard.bind(on_key_down=self.on_keyboard_down)

    def on_keyboard_down(self, keyboard, keycode, text, modifiers):
        if keycode[1] == 'left':
            self.x -= 10
        elif keycode[1] == 'right':
            self.x += 10
        elif keycode[1] == 'up':
            self.y += 10
        elif keycode[1] == 'down':
            self.y -= 10
        else:
            return False
        return True

    def update(self, dt):
        self.pFrame += 1
        if self.parent.parent.block.collide_widget(self.parent.parent.wimg):
            print ("collide with", self.parent.parent.block)

#############################################################################

class MeinApp(App):

    def build(self):
        sm = ScreenManager()
        sm.add_widget(GameScreen(name='gameplay'))
        return sm

if __name__ == '__main__':
    MeinApp().run()

1 个答案:

答案 0 :(得分:0)

在这种情况下,问题是FloatLayout使用size_hint设置窗口小部件的大小,因为它没有设置它,它们取值(1, 1),即,窗户的大小。

除了使用size_hint中的(None, None)allow_stretch中的True之外,解决方法是将keep_ratio设置为False。在代码中,我将块pos_hint设置为(0.5, 0.5),它会将其锚定到该位置,因此它不会移动,如果您希望它能够移动,则必须设置{{1} },而不是pos

pos_hint