├── .gitpod.yml ├── README.md ├── requirements.txt └── snake.py /.gitpod.yml: -------------------------------------------------------------------------------- 1 | image: gitpod/workspace-full-vnc 2 | ports: 3 | - port: 5900 4 | onOpen: ignore 5 | - port: 6080 6 | onOpen: open-preview 7 | tasks: 8 | - init: pip3 install -r requirements.txt 9 | command: python3 snake.py 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Snake-Game 2 | The classic snake game. Made with pygame. 3 | 4 | You can check out my video tutorial series on how to create this game: https://www.youtube.com/watch?v=5tvER0MT14s&t=2s 5 | 6 | # Requirements 7 | - Python 3.x 8 | - pygame 9 | 10 | # 💻 Launch Your Software Development Career Today! 11 | 12 | 🎓 **No degree? No problem!** My program equips you with everything you need to break into tech and land an entry-level software development role. 13 | 14 | 🚀 **Why Join?** 15 | - 💼 **$70k+ starting salary potential** 16 | - 🕐 **Self-paced:** Complete on your own time 17 | - 🤑 **Affordable:** Low risk compared to expensive bootcamps or degrees 18 | - 🎯 **45,000+ job openings** in the market 19 | 20 | 👉 **[Start your journey today!](https://techwithtim.net/dev)** 21 | No experience needed—just your determination. Future-proof your career and unlock six-figure potential like many of our students have! 22 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | pygame 2 | -------------------------------------------------------------------------------- /snake.py: -------------------------------------------------------------------------------- 1 | import math 2 | import random 3 | import pygame 4 | import random 5 | import tkinter as tk 6 | from tkinter import messagebox 7 | 8 | width = 500 9 | height = 500 10 | 11 | cols = 25 12 | rows = 20 13 | 14 | 15 | class cube(): 16 | rows = 20 17 | w = 500 18 | def __init__(self, start, dirnx=1, dirny=0, color=(255,0,0)): 19 | self.pos = start 20 | self.dirnx = dirnx 21 | self.dirny = dirny # "L", "R", "U", "D" 22 | self.color = color 23 | 24 | def move(self, dirnx, dirny): 25 | self.dirnx = dirnx 26 | self.dirny = dirny 27 | self.pos = (self.pos[0] + self.dirnx, self.pos[1] + self.dirny) 28 | 29 | 30 | def draw(self, surface, eyes=False): 31 | dis = self.w // self.rows 32 | i = self.pos[0] 33 | j = self.pos[1] 34 | 35 | pygame.draw.rect(surface, self.color, (i*dis+1,j*dis+1,dis-2,dis-2)) 36 | if eyes: 37 | centre = dis//2 38 | radius = 3 39 | circleMiddle = (i*dis+centre-radius,j*dis+8) 40 | circleMiddle2 = (i*dis + dis -radius*2, j*dis+8) 41 | pygame.draw.circle(surface, (0,0,0), circleMiddle, radius) 42 | pygame.draw.circle(surface, (0,0,0), circleMiddle2, radius) 43 | 44 | 45 | 46 | class snake(): 47 | body = [] 48 | turns = {} 49 | 50 | def __init__(self, color, pos): 51 | #pos is given as coordinates on the grid ex (1,5) 52 | self.color = color 53 | self.head = cube(pos) 54 | self.body.append(self.head) 55 | self.dirnx = 0 56 | self.dirny = 1 57 | 58 | def move(self): 59 | for event in pygame.event.get(): 60 | if event.type == pygame.QUIT: 61 | pygame.quit() 62 | keys = pygame.key.get_pressed() 63 | 64 | for key in keys: 65 | if keys[pygame.K_LEFT]: 66 | self.dirnx = -1 67 | self.dirny = 0 68 | self.turns[self.head.pos[:]] = [self.dirnx,self.dirny] 69 | elif keys[pygame.K_RIGHT]: 70 | self.dirnx = 1 71 | self.dirny = 0 72 | self.turns[self.head.pos[:]] = [self.dirnx,self.dirny] 73 | elif keys[pygame.K_UP]: 74 | self.dirny = -1 75 | self.dirnx = 0 76 | self.turns[self.head.pos[:]] = [self.dirnx,self.dirny] 77 | elif keys[pygame.K_DOWN]: 78 | self.dirny = 1 79 | self.dirnx = 0 80 | self.turns[self.head.pos[:]] = [self.dirnx,self.dirny] 81 | 82 | for i, c in enumerate(self.body): 83 | p = c.pos[:] 84 | if p in self.turns: 85 | turn = self.turns[p] 86 | c.move(turn[0], turn[1]) 87 | if i == len(self.body)-1: 88 | self.turns.pop(p) 89 | else: 90 | c.move(c.dirnx,c.dirny) 91 | 92 | 93 | def reset(self,pos): 94 | self.head = cube(pos) 95 | self.body = [] 96 | self.body.append(self.head) 97 | self.turns = {} 98 | self.dirnx = 0 99 | self.dirny = 1 100 | 101 | def addCube(self): 102 | tail = self.body[-1] 103 | dx, dy = tail.dirnx, tail.dirny 104 | 105 | if dx == 1 and dy == 0: 106 | self.body.append(cube((tail.pos[0]-1,tail.pos[1]))) 107 | elif dx == -1 and dy == 0: 108 | self.body.append(cube((tail.pos[0]+1,tail.pos[1]))) 109 | elif dx == 0 and dy == 1: 110 | self.body.append(cube((tail.pos[0],tail.pos[1]-1))) 111 | elif dx == 0 and dy == -1: 112 | self.body.append(cube((tail.pos[0],tail.pos[1]+1))) 113 | 114 | self.body[-1].dirnx = dx 115 | self.body[-1].dirny = dy 116 | 117 | def draw(self, surface): 118 | for i,c in enumerate(self.body): 119 | if i == 0: 120 | c.draw(surface, True) 121 | else: 122 | c.draw(surface) 123 | 124 | 125 | 126 | def redrawWindow(): 127 | global win 128 | win.fill((0,0,0)) 129 | drawGrid(width, rows, win) 130 | s.draw(win) 131 | snack.draw(win) 132 | pygame.display.update() 133 | pass 134 | 135 | 136 | 137 | def drawGrid(w, rows, surface): 138 | sizeBtwn = w // rows 139 | 140 | x = 0 141 | y = 0 142 | for l in range(rows): 143 | x = x + sizeBtwn 144 | y = y +sizeBtwn 145 | 146 | pygame.draw.line(surface, (255,255,255), (x, 0),(x,w)) 147 | pygame.draw.line(surface, (255,255,255), (0, y),(w,y)) 148 | 149 | 150 | 151 | def randomSnack(rows, item): 152 | positions = item.body 153 | 154 | while True: 155 | x = random.randrange(1,rows-1) 156 | y = random.randrange(1,rows-1) 157 | if len(list(filter(lambda z:z.pos == (x,y), positions))) > 0: 158 | continue 159 | else: 160 | break 161 | 162 | return (x,y) 163 | 164 | 165 | def main(): 166 | global s, snack, win 167 | win = pygame.display.set_mode((width,height)) 168 | s = snake((255,0,0), (10,10)) 169 | s.addCube() 170 | snack = cube(randomSnack(rows,s), color=(0,255,0)) 171 | flag = True 172 | clock = pygame.time.Clock() 173 | 174 | while flag: 175 | pygame.time.delay(50) 176 | clock.tick(10) 177 | s.move() 178 | headPos = s.head.pos 179 | if headPos[0] >= 20 or headPos[0] < 0 or headPos[1] >= 20 or headPos[1] < 0: 180 | print("Score:", len(s.body)) 181 | s.reset((10, 10)) 182 | 183 | if s.body[0].pos == snack.pos: 184 | s.addCube() 185 | snack = cube(randomSnack(rows,s), color=(0,255,0)) 186 | 187 | for x in range(len(s.body)): 188 | if s.body[x].pos in list(map(lambda z:z.pos,s.body[x+1:])): 189 | print("Score:", len(s.body)) 190 | s.reset((10,10)) 191 | break 192 | 193 | redrawWindow() 194 | 195 | main() 196 | 197 | 198 | 199 | --------------------------------------------------------------------------------