11. Python Game Development in Hindi - Detecting Boundaries
Code :
import pygame
pygame.init()
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((800,600))
pygame.display.set_caption('Snake Game')
pygame.display.update()
gameClose = False
start_x = 400
start_y = 300
update_x = 0
update_y = 0
clk = pygame.time.Clock()
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 = -15
update_y = 0
if event.key == pygame.K_RIGHT:
update_x = +15
update_y = 0
if event.key == pygame.K_UP:
update_y = -15
update_x = 0
if event.key == pygame.K_DOWN:
update_y = +15
update_x = 0
if start_x >= 800 or start_x < 0 or start_y >= 600 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,15,15])
pygame.display.update()
clk.tick(13)
pygame.quit()
quit()
YouTube :
0 Comments