Created
November 26, 2020 15:02
-
-
Save Ved-programmer/e19b91ac34b9046cb246d6e7ff88bd70 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pygame as pg | |
import time, random | |
pg.init() | |
white = [255, 255, 255];black = [0, 0, 0];red = [255, 0, 0];green = [0, 155, 0];blue = [0, 0, 255];grey = [0, 255, 255] #Colors follow the RGB pattern | |
displayWidth, displayHeight = 800, 600 #Change this to change the dimensions of the screen | |
gameDisplay = pg.display.set_mode((displayWidth, displayHeight)) | |
pg.display.set_caption("Slither") #Change the "Slither" to your wish | |
background = grey #Change the background color but make sure it is defined above. | |
gameDisplay.fill(background) | |
clock = pg.time.Clock() | |
pg.display.update() | |
def snake(snakeList, blockSize): | |
for XnY in snakeList: | |
pg.draw.rect(gameDisplay, blue, rect = [XnY[0], XnY[1], blockSize, blockSize]) | |
def msgToScreen(msg, color, size, positions): | |
if positions == "center":positions = [displayWidth // 2 - (len(msg) * size // 6), displayHeight // 2] | |
font = pg.font.SysFont(None, size) | |
text = font.render(msg, True, color) | |
gameDisplay.blit(text, positions) | |
def gameLoop(): | |
snakeLength = 5 #Change this to change the starting length of the snake | |
diff = 2;blockSize = 30 #Change blockSize to change to size of the snake and the apple | |
score = 0 #Change score to change the starting score | |
applePlacement = lambda : [random.randrange(blockSize*2, displayWidth - blockSize*2), random.randrange(blockSize*2, displayHeight - blockSize*2), blockSize*2//3, blockSize*2//3] | |
curApplePlace = applePlacement() | |
leadX = leadY = 200 #Change this to change the starting position of the snake | |
pg.draw.rect(gameDisplay, blue, rect = [leadX, leadY, blockSize, blockSize]);pg.display.update() | |
leadXChange = leadYChange = 0 | |
gameOn = True;gameOver = False | |
FPS = 150 #change FPS to change the speed | |
#NOTE:The higher the FPS the higher the CPU and RAM consumption | |
snakeList = [] | |
while gameOn: | |
for event in pg.event.get(): | |
if event.type == pg.QUIT:gameOn = False | |
elif event.type == pg.KEYDOWN: | |
if event.key == pg.K_LEFT: | |
leadXChange = -diff;leadYChange = 0 | |
if event.key == pg.K_RIGHT: | |
leadXChange = diff;leadYChange = 0 | |
if event.key == pg.K_UP: | |
leadYChange = -diff;leadXChange = 0 | |
if event.key == pg.K_DOWN: | |
leadYChange = diff;leadXChange = 0 | |
if event.key == pg.K_SPACE:gameOn = False | |
leadX += leadXChange;leadY += leadYChange | |
if leadX + blockSize >= displayWidth or leadX <= 0 or leadY + blockSize >= displayHeight or leadY <= 0:gameOver = True | |
gameDisplay.fill(background) | |
pg.draw.rect(gameDisplay, red, rect = curApplePlace) | |
snakeList.append([leadX, leadY]) | |
if len(snakeList) > snakeLength:del snakeList[0] | |
if snakeList.count(snakeList[-1]) != len(snakeList): | |
for element in snakeList[:-1]: | |
if element == snakeList[-1]: | |
gameOver = True | |
snake(snakeList, blockSize) | |
msgToScreen(f"Score: {score}", green, 30, [25, 25]) | |
pg.display.update() | |
if (leadX <= curApplePlace[0] <= leadX + blockSize and leadY <= curApplePlace[1] <= leadY + blockSize) or (leadX <= curApplePlace[0] + curApplePlace[2] <= leadX + blockSize and leadY <= curApplePlace[1] + curApplePlace[3] <= leadY + blockSize): | |
score += 1 | |
if snakeLength > 100:snakeLength += 25 | |
elif snakeLength > 500:snakeLength += 50 | |
elif snakeLength > 1500:snakeLength += 80 | |
if snakeLength == 2000:FPS = 200 | |
else:snakeLength += 10 | |
curApplePlace = applePlacement() | |
clock.tick(FPS) | |
while gameOver: | |
gameDisplay.fill(background) | |
msgToScreen(f'Your score was {score}. Press "C" to continue and "Q" to quit', red, 40, "center") | |
pg.display.update() | |
for event in pg.event.get(): | |
if event.type == pg.QUIT:gameOn = gameOver = False | |
elif event.type == pg.KEYDOWN: | |
if event.key == pg.K_c: | |
gameLoop();gameOn = gameOver = False | |
elif event.key == pg.K_q:gameOn = gameOver = False | |
gameLoop() | |
pg.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment