create account

[Tutorial] Kivy App Development(Animating the first block)Part 2 by yalzeee

View this thread on: hive.blogpeakd.comecency.com
· @yalzeee ·
$7.66
[Tutorial] Kivy App Development(Animating the first block)Part 2
![images.png](https://cdn.steemitimages.com/DQmYqni9vxME8D1tzy4wZGgu8CT8zK2Goq5cwDaQia7Zg7J/images.png)
[image source](https://goo.gl/images/HbqzGY)
**Repository**: [Python, Open Source Repository](https://github.com/python)

**Software Requirements**: For software download python 3.0 compatible with your OS [here](https://www.python.org/downloads/)

**What you will learn**:
In this tutorial you will learn how to
-  Automating block generation using a loop in ``kivy`` for our tetris app
 
- Using the ``kivy.clock`` and ``kivy.window`` modules

- How to use ``kivy``s built in ``EventDispatcher`` to add properties to our super classes ``init`` function

**Difficulty** : Intermediate

**Tutorial**
**The topics covered in this tutorial are**:
**Creating block widgets**
**Making our widgets fall**
**Defining how our application will build**

In the last of this series we saw the basics for startup for a kivy applications visiting the basic layout and how to startup an application using kivy syntax. Today we will be going on to the development of the application starting out with its widgets.
As earlier stated it is advisable to divide files into graphics and logic for better assesment and easier coding. We will be building six blocks for the tetris game. Each block will have a different color and be drawn on a different ``canvas`` addreses as different widgets.

**Creating the block widgets**
We start by creating six new classes of widgets
```
#Create the classes and pass them
class Block1(Widget):
    pass
class Block2(Widget):
    pass
class Block3(Widget):
    pass
class Block4(Widget):
    pass
class Block5(Widget):
    pass
class Block6(Widget):
    pass
```
 You then link all these to consecutive kv styles in your ``.kv`` file
```
<Block1>:
	canvas:
	    Color:
		    rgb: 0,0,255
        Rectangle:
		    size: root.width/5, root.height/20 
			pos: self.pos
<Block2>:
    canvas:
	    Color:
		    rgba:1,0,0,1
		Rectangle:
		    size: root.width/10, root.height/20
			pos: self.pos
		
<Block3>:
    canvas:
	    Color:
		    rgba:1,0,0,1
		Rectangle:
		    size: root.width/10, root.height/20
			pos: self.pos

<Block4>:
    canvas:
	    Color:
		    rgba:1,0,0,1
		Rectangle:
		    size: root.width/10, root.height/20
			pos: self.pos

<Block5>:
    canvas:
	    Color:
		    rgba:1,0,0,1
		Rectangle:
		    size: root.width/10, root.height/20
			pos: self.pos

<Block6>:
    canvas:#A canvas is kivys default work area where things can be drawn
	    Color:#This is the color intruction in rgba
		    rgba:1,0,0,1
		Rectangle:
		    size: root.width/10, root.height/20
			pos: self.pos		
```
For now we have widgets but they wont show on our window if run because they have not been called in our app ``build`` function.
**Defining how our app will build**
This tetris app generates a random block out of the six gives it to our console at intervals. To do this we have to create a loop in our build function. First we set our Tetris game as our root layout and then add the widgets systematically using the loop. We write our code like this.
```
class TetrisApp(App):
    def build(self):#This is the root component of our app or rather where it loads
        game = TetrisGame()
        x = Window.width#Using our kivy.window module
        block1 = Block1(pos=(randint(0,x),Window.height))#We use this code to determine where the widget will load
        block2 = Block2(pos=(randint(0,x),Window.height))#We want it to load at a random x axis but on a constant y axis
        block3 = Block3(pos=(randint(0,x),Window.height))
        block4 = Block4(pos=(randint(0,x),Window.height))
        block5 = Block5(pos=(randint(0,x),Window.height))
        block6 = Block6(pos=(randint(0,x),Window.height))
        allblocks = (block1, block2, block3, block4, block5, block6)
        while game:#This is where we create our loop
            blockchoice = randint(1,6)
            if blockchoice == 1:
                game.add_widget(block1)
                Clock.schedule_interval(block1.fall,1/60)
                for i in allblocks:
                    if block1.collide_widget(i):
                        block1.position[1] = 0
            elif blockchoice == 2:
                game.add_widget(block2)
                Clock.schedule_interval(block2.fall,1/60)
                for i in allblocks:
                    if block2.collide_widget(i):
                        block2.position[1] = 0
            elif blockchoice == 3:
                game.add_widget(block3)
                Clock.schedule_interval(block3.fall,1/60)
                for i in allblocks:
                    if block3.collide_widget(i):
                        block3.position[1] = 0
            elif blockchoice == 4:
                game.add_widget(block4)
                Clock.schedule_interval(block4.fall,1/60)
                for i in allblocks:
                    if block4.collide_widget(i):
                        block4.position[1] = 0
            elif blockchoice == 5:
                game.add_widget(block5)
                Clock.schedule_interval(block5.fall,1/60)
                for i in allblocks:
                    if block5.collide_widget(i):
                        block5.position[1] = 0
            elif blockchoice == 6:
                game.add_widget(block6)
                Clock.schedule_interval(block6.fall,1/60)
                for i in allblocks:
                    if block6.collide_widget(i):
                        block6.position[1] = 0
            else:
                print('this is not possible')
        return game
```
To explain the ``Clock.schedule_interval`` function, we will have to define a fall function within each of our widget classes. The ``Clock.schedule_interval`` function keeps calling this function over a period of time. 
```
#In this case it call the fall function of our widget once every 60 seconds
Clock.schedule_interval(block.fall, 1/60)
```
**Making our blocks fall**
We then make the blocks fall and stop when they reach zero position.
```
#Repeat this piece of code for every block widget
class Block6(Widget):
    vel_x = NumericProperty(0)
    vel_y = NumericProperty(-5)
    position = ReferenceListProperty(vel_x,vel_y)
    def fall(self, pos):
        self.pos =  Vector(self.position) + self.pos
        if self.pos[1]==0:
            self.position[1] = 0
```
If you followed you should see your blocks falling one after the other and lumping on each other when they reach the end
![Screenshot (24).png](https://cdn.steemitimages.com/DQmWn2Yz4NXxK76zyrsL5eC8uJsMVjDfPre2m33YigQ1qMr/Screenshot%20(24).png)
This tutorial would continue in the next of its series

**For my previous posts in the python tutorial series**
-[Click Here* Creating a simple calculator using python 3.8(cpython)](https://steemit.com/utopian-io/@yalzeee/tutorial-developing-a-simple-calculator-with-basic-command-with-python-fix-bugs-in-your-code)

**Creating an encryption key(Cryptography)**
-[ Part 1](https://steemit.com/utopian-io/@yalzeee/tutorial-creating-a-visual-cube-encryption-key-using-python-3-0-part-1)
-[ Part 2](https://steemit.com/utopian-io/@yalzeee/tutorial-creating-a-visual-encryption-key-in-python-3-6-part-2)
-[ Part 3](https://steemit.com/utopian-io/@yalzeee/tutorial-creating-an-visual-cube-encryption-program-with-python-3-6-part-3)
-[ Part 4](https://steemit.com/utopian-io/@yalzeee/tutorial-creating-an-encryption-program-with-python-3-6-part-4)

**Developing an institutions data collection backend and frontend with python 3.6 series**
[Part 1 of this series](https://steemit.com/utopian-io/@yalzeee/tutorial-development-of-an-institutions-data-collection-backend-using-python-3-6-part-1)
[Part 2 of this series](https://steemit.com/utopian-io/@yalzeee/tutorial-development-of-an-institutions-data-collection-back-end-using-python-3-6-part-2)
[Part 3 of this series](https://steemit.com/utopian-io/@yalzeee/tutorial-development-of-an-institutions-data-collection-back-end-with-python-3-part-3)
[Part 4 of this series](https://steemit.com/utopian-io/@yalzeee/tutorial-developing-an-institutions-data-collection-web-front-end-with-python-3-6-part-4)
[Part 5 of this series](https://steemit.com/utopian-io/@yalzeee/tutorial-developing-an-institutions-data-collection-web-front-end-with-python-3-6-part-5)
[Part 6 of this series](https://steemit.com/utopian-io/@yalzeee/tutorial-developing-and-institutions-data-collection-web-front-end-and-back-end-with-python3-6-final-part6)

**Previous Series**
[Automating OnScreen Processes](https://steemit.com/utopian-io/@yalzeee/tutorial-navigation-and-system-interaction-automating-system-processes-in-python-development)
[Screen Mapping and Navigation](https://steemit.com/utopian-io/@yalzeee/tutorial-image-recognition-and-screen-mapping-in-python-developments-part-2)
[Packaging and Deploying](https://steemit.com/utopian-io/@yalzeee/tutorial-packaging-and-deploying-part-3)

**Proof of work done**
[Github](https://github.com/yalzeee/Python-tutorial-series/blob/master/Kivy%20Tetris%20App)
πŸ‘  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 72 others
properties (23)
authoryalzeee
permlinktutorial-kivy-app-development-animating-the-first-block-part-2
categoryutopian-io
json_metadata{"tags":["utopian-io","tutorials","programming","stach","wafrica"],"image":["https://cdn.steemitimages.com/DQmYqni9vxME8D1tzy4wZGgu8CT8zK2Goq5cwDaQia7Zg7J/images.png","https://cdn.steemitimages.com/DQmWn2Yz4NXxK76zyrsL5eC8uJsMVjDfPre2m33YigQ1qMr/Screenshot%20(24).png"],"links":["https://goo.gl/images/HbqzGY","https://github.com/python","https://www.python.org/downloads/","https://steemit.com/utopian-io/@yalzeee/tutorial-developing-a-simple-calculator-with-basic-command-with-python-fix-bugs-in-your-code","https://steemit.com/utopian-io/@yalzeee/tutorial-creating-a-visual-cube-encryption-key-using-python-3-0-part-1","https://steemit.com/utopian-io/@yalzeee/tutorial-creating-a-visual-encryption-key-in-python-3-6-part-2","https://steemit.com/utopian-io/@yalzeee/tutorial-creating-an-visual-cube-encryption-program-with-python-3-6-part-3","https://steemit.com/utopian-io/@yalzeee/tutorial-creating-an-encryption-program-with-python-3-6-part-4","https://steemit.com/utopian-io/@yalzeee/tutorial-development-of-an-institutions-data-collection-backend-using-python-3-6-part-1","https://steemit.com/utopian-io/@yalzeee/tutorial-development-of-an-institutions-data-collection-back-end-using-python-3-6-part-2","https://steemit.com/utopian-io/@yalzeee/tutorial-development-of-an-institutions-data-collection-back-end-with-python-3-part-3","https://steemit.com/utopian-io/@yalzeee/tutorial-developing-an-institutions-data-collection-web-front-end-with-python-3-6-part-4","https://steemit.com/utopian-io/@yalzeee/tutorial-developing-an-institutions-data-collection-web-front-end-with-python-3-6-part-5","https://steemit.com/utopian-io/@yalzeee/tutorial-developing-and-institutions-data-collection-web-front-end-and-back-end-with-python3-6-final-part6","https://steemit.com/utopian-io/@yalzeee/tutorial-navigation-and-system-interaction-automating-system-processes-in-python-development","https://steemit.com/utopian-io/@yalzeee/tutorial-image-recognition-and-screen-mapping-in-python-developments-part-2","https://steemit.com/utopian-io/@yalzeee/tutorial-packaging-and-deploying-part-3","https://github.com/yalzeee/Python-tutorial-series/blob/master/Kivy%20Tetris%20App"],"app":"steemit/0.1","format":"markdown"}
created2018-08-26 14:12:36
last_update2018-08-26 14:12:36
depth0
children5
last_payout2018-09-02 14:12:36
cashout_time1969-12-31 23:59:59
total_payout_value5.936 HBD
curator_payout_value1.728 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length9,022
author_reputation12,484,565,044,191
root_title"[Tutorial] Kivy App Development(Animating the first block)Part 2"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id69,414,266
net_rshares5,224,693,713,121
author_curate_reward""
vote details (136)
@portugalcoin ·
$4.59
Thanks for your contribution.
We've been reviewing your contribution and suggest the following to improve on upcoming tutorials.

- You use many blocks of code repeated, we suggest you put a function that will always use the same code is called.
The code repeated is this:
```
if blockchoice == 1:
    game.add_widget(block1)
    Clock.schedule_interval(block1.fall,1/60)
        for i in allblocks:
           if block1.collide_widget(i):
               block1.position[1] = 0
```
<br/>

- Your tutorial is quite short for a good tutorial.If your code didn't have code repetition the tutorial was very short. We recommend you aim for capturing at least 2-3 concepts in single tutorial.


Your contribution has been evaluated according to [Utopian policies and guidelines](https://join.utopian.io/guidelines), as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, [click here](https://review.utopian.io/result/8/31313434).

---- 
Need help? Write a ticket on https://support.utopian.io/. 
Chat with us on [Discord](https://discord.gg/uTyJkNm). 
[[utopian-moderator]](https://join.utopian.io/)
πŸ‘  , , , , , , , , ,
properties (23)
authorportugalcoin
permlinkre-yalzeee-tutorial-kivy-app-development-animating-the-first-block-part-2-20180826t204735840z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"links":["https://join.utopian.io/guidelines","https://review.utopian.io/result/8/31313434","https://support.utopian.io/","https://discord.gg/uTyJkNm","https://join.utopian.io/"],"app":"steemit/0.1"}
created2018-08-26 20:47:36
last_update2018-08-26 20:47:36
depth1
children2
last_payout2018-09-02 20:47:36
cashout_time1969-12-31 23:59:59
total_payout_value3.472 HBD
curator_payout_value1.121 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,179
author_reputation598,828,312,571,988
root_title"[Tutorial] Kivy App Development(Animating the first block)Part 2"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id69,441,400
net_rshares3,152,457,933,678
author_curate_reward""
vote details (10)
@utopian-io ·
$0.02
Thank you for your review, @portugalcoin!

So far this week you've reviewed 16 contributions. Keep up the good work!
πŸ‘  ,
properties (23)
authorutopian-io
permlinkre-re-yalzeee-tutorial-kivy-app-development-animating-the-first-block-part-2-20180826t204735840z-20180827t054508z
categoryutopian-io
json_metadata"{"app": "beem/0.19.42"}"
created2018-08-27 05:45:09
last_update2018-08-27 05:45:09
depth2
children0
last_payout2018-09-03 05:45:09
cashout_time1969-12-31 23:59:59
total_payout_value0.019 HBD
curator_payout_value0.004 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length116
author_reputation152,955,367,999,756
root_title"[Tutorial] Kivy App Development(Animating the first block)Part 2"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id69,471,094
net_rshares16,464,487,379
author_curate_reward""
vote details (2)
@yalzeee ·
Thanks for the tip
Prior to this i thought my code was rather quite lenghty, but in the next of my tutorial series i would try to encapsulate more
properties (22)
authoryalzeee
permlinkre-portugalcoin-re-yalzeee-tutorial-kivy-app-development-animating-the-first-block-part-2-20180826t232854227z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"app":"steemit/0.1"}
created2018-08-26 23:29:30
last_update2018-08-26 23:29:30
depth2
children0
last_payout2018-09-02 23:29:30
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length146
author_reputation12,484,565,044,191
root_title"[Tutorial] Kivy App Development(Animating the first block)Part 2"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id69,449,939
net_rshares0
@utopian-io ·
$0.02
Hey @yalzeee
 **Thanks for contributing on Utopian**.
We’re already looking forward to your next contribution!

**Want to chat? Join us on Discord https://discord.gg/h52nFrV.**

<a href='https://steemconnect.com/sign/account-witness-vote?witness=utopian-io&approve=1'>Vote for Utopian Witness!</a>
πŸ‘  ,
properties (23)
authorutopian-io
permlinkre-tutorial-kivy-app-development-animating-the-first-block-part-2-20180827t052533z
categoryutopian-io
json_metadata"{"app": "beem/0.19.42"}"
created2018-08-27 05:25:33
last_update2018-08-27 05:25:33
depth1
children0
last_payout2018-09-03 05:25:33
cashout_time1969-12-31 23:59:59
total_payout_value0.019 HBD
curator_payout_value0.004 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length297
author_reputation152,955,367,999,756
root_title"[Tutorial] Kivy App Development(Animating the first block)Part 2"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id69,469,832
net_rshares16,464,487,379
author_curate_reward""
vote details (2)
@wafrica ·
Hello! I find your post valuable for the wafrica community! Thanks for the great post! We encourage and support quality contents and projects  from the West African region.<br>Do you have a suggestion, concern or want to appear as a guest author on WAfrica, join our [discord server](https://discord.gg/AfFUhnC) and discuss with a member of our curation team.<br>Don't forget to join us every Sunday by 20:30GMT for our Sunday WAFRO party on our [discord channel](https://discord.gg/AfFUhnC). Thank you.
properties (22)
authorwafrica
permlinkre-tutorial-kivy-app-development-animating-the-first-block-part-2-20180826t143436
categoryutopian-io
json_metadata""
created2018-08-26 14:34:36
last_update2018-08-26 14:34:36
depth1
children0
last_payout2018-09-02 14:34:36
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length503
author_reputation38,945,611,432,157
root_title"[Tutorial] Kivy App Development(Animating the first block)Part 2"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id69,415,981
net_rshares0