Visitar URL original
snake, apple game · codebasics/python_projects@c4ebe5d · GitHub
Skip to content

Commit c4ebe5d

Browse files
committed
snake, apple game
1 parent 0179cae commit c4ebe5d

19 files changed

Lines changed: 1223 additions & 0 deletions

‎.gitignore‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.idea
2+
13
# Byte-compiled / optimized / DLL files
24
__pycache__/
35
*.py[cod]
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Build a screen and a moving block
2+
3+
import pygame
4+
from pygame.locals import *
5+
6+
def draw_block():
7+
surface.fill((110, 110, 5))
8+
surface.blit(block, (block_x, block_y))
9+
pygame.display.flip()
10+
11+
if __name__ == '__main__':
12+
pygame.init()
13+
14+
surface = pygame.display.set_mode((500, 500))
15+
surface.fill((110, 110, 5))
16+
17+
block = pygame.image.load("resources/block.jpg").convert()
18+
19+
block_x, block_y = 100, 100
20+
21+
surface.blit(block, (block_x, block_y))
22+
23+
pygame.display.flip()
24+
25+
running = True
26+
27+
while running:
28+
for event in pygame.event.get():
29+
if event.type == KEYDOWN:
30+
if event.key == K_ESCAPE:
31+
running = False
32+
if event.key == K_LEFT:
33+
block_x -= 10
34+
draw_block()
35+
if event.key == K_RIGHT:
36+
block_x += 10
37+
draw_block()
38+
if event.key == K_UP:
39+
block_y -= 10
40+
draw_block()
41+
if event.key == K_DOWN:
42+
block_y += 10
43+
draw_block()
44+
45+
elif event.type == QUIT:
46+
running = False
47+
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Make it all object oriented by converting the code in various classes
2+
3+
import pygame
4+
from pygame.locals import *
5+
6+
class Snake:
7+
def __init__(self, surface):
8+
self.parent_screen = surface
9+
self.block = pygame.image.load("resources/block.jpg").convert()
10+
self.x = 100
11+
self.y = 100
12+
13+
def move_left(self):
14+
self.x -= 10
15+
self.draw()
16+
17+
def move_right(self):
18+
self.x += 10
19+
self.draw()
20+
21+
def move_up(self):
22+
self.y -= 10
23+
self.draw()
24+
25+
def move_down(self):
26+
self.y += 10
27+
self.draw()
28+
29+
def draw(self):
30+
self.parent_screen.fill((110, 110, 5))
31+
32+
self.parent_screen.blit(self.block, (self.x, self.y))
33+
pygame.display.flip()
34+
35+
36+
class Game:
37+
def __init__(self):
38+
pygame.init()
39+
self.surface = pygame.display.set_mode((500, 500))
40+
self.snake = Snake(self.surface)
41+
self.snake.draw()
42+
43+
def run(self):
44+
running = True
45+
46+
while running:
47+
for event in pygame.event.get():
48+
if event.type == KEYDOWN:
49+
if event.key == K_ESCAPE:
50+
running = False
51+
if event.key == K_LEFT:
52+
self.snake.move_left()
53+
54+
if event.key == K_RIGHT:
55+
self.snake.move_right()
56+
57+
if event.key == K_UP:
58+
self.snake.move_up()
59+
60+
if event.key == K_DOWN:
61+
self.snake.move_down()
62+
63+
elif event.type == QUIT:
64+
running = False
65+
66+
if __name__ == '__main__':
67+
game = Game()
68+
game.run()
69+
70+
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Moving block with a timer
2+
3+
import pygame
4+
from pygame.locals import *
5+
import time
6+
7+
class Snake:
8+
def __init__(self, surface):
9+
self.parent_screen = surface
10+
self.block = pygame.image.load("resources/block.jpg").convert()
11+
self.x = 100
12+
self.y = 100
13+
self.direction = 'down'
14+
15+
def move_left(self):
16+
self.direction = 'left'
17+
18+
def move_right(self):
19+
self.direction = 'right'
20+
21+
def move_up(self):
22+
self.direction = 'up'
23+
24+
def move_down(self):
25+
self.direction = 'down'
26+
27+
def walk(self):
28+
if self.direction == 'left':
29+
self.x -= 10
30+
if self.direction == 'right':
31+
self.x += 10
32+
if self.direction == 'up':
33+
self.y -= 10
34+
if self.direction == 'down':
35+
self.y += 10
36+
37+
self.draw()
38+
39+
40+
def draw(self):
41+
self.parent_screen.fill((110, 110, 5))
42+
43+
self.parent_screen.blit(self.block, (self.x, self.y))
44+
pygame.display.flip()
45+
46+
47+
class Game:
48+
def __init__(self):
49+
pygame.init()
50+
self.surface = pygame.display.set_mode((500, 500))
51+
self.snake = Snake(self.surface)
52+
self.snake.draw()
53+
54+
def run(self):
55+
running = True
56+
57+
while running:
58+
for event in pygame.event.get():
59+
if event.type == KEYDOWN:
60+
if event.key == K_ESCAPE:
61+
running = False
62+
63+
if event.key == K_LEFT:
64+
self.snake.move_left()
65+
66+
if event.key == K_RIGHT:
67+
self.snake.move_right()
68+
69+
if event.key == K_UP:
70+
self.snake.move_up()
71+
72+
if event.key == K_DOWN:
73+
self.snake.move_down()
74+
75+
elif event.type == QUIT:
76+
running = False
77+
78+
self.snake.walk()
79+
80+
time.sleep(.2)
81+
82+
if __name__ == '__main__':
83+
game = Game()
84+
game.run()
85+
86+
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# Convert block into a snake
2+
# Draw apple at random locations
3+
4+
import pygame
5+
from pygame.locals import *
6+
import time
7+
import random
8+
9+
SIZE = 40
10+
11+
class Apple:
12+
def __init__(self, parent_screen):
13+
self.parent_screen = parent_screen
14+
self.image = pygame.image.load("resources/apple.jpg").convert()
15+
self.x = 120
16+
self.y = 120
17+
18+
def draw(self):
19+
self.parent_screen.blit(self.image, (self.x, self.y))
20+
pygame.display.flip()
21+
22+
def move(self):
23+
self.x = random.randint(1,25)*SIZE
24+
self.y = random.randint(1,20)*SIZE
25+
26+
class Snake:
27+
def __init__(self, parent_screen, length):
28+
self.parent_screen = parent_screen
29+
self.image = pygame.image.load("resources/block.jpg").convert()
30+
self.direction = 'down'
31+
32+
self.length = length
33+
self.x = [40]*length
34+
self.y = [40]*length
35+
36+
def move_left(self):
37+
self.direction = 'left'
38+
39+
def move_right(self):
40+
self.direction = 'right'
41+
42+
def move_up(self):
43+
self.direction = 'up'
44+
45+
def move_down(self):
46+
self.direction = 'down'
47+
48+
def walk(self):
49+
# update body
50+
for i in range(self.length-1,0,-1):
51+
self.x[i] = self.x[i-1]
52+
self.y[i] = self.y[i-1]
53+
54+
# update head
55+
if self.direction == 'left':
56+
self.x[0] -= SIZE
57+
if self.direction == 'right':
58+
self.x[0] += SIZE
59+
if self.direction == 'up':
60+
self.y[0] -= SIZE
61+
if self.direction == 'down':
62+
self.y[0] += SIZE
63+
64+
self.draw()
65+
66+
def draw(self):
67+
self.parent_screen.fill((110, 110, 5))
68+
69+
for i in range(self.length):
70+
self.parent_screen.blit(self.image, (self.x[i], self.y[i]))
71+
pygame.display.flip()
72+
73+
def increase_length(self):
74+
self.length += 1
75+
self.x.append(-1)
76+
self.y.append(-1)
77+
78+
class Game:
79+
def __init__(self):
80+
pygame.init()
81+
self.surface = pygame.display.set_mode((1000, 800))
82+
self.snake = Snake(self.surface, 5)
83+
self.snake.draw()
84+
self.apple = Apple(self.surface)
85+
self.apple.draw()
86+
87+
88+
def play(self):
89+
self.snake.walk()
90+
self.apple.draw()
91+
92+
def run(self):
93+
running = True
94+
95+
while running:
96+
for event in pygame.event.get():
97+
if event.type == KEYDOWN:
98+
if event.key == K_ESCAPE:
99+
running = False
100+
101+
if event.key == K_LEFT:
102+
self.snake.move_left()
103+
104+
if event.key == K_RIGHT:
105+
self.snake.move_right()
106+
107+
if event.key == K_UP:
108+
self.snake.move_up()
109+
110+
if event.key == K_DOWN:
111+
self.snake.move_down()
112+
113+
elif event.type == QUIT:
114+
running = False
115+
116+
self.play()
117+
118+
time.sleep(.2)
119+
120+
if __name__ == '__main__':
121+
game = Game()
122+
game.run()
123+
124+

0 commit comments

Comments
 (0)