Skip to main content

How to make calculator in python kivy

  How make calculator in python We make a calculator in python using kivy module . Kivy module helps us to make android apps . First wait , If you run a kivy program in your terminal then you see a error like this To solve this problem you have to go kivy docs and follow the instruction of kivy docs.  First we have to make a virtual environment and then in the virtual environment you have to install kivy . After that you have to write  kivy_venv\Scripts\activate in your terminal and then run the file . If this method not work then write the comment below. LETS START CODING First we import kivy .For this we hve to write  from  kivy.app  import  App from  kivy.uix.label  import  Label from  kivy.uix.button  import  Button from  kivy.core.window  import  Window from  kivy.uix.gridlayout  import  GridLayout from  kivy.uix.widget  import  Widget from  kivy.lang  import ...

How to make snake game in python

 How to make snake game in python 

We make snake game in python with help of pygame module. pygame module helps in making games in python . 

LETS START CODING 

Import pygame and random

we have to import pygame and random module for making snake game. Random module helps to generate random position for food in the screen. for this we have to write 

import pygame
import random

Creating game window

First we have to make game screen with the help of pygame. For this we have to write 

screen_widht=460
screen_height=600

gameWindow=pygame.display.set_mode((screen_widht,screen_height))
pygame.display.set_caption("SNAKE GAME")
pygame.display.update()

Creating welcome screen 

First we have to display welcome screen. For display screen we have to make a function . For this we have to write def welcome():

    exit_game=False
    while not exit_game:
        gameWindow.fill((233,223,223))
        # gameWindow.blit(home,(0,0))
        textscreen("WELCOME TO SNAKES",red,33,33)
        textscreen("PESS SPACE TO PLAY",red,33,66)
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                exit_game=True
            if event.type==pygame.KEYDOWN:
                if event.key==pygame.K_SPACE:
                    gameloop()
        pygame.display.update()
        clock.tick(60)

Creating snake 

After that we make a function which take gameWindow , color , snake length , snake width and plot the snake .For this we have to write 


def plotsnake(gameWindow,color,snk_list,snake_size):
    for x,y in snk_list:
        pygame.draw.rect(gameWindow,color,[x,y,snake_size,snake_size])

NOTE - we take snk_list to increase the length of snake .

Moving the snake 

After making the snake we have to move the snake . For this we use key event in the KEYDOWN class which is in pygame. We use K_UP , K_DOWN,  K_LEFT , K_RIGHT to snake move up , down , left and right. After that we create   velocity_x=0, velocity_y=0initvelocity=5

For make snake move we have to write 

 for event in pygame.event.get():
                      
                if event.type==pygame.QUIT:
                    exit_game=True
                if event.type==pygame.KEYDOWN:
                    if event.key==pygame.K_RIGHT:
                        velocity_x=initvelocity
                        velocity_y=0
                    if event.key==pygame.K_LEFT:
                        velocity_x=-initvelocity
                        velocity_y=0
                    if event.key==pygame.K_DOWN:
                        velocity_y=initvelocity
                        velocity_x=0
                    if event.key==pygame.K_UP:
                        velocity_y=-initvelocity
                        velocity_x=0


            snake_x = snake_x + velocity_x
            snake_y = snake_y + velocity_y

Game over when snake hit the boundaries and itself 

When snake hit the boundary and itself then game over and game over screen open and print the score . For this we have to write  

if head in snk_list[:-1]:
                game_over=True
            if snake_x<0 or snake_x>screen_widht or snake_y<0 or snake_y>screen_height:
                game_over=True

After game over a game over screen open where score are print 

Adding food and score

For adding food we have to plot a rectangle in game screen. For this we have to write 

 pygame.draw.rect(gameWindow,foodcolor,[food_x,food_y,snake_size,snake_size])

When snake eat the food then add 10 score and also add snk_lenght which used to increase the size of snake . For this we have to write 

   if abs(snake_x-food_x)<10 and abs(snake_y-food_y)<10:
                score += 10
                food_x=random.randint(100,screen_widht-100)
                food_y=random.randint(100,screen_height-100)
                snakecolor=foodcolor
                foodcolor=(random.randint(0,255),random.randint(0,255),random.randint(0,255))
                
                snk_lenght+=5

Increasing the length of snake 

To increase the length of snake, we have to make a list where then append position of snake in each frame in snk_list list . After that when snk_list more then snk_lenght , Then we delete the last element of list . For this we have to write

 head.append(snake_x)
            head.append(snake_y)
            snk_list.append(head)
            if len(snk_list)>snk_lenght:
                del snk_list[0]

 Game over screen

When snake hit the boundary and itself then game over window open and display the score . For this we have to write gameWindow.fill(white)

           
            textscreen("GAME OVER",(255,55,55),5,5)
            textscreen("PESS ENTER TO PLAY",(125,255,255),5,55)
            textscreen("YOUR SCORE:"+str(score),(255,211,233),5,99)
            for event in pygame.event.get():
                if event.type==pygame.QUIT:
                    exit_game=True
                if event.type==pygame.KEYDOWN:
                    if event.key==pygame.K_RETURN:
                        welcome()

Here text screen is a function . We use text screen function to display the text. For this we have to write 


def textscreen(text,color,x,y):
    screen_text=font.render(text,True,color)
    gameWindow.blit(screen_text,[x,y])

HERE ARE THE CODE 



OUTPUT