Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame, sys, random, math, time
- from pygame.locals import *
- #defines some key variables
- cell_x_coord = list()
- cell_y_coord = list()
- x_coord = 0
- y_coord = 0
- counter = 0
- left = bool()
- right = bool()
- upwards = bool()
- downwards = bool()
- x_coord_for_food = list()
- y_coord_for_food = list()
- x_coord_list = list()
- y_coord_list = list()
- pygame.init()
- #defining variables for the width and length of screen
- LENGTH = 500
- WIDTH = 500
- FPS = 1
- #this makes the screen appear
- DISPLAYSURF = pygame.display.set_mode((WIDTH,LENGTH), 0, 32)
- #the class to split up the screen (refer to line 84 for more information)
- class split_up:
- def __init__(self):
- t = 10
- WIDTH = 490
- self.width_split_list = []
- WIDTH = int(WIDTH/10)
- for i in range(WIDTH):
- self.width_split_list.append(t)
- t+=10
- self.length_split_list = []
- j = 10
- LENGTH = 490
- LENGTH = int(LENGTH/10)
- for i in range((LENGTH)):
- self.length_split_list.append(j)
- j+=10
- return
- #defines the csolors
- WHITE = (255,255,255)
- RED = (255,0,0)
- BLACK = (0,0,0)
- FOODCOLOR = (180, 159, 7)
- #pygame.draw.rect(foo) is complicated and ineficiant, therefore this function creates the snake
- def draw_square_snake(area,x_coord,y_coord,COLOR):
- area = math.sqrt(area)
- pygame.draw.rect(DISPLAYSURF, COLOR, (x_coord, y_coord, area, area))
- return
- #this function draws black for the ares where the snake used to be, and the ares where the snake ate the food
- def draw_square_background(area, x_coord, y_coord, COLOR):
- area = math.sqrt(area)
- pygame.draw.rect(DISPLAYSURF, COLOR, (x_coord, y_coord, area , area))
- return
- #this class makes the food
- class make_food:
- def __init__(self, area,COLOR):
- self.color = COLOR
- self.area = math.sqrt(area)
- split_up_object = split_up()
- self.random_width = random.choice(split_up_object.width_split_list)
- self.random_length = random.choice(split_up_object.length_split_list)
- pygame.draw.rect(DISPLAYSURF, self.color, (self.random_width, self.random_length, 10,10))
- return
- def main():
- #defining varaibles for the loop
- area = 100
- clock = pygame.time.Clock()
- count = 0
- counter = 0
- left = bool()
- right = bool()
- upwards = bool()
- downwards = bool()
- x_coord = 20
- y_coord = 20
- eat_counter = 0
- eaten = bool()
- cell_x_coord = list()
- cell_y_coord = list()
- speed = 10
- var = 10
- food_eaten = bool()
- cell_count = 0
- #splits up the screen into squares of 10 (area = math.sqrt(WIDTH)/ math.sqrt(LENGTH))
- split_up_object = split_up()
- random_width = random.choice(split_up_object.width_split_list)
- random_length = random.choice(split_up_object.length_split_list)
- timer_event = pygame.USEREVENT+1
- pygame.time.set_timer(timer_event, 250)
- while True:
- #these 'try' statements will do things, if it is the first iteration of the loop, it will pass
- try:
- draw_square_background(500, cell_x_coord[0], cell_y_coord[0], BLACK)
- except IndexError:
- time.sleep(0.0000000000000001)
- try:
- del cell_x_coord[0]
- del cell_y_coord[0]
- except IndexError:
- time.sleep(0.0000000000000001)
- #this will set the FPS to 30
- clock.tick(30)
- #the only things that will happen in this event handler is if the user wants to quit, the program will end
- for event in pygame.event.get():
- if event.type == QUIT:
- pygame.quit()
- sys.exit()
- pygame.display.update()
- #these will check if the user has pressed any of the arrow keys, any other event will be sent to the event handler, (above this comment) and since the only algorithm in that is the QUIT, nothing will happen
- if pygame.key.get_pressed()[pygame.K_UP] == True:
- upwards = True
- downwards = False
- left = False
- right = False
- elif pygame.key.get_pressed()[pygame.K_DOWN] == True:
- downwards = True
- upwards = False
- left = False
- right = False
- elif pygame.key.get_pressed()[pygame.K_LEFT] == True:
- left = True
- upwards = False
- downwards = False
- right = False
- elif pygame.key.get_pressed()[pygame.K_RIGHT] == True:
- right = True
- left = False
- downwards = False
- upwards = False
- #passes if none of the arrow keys are pressed
- else:
- pass
- #the reason why the x_coord and y_coord don't go higher in the first statements is because the program needs to continue even if no keys are pressed, therefore they have their own seperate statements
- if upwards == False and downwards == False and right == False and left == False:
- x_coord+=0
- y_coord+=0
- if upwards == True:
- x_coord+=0
- y_coord-=speed
- elif downwards == True:
- x_coord+=0
- y_coord+=speed
- elif right == True:
- x_coord+=speed
- y_coord+=0
- elif left == True:
- x_coord-=speed
- y_coord+=0
- if x_coord >=int((WIDTH-19)):
- pygame.quit()
- sys.exit()
- elif x_coord <=-2:
- pygame.quit()
- sys.exit()
- elif y_coord >=int((LENGTH-19)):
- pygame.quit()
- sys.exit()
- elif y_coord<=-2:
- pygame.quit()
- sys.exit()
- #although this is only 2 lines, it could be the most important part of the program
- cell_x_coord.append(x_coord)
- cell_y_coord.append(y_coord)
- #draws the food if the loop has only been through one iteration
- if eat_counter == 0:
- area = math.sqrt(area)
- split_up_object = split_up()
- random_width = random.choice(split_up_object.width_split_list)
- random_length = random.choice(split_up_object.length_split_list)
- pygame.draw.rect(DISPLAYSURF, FOODCOLOR, (20, 20, 1,1))
- food_for_snake = make_food(10, FOODCOLOR)
- eat_counter+=1
- #length_x_coord=len(x_coord)
- #checks if the head of the snake is in the range of the food
- #print(len(x_coord))
- #print(len(cell_x_coord))
- if (((cell_x_coord[0] <= food_for_snake.random_width and cell_x_coord[0] >= (food_for_snake.random_width-var)) and (cell_y_coord[0] <= food_for_snake.random_length and cell_y_coord[0] >= (food_for_snake.random_length-var))) or ((cell_y_coord[0] <=food_for_snake.random_length and cell_y_coord[0] >= (food_for_snake.random_length-var)) and cell_x_coord[0] <= food_for_snake.random_width and cell_x_coord[0] >= (food_for_snake.random_width-var)) and food_eaten !=True):
- food_eaten = True
- last_in_list_x = int((len(cell_x_coord)-1))
- last_in_list_y = int((len(cell_y_coord)-1))
- last_element_x = cell_x_coord[last_in_list_x]
- last_element_y = cell_y_coord[last_in_list_y]
- for i in range((len(cell_x_coord))):
- draw_square_snake(500, cell_x_coord[i], cell_y_coord[i], RED)
- if food_eaten == 1:
- cell_x_coord.append((x_coord-1000))
- cell_y_coord.append((y_coord))
- food_for_snake = make_food(10, FOODCOLOR)
- food_eaten = False
- #print(cell_count)
- #cell_count+=1
- pygame.display.update()
- if __name__== '__main__':
- #executes the 'main' function
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement