Last active
August 23, 2018 16:01
-
-
Save linnil1/cb8ecbac8702c38353f871644f6dc61f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from kivy.app import App | |
from kivy.lang.builder import Builder | |
from kivy.uix.widget import Widget | |
from kivy.uix.floatlayout import FloatLayout | |
from kivy.clock import Clock | |
from kivy.properties import ObjectProperty, NumericProperty, ListProperty | |
from kivy.core.window import Window | |
# config Start # | |
window_size = (800, 600) | |
update_interval = 1. / 60 | |
my_brick = [(20 + i * 100, 600 - 20 - 60) for i in range(8)] | |
move_speed = 10 | |
kv_string = """ | |
<Brick>: | |
size: (90, 20) | |
canvas: | |
Color: | |
rgb: (1, 1, 1) | |
Rectangle: | |
pos: self.pos | |
size: self.size | |
<Ball>: | |
size: (20, 20) | |
canvas: | |
Color: | |
rgb: (0, 1, 0) | |
Ellipse: | |
pos: self.pos | |
size: self.size | |
<Paddle>: | |
size: (120, 20) | |
canvas: | |
Color: | |
rgb: (1, 0, 0) | |
Rectangle: | |
pos: self.pos | |
size: self.size | |
<Game>: | |
ball: my_ball | |
paddle: my_paddle | |
Ball: | |
id: my_ball | |
pos: (self.parent.center[0], 40) | |
Paddle: | |
id: my_paddle | |
pos: (self.parent.center[0], 20) | |
Label: | |
font_size: 70 | |
right: root.width - 20 | |
bottom: 20 | |
text: str(root.score) | |
""" | |
# config end # | |
class Paddle(Widget): | |
pass | |
class Brick(Widget): | |
def __init__(self, x, y, **kwargs): | |
super(Brick, self).__init__(**kwargs) | |
self.pos = (x, y) | |
class Ball(Widget): | |
vx = NumericProperty(7) | |
vy = NumericProperty(3) | |
def isTouch(self, pos, size): | |
# left up right down | |
return pos[0] + size[0] >= self.pos[0] and \ | |
pos[1] + size[1] >= self.pos[1] and \ | |
pos[0] <= self.pos[0] + self.size[0] and \ | |
pos[1] <= self.pos[1] + self.size[1] | |
def changeDir(self, pos, size): | |
if self.vx > 0 and pos[0] > self.pos[0]: | |
self.vx *= -1 | |
elif self.vx < 0 and pos[0] + size[0] < self.pos[0] + self.size[0]: | |
self.vx *= -1 | |
if self.vy > 0 and pos[1] > self.pos[1]: | |
self.vy *= -1 | |
elif self.vy < 0 and pos[1] + size[1] < self.pos[1] + self.size[1]: | |
self.vy *= -1 | |
class Game(Widget): | |
ball = ObjectProperty(None) | |
paddle = ObjectProperty(None) | |
bricks = ListProperty([]) | |
score = NumericProperty(0) | |
# keyboard | |
def __init__(self, **kwargs): | |
super(Game, self).__init__(**kwargs) | |
self._keyboard = Window.request_keyboard(self._keyboard_closed, self) | |
self._keyboard.bind(on_key_down=self._on_keyboard_down) | |
def _keyboard_closed(self): | |
self._keyboard.unbind(on_key_down=self._on_keyboard_down) | |
self._keyboard = None | |
def _on_keyboard_down(self, keyboard, keycode, text, modifiers): | |
if keycode[1] == 'left': | |
self.paddle.pos[0] -= move_speed | |
elif keycode[1] == 'right': | |
self.paddle.pos[0] += move_speed | |
return True | |
# add bricks | |
def initBrick(self): | |
parent_size = Window.size | |
for b in my_brick: | |
brick = Brick(*b) | |
self.bricks.append(brick) | |
self.add_widget(brick) | |
def update(self, interval): | |
self.ball.pos = (self.ball.pos[0] + self.ball.vx, | |
self.ball.pos[1] + self.ball.vy) | |
# wall | |
wall_border = self.parent.size | |
pos, size = self.ball.pos, self.ball.size | |
if pos[0] + size[0] >= wall_border[0] or \ | |
pos[0] <= 0: | |
self.ball.vx *= -1 | |
if pos[1] + size[1] >= wall_border[1] or \ | |
pos[1] <= 0: | |
self.ball.vy *= -1 | |
# paddle | |
if self.ball.isTouch(self.paddle.pos, self.paddle.size): | |
self.ball.changeDir(self.paddle.pos, self.paddle.size) | |
for brick in list(self.bricks): | |
if self.ball.isTouch(brick.pos, brick.size): | |
self.ball.changeDir(brick.pos, brick.size) | |
self.score += 1 | |
self.bricks.remove(brick) | |
self.remove_widget(brick) | |
if not len(self.bricks): | |
self.initBrick() | |
class GameApp(App): | |
def build(self): | |
Builder.load_string(kv_string) | |
game = Game() | |
game.initBrick() | |
Clock.schedule_interval(game.update, update_interval) | |
Window.size = window_size | |
return game | |
if __name__ == '__main__': | |
GameApp().run() |
Author
linnil1
commented
Aug 23, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment