STAY WITH US

15. Python Game Development in Hindi - Adding Apple


15. Python Game Development in Hindi - Adding Apple


Code :


import pygame

import time

import random

pygame.init()

game_width = 800
game_height = 600
white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)

gameWindow = pygame.display.set_mode((game_width,game_height))
pygame.display.set_caption('Snake Game')

pygame.display.update()

block = 20
FPS = 13


clk = pygame.time.Clock()

font = pygame.font.SysFont(None,30)

def message_to_screen(msg,color):
    screen_text = font.render(msg,True,color)
    gameWindow.blit(screen_text,[game_width/4,game_height/2])

background_image = pygame.image.load("background.jpg").convert()

def loop():
    gameClose = False
    gameOver = False

    rAppleX = round(random.randrange(0,game_width-block)/20.0)*20.0
    rAppleY = round(random.randrange(0,game_height-block)/20.0)*20.0

    start_x = game_width / 2
    start_y = game_height / 2
    update_x = 0
    update_y = 0
    while not gameClose:
        while gameOver == True:
            gameWindow.fill(white)
            message_to_screen("You loose!!!, Press 'r' to Replay or Press 'q' to Quit", red)
            pygame.display.update()
            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_q:
                        gameClose = True
                        gameOver = False
                    if event.key == pygame.K_r:
                        loop()

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameClose = True

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    update_x = -block
                    update_y = 0

                if event.key == pygame.K_RIGHT:
                    update_x = +block
                    update_y = 0

                if event.key == pygame.K_UP:
                    update_y = -block
                    update_x = 0

                if event.key == pygame.K_DOWN:
                    update_y = +block
                    update_x = 0


        if start_x >= game_width or start_x < 0 or start_y >= game_height or start_y < 0:

            gameOver = True

        start_x += update_x
        start_y += update_y
        gameWindow.blit(background_image,[0,0])
        pygame.draw.rect(gameWindow, green, [rAppleX, rAppleY, block, block])
        pygame.draw.rect(gameWindow,blue,[start_x,start_y,block,block])
        pygame.display.update()

        if start_x == rAppleX and start_y == rAppleY:
            print("Main ne Apple kha liya hai")
        clk.tick(FPS)



    pygame.quit()

    quit()

loop()

YouTube : 


Post a Comment

0 Comments