From 5e4617a149765a6e5f36faa204b67231f269edf3 Mon Sep 17 00:00:00 2001 From: Cleverect <44272724+Cleverect@users.noreply.github.com> Date: Fri, 19 Oct 2018 12:22:34 +0530 Subject: [PATCH] Snake Game Using Turtle Graphics --- snake.py | 364 ++++++++++++++++++++++++++----------------------------- 1 file changed, 172 insertions(+), 192 deletions(-) diff --git a/snake.py b/snake.py index edb2c79..487cafc 100644 --- a/snake.py +++ b/snake.py @@ -1,195 +1,175 @@ -import curses -import time -import sys -from random import randint - -class Field: - def __init__(self, size): - self.size = size - self.icons = { - 0: ' . ', - 1: ' * ', - 2: ' # ', - 3: ' & ', - } - self.snake_coords = [] - self._generate_field() - self.add_entity() - - def add_entity(self): +import turtle +import random + + +head = [0] + +#score +a = [0] +b = [0] + +#food coord +foodcoord = [0,0,0] + +#cposition +cpos = [] + + +def home(x,y): + x = 0 + y = 0 + a[0] = 0 + b[0] = 0 + head[0] = 0 + foodcoord[2] = 0 + cpos[:] = [] + turtle.hideturtle() + turtle.clear() + turtle.pu() + turtle.color("red") + turtle.goto(0,0) + turtle.write("Play") + turtle.title("My Snake Game") + turtle.onscreenclick(start) + turtle.mainloop() + +def window(): + turtle.clear() + turtle.pu() + turtle.speed(0) + turtle.pensize(20) + turtle.color("black") + turtle.goto(-220,220) + turtle.pd() + turtle.goto(220,220) + turtle.goto(220,-220) + turtle.goto(-220,-220) + turtle.goto(-220,220) + turtle.pu() + turtle.goto(0,0) + +def start(x,y): + turtle.onscreenclick(None) + + window() + + tfood = turtle.Turtle() + tfood.hideturtle() + tfood.pu() + tfood.speed(0) + tfood.shape("square") + tfood.color("red") + + tscore = turtle.Turtle() + tscore.hideturtle() + tscore.pu() + tscore.speed(0) + tscore.goto(100,-250) + tscore.write("Score:" + str(a[0]), align="center",font=(10)) + + while x > -210 and x < 210 and y > -210 and y <210: + if foodcoord[2] == 0: + food(tfood) + foodcoord[2] = 1 + turtle.onkey(u,"Up") + turtle.onkey(l,"Left") + turtle.onkey(r,"Right") + turtle.onkey(d,"Down") + turtle.listen() + move() + x = turtle.xcor() + y = turtle.ycor() + if x > foodcoord[0]*20-5 and x < foodcoord[0]*20+5 and y > foodcoord[1]*20-5 and y < foodcoord[1]*20+5: + foodcoord[2] = 0 + tfood.clear() + a[0] += 1 + tscore.clear() + tscore.write("Score:" + str(a[0]), align="center",font=(10)) - while(True): - i = randint(0, self.size-1) - j = randint(0, self.size-1) - entity = [i, j] - - if entity not in self.snake_coords: - self.field[i][j] = 3 - break - - def _generate_field(self): - self.field = [[0 for j in range(self.size)] for i in range(self.size)] - - def _clear_field(self): - self.field = [[j if j!= 1 and j!= 2 else 0 for j in i] for i in self.field] - - - def render(self, screen): - size = self.size - self._clear_field() - - - # Render snake on the field - for i, j in self.snake_coords: - self.field[i][j] = 1 - - # Mark head - head = self.snake_coords[-1] - self.field[head[0]][head[1]] = 2 - - for i in range(size): - row = '' - for j in range(size): - row += self.icons[ self.field[i][j] ] - - screen.addstr(i, 0, row) - - def get_entity_pos(self): - for i in range(self.size): - for j in range(self.size): - if self.field[i][j] == 3: - return [i, j] - - return [-1, -1] - - - def is_snake_eat_entity(self): - entity = self.get_entity_pos() - head = self.snake_coords[-1] - return entity == head - - -class Snake: - def __init__(self, name): - self.name = name - self.direction = curses.KEY_RIGHT - - # Init basic coords - self.coords = [[0, 0], [0, 1], [0, 2], [0, 3]] - - def set_direction(self, ch): - - # Check if wrong direction - if ch == curses.KEY_LEFT and self.direction == curses.KEY_RIGHT: - return - if ch == curses.KEY_RIGHT and self.direction == curses.KEY_LEFT: - return - if ch == curses.KEY_UP and self.direction == curses.KEY_DOWN: - return - if ch == curses.KEY_DOWN and self.direction == curses.KEY_UP: - return - - self.direction = ch - - def level_up(self): - # get last point direction - a = self.coords[0] - b = self.coords[1] - - tail = a[:] - - if a[0] < b[0]: - tail[0]-=1 - elif a[1] < b[1]: - tail[1]-=1 - elif a[0] > b[0]: - tail[0]+=1 - elif a[1] > b[1]: - tail[1]+=1 - - tail = self._check_limit(tail) - self.coords.insert(0, tail) - - def is_alive(self): - head = self.coords[-1] - snake_body = self.coords[:-1] - return head not in snake_body - - def _check_limit(self, point): - # Check field limit - if point[0] > self.field.size-1: - point[0] = 0 - elif point[0] < 0: - point[0] = self.field.size-1 - elif point[1] < 0: - point[1] = self.field.size-1 - elif point[1] > self.field.size-1: - point[1] = 0 - - return point - - def move(self): - # Determine head coords - head = self.coords[-1][:] - - # Calc new head coords - if self.direction == curses.KEY_UP: - head[0]-=1 - elif self.direction == curses.KEY_DOWN: - head[0]+=1 - elif self.direction == curses.KEY_RIGHT: - head[1]+=1 - elif self.direction == curses.KEY_LEFT: - head[1]-=1 - - # Check field limit - head = self._check_limit(head) - - del(self.coords[0]) - self.coords.append(head) - self.field.snake_coords = self.coords - - if not self.is_alive(): - sys.exit() - - - # check if snake eat an entity - if self.field.is_snake_eat_entity(): - curses.beep() - self.level_up() - self.field.add_entity() - - - - - def set_field(self, field): - self.field = field - - -def main(screen): - # Configure screen - screen.timeout(0) - - # Init snake & field - field = Field(10) - snake = Snake("Joe") - snake.set_field(field) - - while(True): - # Get last pressed key - ch = screen.getch() - if ch != -1: - # If some arrows did pressed - change direction - snake.set_direction(ch) - - # Move snake - snake.move() + if len(cpos) > 1: + for i in range(1,len(cpos)): + if x < cpos[i][0]+5 and x > cpos[i][0]-5 and y < cpos[i][1]+5 and y > cpos[i][1]-5: + tscore.clear() + tfood.clear() + gameover() + tscore.clear() + tfood.clear() + gameover() + + +#Food +def food(tfood): + x = random.randrange(-8,8,1) + y = random.randrange(-8,8,1) + foodcoord[0] = x + foodcoord[1] = y + tfood.hideturtle() + tfood.pu() + tfood.shape("square") + tfood.color("blue") + tfood.goto(x*20,y*20) + tfood.stamp() + +#Up +def u(): + if head[0] == 270: + pass + else: + head[0] = 90 +#Down +def d(): + if head[0] == 90: + pass + else: + head[0] = 270 +#Left +def l(): + if head[0] == 0: + pass + else: + head[0] = 180 +#Right +def r(): + if head[0] == 180: + pass + else: + head[0] = 0 + +def move(): + turtle.pensize(1) + turtle.color("green") + turtle.pu() + turtle.speed(3) + turtle.setheading(head[0]) + turtle.shape("square") + turtle.stamp() + turtle.fd(20) + x = turtle.xcor() + y = turtle.ycor() + if b[0] > a[0]: + turtle.clearstamps(1) + cpos.insert(0,[round(x),round(y)]) + cpos.pop(-1) + else: + cpos.insert(0,[round(x),round(y)]) + b[0] += 1 + +def gameover(): + turtle.onscreenclick(None) + turtle.speed(0) + turtle.pu() + turtle.goto(0,150) + turtle.color("red") + turtle.write("Sorry, game over",align="center", font=(15)) + turtle.goto(0,50) + turtle.write("Your Score:" + str(a[0]),align="center",font=(10)) + turtle.goto(200,-200) + turtle.write("Click to return",align="right",font=(0.0000001)) + turtle.onscreenclick(home) + turtle.mainloop() + - # Render field - field.render(screen) - screen.refresh() - - time.sleep(.4) -if __name__=='__main__': - curses.wrapper(main) + +if __name__ == '__main__': + home(0,0)