jeudi 5 juillet 2018

snake game in python

I wrote the whole code for the first level of the game(you control the snake by up,down,right and left botton to eat the apple and the snake's length grows) but I have no idea about how I write the second level (in which there is two snakes that you control one of them like I said and computer(random) controls the other one and there are some walls as barriers;I want it to be like it goes to the next level when you hit the 5th score in level 1) could someone please complete this project?! Here's my code:

`

import pygame
import random
pygame.init()

#colors
black=(0,0,0)
crismon=(220,20,60)
white=(255,255,255)
vanila=(255,250,226)
pink=(255,204,255)

display_width=700
display_height=500

gameDisplay=pygame.display.set_mode((display_width,display_height))
#caption0
pygame.display.set_caption("Snake")

#icon
icon=pygame.image.load('the picture's path')
pygame.display.set_icon(icon)

clock=pygame.time.Clock()


block_size=10
#frames per second
fps=25

#font
font=pygame.font.SysFont(None,25)

#score
def score(score):
    text=font.render("Score:"+str(score),True,crismon)
    gameDisplay.blit(text,[0,0])

def message_to_screen(msg,color,y_displace=0):
    textSurf=font.render(msg,True,color)
    textRect=textSurf.get_rect()
    textRect.center=(display_width/2),(display_height/2)+y_displace
    gameDisplay.blit(textSurf,textRect)

def snake(block_size,snakeList):
    for XnY in snakeList:
        pygame.draw.rect(gameDisplay,crismon,[XnY[0],XnY[1],block_size,block_size])

def game_intro():
    intro=True
    while intro:
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                pygame.quit()
                quit()
            if event.type==pygame.KEYDOWN:
                if event.key==pygame.K_p:
                    intro=False
                if event.type==pygame.K_q:
                    pygame.quit()
                    quit()


        gameDisplay.fill(crismon)
        message_to_screen("Welcome to Snake",white,-70)
        message_to_screen("Eat the Apple!",white,-40)
        message_to_screen("Press P to play or Q to quit!",white,-10)
        pygame.display.update()

def gameLoop():
    gameExit=True

    lead_x=display_width/2
    lead_y=display_height/2

    lead_x_change=0
    lead_y_change=0

    snakeList=[]
    snakeLength=10

    randAppleX=round(random.randrange(0,display_width-block_size)/10.0)*10.0
    randAppleY=round(random.randrange(0,display_height-block_size)/10.0)*10.0

    while gameExit:
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                pygame.quit()
                quit()
            if event.type==pygame.KEYDOWN:
                if event.key==pygame.K_RIGHT:
                    lead_x_change=block_size
                    lead_y_change=0
                elif event.key==pygame.K_LEFT:
                    lead_x_change=-block_size
                    lead_y_change=0
                elif event.key==pygame.K_UP:
                    lead_y_change=-block_size
                    lead_x_change=0
                elif event.key==pygame.K_DOWN:
                    lead_y_change=block_size
                    lead_x_change=0
                elif event.key==pygame.K_q:
                    pygame.quit()
                    quit()

        if lead_x>=display_width or lead_x<0 or lead_y>=display_height or lead_y<0:
            pygame.quit()
            quit()

        lead_x+=lead_x_change
        lead_y+=lead_y_change

        gameDisplay.fill(pink)
        pygame.draw.rect(gameDisplay,black,[randAppleX,randAppleY,block_size,block_size])

        snakeHead=[]
        snakeHead.append(lead_x)
        snakeHead.append(lead_y)
        snakeList.append(snakeHead)

        if len(snakeList)>snakeLength:
            del snakeList[0]

        snake(block_size,snakeList)

        score(snakeLength-10)

        pygame.display.update()

        if lead_x==randAppleX and lead_y==randAppleY:
            pygame.mixer.music.load('the music's path')
            pygame.mixer.music.play(1)
            randAppleX=round(random.randrange(0,display_width-block_size)/10.0)*10.0
            randAppleY=round(random.randrange(0,display_height-block_size)/10.0)*10.0
            snakeLength+=1

        clock.tick(fps)

    pygame.quit()
    quit()

game_intro()
gameLoop()

`




Aucun commentaire:

Enregistrer un commentaire