Hello, this is Yuki (@engineerblog_Yu), a student engineer.
This article is about how to create a simple application using Python with just copy and paste.
If you are new to programming and can’t write code to create an application, this article is for you.
I introduced a method to create an application just by copying and pasting code for those who are new to programming and can’t write code to create an application.
Installation of kivy
To install Kivy, go to this website and install it according to your environment.
Go to User’s Guide and go to Installation to install.
How to play Kivy (ping pong game)
Let’s start by creating a pong game.
First, you can create a pong game by going to the Pong Game Tutorial from Tutorial on kivy.
Copy and paste the main.py and pong.kv files at the bottom of the tutorial into your Python runtime environment.
In my case, I used a Python execution environment called Pycharm.
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import NumericProperty, ReferenceListProperty,\
ObjectProperty
from kivy.vector import Vector
from kivy.clock import Clock
from random import randint
class PongBall(Widget):
velocity_x = NumericProperty(0)
velocity_y = NumericProperty(0)
velocity = ReferenceListProperty(velocity_x, velocity_y)
def move(self):
self.pos = Vector(*self.velocity) + self.pos
class PongGame(Widget):
ball = ObjectProperty(None)
def serve_ball(self):
self.ball.center = self.center
self.ball.velocity = Vector(4, 0).rotate(randint(0, 360))
def update(self, dt):
self.ball.move()
# bounce off top and bottom
if (self.ball.y < 0) or (self.ball.top > self.height):
self.ball.velocity_y *= -1
# bounce off left and right
if (self.ball.x < 0) or (self.ball.right > self.width):
self.ball.velocity_x *= -1
class PongApp(App):
def build(self):
game = PongGame()
game.serve_ball()
Clock.schedule_interval(game.update, 1.0 / 60.0)
return game
if __name__ == '__main__':
PongApp().run()
#:kivy 1.0.9
<PongBall>:
size: 50, 50
canvas:
Ellipse:
pos: self.pos
size: self.size
<PongGame>:
ball: pong_ball
canvas:
Rectangle:
pos: self.center_x-5, 0
size: 10, self.height
Label:
font_size: 70
center_x: root.width / 4
top: root.top - 50
text: "0"
Label:
font_size: 70
center_x: root.width * 3 / 4
top: root.top - 50
text: "0"
PongBall:
id: pong_ball
center: self.parent.center
Create these two files on the same directory.
python main.py
You can create a ping pong game by running it on the terminal.

How to play Kivy (drawing game)
Next, let’s create a drawing game in the same way.
As with the ping pong game, go to A Simple Paint App from Tutorial and create a drawing game as well.
Copy and paste the code described there into main.py.
from random import random
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.graphics import Color, Ellipse, Line
class MyPaintWidget(Widget):
def on_touch_down(self, touch):
color = (random(), 1, 1)
with self.canvas:
Color(*color, mode='hsv')
d = 30.
Ellipse(pos=(touch.x - d / 2, touch.y - d / 2), size=(d, d))
touch.ud['line'] = Line(points=(touch.x, touch.y))
def on_touch_move(self, touch):
touch.ud['line'].points += [touch.x, touch.y]
class MyPaintApp(App):
def build(self):
parent = Widget()
self.painter = MyPaintWidget()
clearbtn = Button(text='Clear')
clearbtn.bind(on_release=self.clear_canvas)
parent.add_widget(self.painter)
parent.add_widget(clearbtn)
return parent
def clear_canvas(self, obj):
self.painter.canvas.clear()
if __name__ == '__main__':
MyPaintApp().run()
python main.py

How was it?
I hope you have realized that it is possible to create such a game with programming, although it is only copy and paste.
If you are interested, you can take a course on Udemy.
コメント