12. Python Game Development in Hindi - Avoid Hard Coding
import pygame
import time
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()
gameClose = False
block = 15
FPS = 20
start_x = game_width/2
start_y = game_height/2
update_x = 0
update_y = 0
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/2.4,game_height/2])
background_image = pygame.image.load("background.jpg").convert()
while not gameClose:
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:
gameClose = True
start_x += update_x
start_y += update_y
gameWindow.blit(background_image,[0,0])
pygame.draw.rect(gameWindow,blue,[start_x,start_y,block,block])
pygame.display.update()
clk.tick(FPS)
message_to_screen("You loose!!!!!",red)
pygame.display.update()
time.sleep(5)
pygame.quit()
quit()
Youtube :
0 Comments