我是Kivy的新手,并且一直在使用Richard Jones的FlappyBird模型作为开始。我目前正在使用Kivy 1.9.2-dev0()并且喜欢使用python 2.7。我注意到,当我启动应用程序时,显示的图形大小只有一半。但是,窗口尺寸显示为正确的尺寸(288,384),但是如果我在窗口内跟踪我的位置,它将显示为尺寸的两倍。另外,我注意到x,y(0,0)位置在左下方而不是左上方?
这是我的代码:
import kivy
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.graphics import Color, Rectangle
from kivy.core.window import Window
from kivy.graphics.texture import Texture
from kivy.uix.image import Image
from kivy.clock import Clock
from kivy.uix.label import Label
from kivy.core.audio import SoundLoader
from kivy.core.text import Text
from kivy.uix.floatlayout import FloatLayout
from kivy.config import Config
from kivy.input.motionevent import MotionEvent
from kivy.animation import Animation
import random
Config.set('graphics','resizable',0)
Window.size = (288,384)
sfx_flap = SoundLoader.load('audio/flap.wav')
sfx_score = SoundLoader.load('audio/score.wav')
sfx_die = SoundLoader.load('audio/die.wav')
sfx_music = SoundLoader.load('audio/flappymusic.wav')
sfx_level_up = SoundLoader.load('audio/level_up.wav')
sfx_lose = SoundLoader.load('audio/lose.wav')
sfx_music.loop = True
class Menu(Widget):
def __init__(self):
super(Menu, self).__init__()
self.add_widget(Sprite(source='images/background.png'))
self.size = self.children[0].size
self.add_widget(Ground(source='images/ground.png'))
self.add_widget(Label(center=self.center, text="tap to start",
font_name='images/04b_19.ttf'))
self.lives_left = Label(center_x=self.center_x, top=self.top,
text='x ' + str(App.lives), font_name='images/04b_19.ttf',
font_size=20, color=(1,1,0,1), opacity=1)
self.add_widget(self.lives_left)
def on_touch_down(self, touch):
parent = self.parent
parent.remove_widget(self)
parent.add_widget(Game())
class lose(Widget):
def __init__(self):
super(lose, self).__init__()
self.add_widget(Sprite(source='images/background.png'))
self.size = self.children[0].size
self.add_widget(Ground(source='images/ground.png'))
self.add_widget(Label(center=self.center, text="You Lose!",
font_name='images/04b_19.ttf', color=(1,0,0,1)))
def on_touch_down(self, *ignore):
pass
class Sprite(Image):
def __init__(self, **kwargs):
super(Sprite, self).__init__(**kwargs)
self.size = self.texture.size
class Background(Widget):
def __init__(self, source):
super(Background, self).__init__()
self.image = Sprite(source=source)
self.add_widget(self.image)
self.size = self.image.size
# self.image_dupe = Sprite(source=source, x=self.width)
# self.add_widget(self.image_dupe)
def update(self):
self.image.x -= 2
self.image_dupe.x -= 2
if self.image.right <= 0:
self.image.x = 0
self.image_dupe.x = self.width
class Bird(Sprite):
def __init__(self, pos):
super(Bird, self).__init__(source='atlas://images/bird_anim/wing-up',
pos=pos)
self.velocity_y = 0
self.gravity = -.3
def update(self):
self.velocity_y += self.gravity
self.velocity_y = max(self.velocity_y, -10)
self.y += self.velocity_y
if self.velocity_y < -5:
self.source = 'atlas://images/bird_anim/wing-up'
elif self.velocity_y < 0:
self.source = 'atlas://images/bird_anim/wing-mid'
def on_touch_down(self, *ignore):
self.velocity_y = 5.5
self.source = 'atlas://images/bird_anim/wing-down'
sfx_flap.play()
class Ground(Sprite):
def update(self):
self.x -= 2
if self.x < -24:
self.x += 24
class Pipe(Widget):
def __init__(self, pos):
super(Pipe, self).__init__(pos=pos)
self.top_image = Sprite(source='images/pipe_top.png')
self.top_image.pos = (self.x, self.y + 4.5 * 24)
self.add_widget(self.top_image)
self.bottom_image = Sprite(source='images/pipe_bottom.png')
self.bottom_image.pos = (self.x, self.y - self.bottom_image.height)
self.add_widget(self.bottom_image)
self.width = self.top_image.width
self.scored = False
def update(self):
self.x -= 2
self.top_image.x = self.bottom_image.x = self.x
if self.right < 0:
self.parent.remove_widget(self)
class Pipes(Widget):
add_pipe = 0
def update(self, dt):
for child in list(self.children):
child.update()
self.add_pipe -= dt
if self.add_pipe < 0:
y = random.randint(self.y + 50, self.height - 50 - 4.5 * 24)
self.add_widget(Pipe(pos=(self.width, y)))
self.add_pipe = 1.5
class Game(Widget):
def __init__(self):
super(Game, self).__init__()
self.background = Background(source='images/background.png')
self.size = self.background.size
self.add_widget(self.background)
self.ground = Ground(source='images/ground.png')
self.pipes = Pipes(pos=(0, self.ground.height), size=self.size)
self.add_widget(self.pipes)
self.add_widget(self.ground)
self.score_label = Label(center_x=self.center_x,
top=self.top - 30, text="0", font_name='images/04b_19.ttf')
self.add_widget(self.score_label)
self.over_label = Label(center=self.center, opacity=0,
text="GameOver", font_name='images/04b_19.ttf')
self.add_widget(self.over_label)
self.lives_left = Label(center_x=self.center_x, top=self.top +
30, text='x ' + str(App.lives),
font_name='images/04b_19.ttf',
font_size=20, color=(1,1,0,1), opacity=0)
self.add_widget(self.lives_left)
self.bird = Bird(pos=(20, self.height / 2))
self.add_widget(self.bird)
Clock.schedule_interval(self.update, 1.0/60.0)
self.game_over = False
self.score = 0
def update(self, dt):
if self.game_over:
return
# self.background.update()
self.bird.update()
self.ground.update()
self.pipes.update(dt)
print self.background.size
if self.bird.collide_widget(self.ground):
self.game_over = True
for pipe in self.pipes.children:
if pipe.top_image.collide_widget(self.bird):
self.game_over = True
elif pipe.bottom_image.collide_widget(self.bird):
self.game_over = True
if not pipe.scored and pipe.right < self.bird.x:
pipe.scored = True
self.score += 1
self.score_label.text = str(self.score)
sfx_score.play()
if self.lives_left.opacity == 1:
self.lives_left.opacity = 0
self.lives_left.text = 'x ' + str(App.lives)
if self.score % 10 == 0 and self.score != 0:
sfx_level_up.play()
App.lives += 1
self.lives_left.opacity = 1
if self.game_over:
sfx_music.stop()
sfx_die.play()
self.over_label.opacity = 1
self.bind(on_touch_down=self._on_touch_down)
App.lives -= 1
print App.lives
def _on_touch_down(self, *ignore):
parent = self.parent
parent.remove_widget(self)
if App.lives > 0:
sfx_music.play()
parent.add_widget(Menu())
else:
parent.add_widget(lose())
sfx_lose.play()
class FlappyBird(App):
App.lives = 3
def build(self):
self.icon = 'images/flappyicon.png'
music = sfx_music.play()
top = Widget()
top.add_widget(Menu())
return Menu()
print Window.size
return music
if __name__ == "__main__":
FlappyBird().run()
这是我点击窗口右上角并显示其位置的屏幕截图。 Y轴似乎是倒置的,窗口尺寸现在也增加了一倍?
我也遇到了kivy 1.9.2没有为我的标签阅读Kv语言的问题,这就是我在main.py文件中添加了所有args的原因
任何帮助将不胜感激,
谢谢!