#:kivy 1.7.2
<Game>:
orientation: 'vertical'
ScoreExit:
id: sce
size: root.width, 30
pos: 0, root.height - 30
GameSpace:
id: gamespace
size: root.width, root.height - 30
pos: 0, 0
# Note - in order to have access to the Paddle id mypad, need to define
# GameSpace separately below and not nest the ref to Paddle here
<ScoreExit>:
orientation: 'horizontal'
Label:
id: lab
size_hint_x: 0.45
font_size: 30
center_x: root.width / 3
text: "Score: " + str(root.score)
Label:
id: liv
size_hint_x: 0.45
font_size: 30
center_x: root.width * 2/3
text: "Lives: " + str(root.lives)
Button:
id: scebut
size_hint_x: 0.1
text: 'Exit'
font_size: 30
on_press: root.sce_exit
<GameOver>:
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
Button:
text: 'No more lives!\nPress to play again'
<GameSpace>:
Paddle:
id: mypad
size_hint: 0.1, None # Note the None is needed to use height below
# Don't set pos_hint here as it will stop us moving the paddle
Ball:
id: ball
size_hint: 0.02, None
TileArea:
id: tilearea
size_hint: 1, 0.3
pos_hint: {'top': 1}
<Paddle>:
height: self.width/3
canvas:
Color:
rgba: 1, 1, 0.2, 0.8
Rectangle:
size: self.size
pos: self.pos
<Ball>:
height: self.width
canvas:
Color:
rgba: 0, 1, 0.7, 0.8
Ellipse:
pos: self.pos
size: self.size
<Tile>:
canvas:
Rectangle:
id: rect
size: self.size
pos: self.pos
#!/usr/bin/python
__version__ = "0.1"
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.properties import NumericProperty, ReferenceListProperty, StringProperty, ObjectProperty, ListProperty
from kivy.core.window import Window
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.gridlayout import GridLayout
from kivy.graphics import Line, Rectangle, Color, Mesh
from kivy.clock import Clock
from kivy.vector import Vector
from kivy.config import Config
from random import random, choice, uniform, randint
Config.set('graphics', 'multisamples', '0')
class CollidableObject(Widget):
def set_corners(self):
# print "Set cors: ", self.size
self.corners = []
self.corners.append(Vector(self.x, self.y))
self.corners.append(Vector(self.right, self.y))
self.corners.append(Vector(self.right, self.top))
self.corners.append(Vector(self.x, self.top))
self.corners.append(self.corners[0])
# print "Corners: ", self.corners
class Paddle(CollidableObject):
pass
class Ball(Widget):
pass
class TileArea(FloatLayout):
def __init__(self, **kwargs):
super(TileArea, self).__init__(**kwargs)
self.bind(on_size=self.show_tilearea(dt = 'Bind'))
def setup_tiles(self):
print "Setup tiles"
print "Self ", self.size
numrows = 5
numcols = 10
for m in range(numrows):
for n in range(numcols):
newt = (Tile(size_hint = (float(1)/numcols, float(1)/numrows),
pos_hint={'x': float(n)/numcols, 'y': float(m)/numrows}))
self.add_widget(newt)
# print "newt size: ", newt.size
# print "newt pos: ", newt.pos
# newt.set_corners()
def show_tilearea(self, dt):
print "%s TA size: %s"% (dt, self.size)
for child in self.children:
print "Tile size: ", child.size
# print "Tile corners: ", child.corners
break
class Tile(CollidableObject):
col_list = ListProperty([])
def __init__(self, **kwargs):
super(Tile, self).__init__(**kwargs)
self.name = "Tile"
self.col = choice(Tile.col_list)
self.canvas.add(Color(self.col[0], self.col[1], self.col[2], self.col[3], mode = 'rgba'))
self.hits = 0
def initialise_colours(self):
col_list = []
trans = 0.8
col_list.append([1, 0.2, 0, trans]) # red
col_list.append([1, 1, 0.2, trans]) # yel
col_list.append([0.4, 1, 0.4, trans]) # green
col_list.append([0, 1, 0.8, trans]) # blue
Tile.col_list = col_list
class ScoreExit(BoxLayout):
def __init__(self, **kwargs):
super(ScoreExit, self).__init__(**kwargs)
self.score = 0
self.lives = 3
class GameOver(AnchorLayout):
pass
class Game(Widget):
def start(self):
self.ids.gamespace.reset_game()
class GameSpace(FloatLayout):
def __init__(self, **kwargs):
super(GameSpace, self).__init__(**kwargs)
self.initialise_colours()
# Absence of the time parameter means it processes at once
# The lambda dt allows us to call a function that doesn't take dt
# Clock.schedule_once(lambda dt: self.reset_game())
self.bind(on_size=self.show_gamespace(dt = 'Bind'))
def show_gamespace(self, dt):
print "%s GS size: %s" % (dt, self.size)
def initialise_colours(self):
col_list = []
trans = 0.8
col_list.append([1, 0.2, 0, trans]) # red
col_list.append([1, 1, 0.2, trans]) # yel
col_list.append([0.4, 1, 0.4, trans]) # green
col_list.append([0, 1, 0.8, trans]) # blue
Tile.col_list = col_list
class BreakOutApp(App):
def build(self):
Config.set('graphics', 'fullscreen', 1)
# Window.size = (400, 700) # Roughly proportion of phone
# Window.size = (800, 500) # Roughly proportion of hudl
wid = Game(size=Window.size)
print "Game size: ", wid.size
print "Gamespace size: ", wid.ids.gamespace.size
print "Tilearea size: ", wid.ids.gamespace.ids.tilearea.size
print "SCE size: ", wid.ids.sce.size
wid.ids.gamespace.ids.tilearea.setup_tiles()
# for child in wid.ids.gamespace.ids.tilearea.children:
# print "Child x: ", child.pos
# child.set_corners()
Clock.schedule_interval(wid.ids.gamespace.ids.tilearea.show_tilearea, 0.1)
# wid.start()
return wid
if __name__ == '__main__':
BreakOutApp().run()
Hi
Okay, line 105 needs to be:
self.bind(on_size=lambda dt: self.show_gamespace(dt='Bind'))
Similarly for line 43:
self.bind(on_size=lambda dt: self.show_tilearea(dt = 'Bind'))
Aside from that, the behavior is expected. You need to bind to size
and pos changes to accurately determine size and positions when using
hinting. You can do this here by adding this to the CollidableObject class
class CollidableObject(Widget):
def set_corners(self):
# print "Set cors: ", self.size
self.corners = []
self.corners.append(Vector(self.x, self.y))
self.corners.append(Vector(self.right, self.y))
self.corners.append(Vector(self.right, self.top))
self.corners.append(Vector(self.x, self.top))
self.corners.append(self.corners[0])
print "Corners: ", self.corners
def on_size(self, widget, value):
self.set_corners()
def on_pos(self, widget, value):
self.set_corners()
class TileArea(FloatLayout):
def __init__(self, **kwargs):
super(TileArea, self).__init__(**kwargs)
self.cou = 0
for x in range(10000000):
self.cou = x