Wednesday, June 1, 2022
HomeGame DevelopmentAttempting to maneuver the snake easily in a 2D sport

Attempting to maneuver the snake easily in a 2D sport


import pygame
from sys import exit 
import random


pygame.init()

#Variables for the snake 
playerx=580
playery=400
width=10
peak=10
white=(255,255,255)
rising= False

#variabels for the ball
ball_width=110
ball_height=500

clock = pygame.time.Clock()

whereas True:
    for occasion in pygame.occasion.get():
        if occasion.sort == pygame.QUIT:
            pygame.stop()
            exit()
            
            
    #Display screen Particulars
    display=pygame.show.set_mode((1200,800))
    ball=pygame.draw.circle(display,white,(ball_width,ball_height),3)
    pygame.draw.rect(display,(255,0,0),(10,0,1160,800),1)
    snake=pygame.draw.rect(display,white,(playerx,playery,width,peak),0)
    pygame.show.set_caption("Snake Sport")
    
    
    #Keyboard enter
    key_pressed=pygame.key.get_pressed()        

    if key_pressed[pygame.K_UP]:
        playery-=1
    if key_pressed[pygame.K_DOWN]:
        playery+=1
    if key_pressed[pygame.K_LEFT]:
        playerx-=1
    if key_pressed[pygame.K_RIGHT]:
        playerx+=1
    
    #Limiting the snake within the field on x measurement
    if playerx >1160:
        pygame.stop()
    if playerx < 0:
        pygame.stop()
        
    #Limiting the snake in field on y measurement
    if playery >800:
        pygame.stop()
    if playery<0:
        pygame.stop()
    
    #Colliding with the ball 
    if snake.colliderect(ball):
        width+=5
        ball_width=random.selection(vary(0,1160))
        ball_height=random.selection(vary(0,700))
    
    
        
    
        
    
    
    pygame.show.replace()
    clock.tick(90)

‘Hi there ! I wrote this code to create a snake sport. For now it isn’t completed but . The largest drawback is that I dont know the right way to transfer the snake easily and by that I imply to maneuver the snake simply by 1 block . Now once I transfer the snake it strikes with the entire physique . ‘

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments