Skip to content

Commit dee3eab

Browse files
committed
Add ball which Snake can to shot
1 parent 3832f02 commit dee3eab

File tree

4 files changed

+89
-1
lines changed

4 files changed

+89
-1
lines changed

__pycache__/constants.cpython-35.pyc

0 Bytes
Binary file not shown.

ball.py

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import copy, pygame
2+
from constants import *
3+
4+
class Ball:
5+
def __init__(self, screen, color, enemy):
6+
self.ready = True
7+
self.x = 0
8+
self.y = 0
9+
self.dx = 0
10+
self.dy = 0
11+
self.screen = screen
12+
r = color[0] - 100
13+
if r < 0:
14+
r = 0
15+
g = color[1] - 100
16+
if g < 0:
17+
g = 0
18+
b = color[2] - 100
19+
if b < 0:
20+
b = 0
21+
self.color = (r, g, b)
22+
self.enemy = enemy
23+
24+
def shot(self, x, y, direction):
25+
if self.ready != False:
26+
self.x = x
27+
self.y = y
28+
self.ready = False
29+
if direction == 'right':
30+
self.dx = 1
31+
self.dy = 0
32+
if direction == 'left':
33+
self.dx = -1
34+
self.dy = 0
35+
if direction == 'up':
36+
self.dx = 0
37+
self.dy = -1
38+
if direction == 'down':
39+
self.dx = 0
40+
self.dy = 1
41+
42+
self.go()
43+
44+
def go(self):
45+
self.x += self.dx
46+
self.y += self.dy
47+
if self.x < 0 or self.x >= W or self.y < 0 or self.y >= H:
48+
self.ready = True
49+
50+
if [self.x, self.y] in self.enemy.body:
51+
self.ready = False
52+
if self.enemy.body[0][0] == self.x and self.enemy.body[0][1] == self.y:
53+
self.enemy.isAlive = False
54+
else:
55+
newBody = []
56+
for b in self.enemy.body:
57+
if b[0] == self.x and b[1] == self.y:
58+
break
59+
newBody.append(b)
60+
self.enemy.body = newBody
61+
62+
def draw(self):
63+
pygame.draw.ellipse(self.screen, self.color, (self.x * blockW, self.y * blockH, blockW, blockH))

main.py

+22
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from snake import Snake
33
from food import Food
44
from constants import *
5+
from ball import Ball
56

67

78
pygame.init()
@@ -84,6 +85,10 @@ def game():
8485

8586
snake1 = Snake(2, 2, screen, (255, 255, 0))
8687
snake2 = Snake(20, 20, screen, (255, 0, 255))
88+
89+
snake1.ball = Ball(screen, snake1.color, snake2)
90+
snake2.ball = Ball(screen, snake2.color, snake1)
91+
8792
yummy = Food(25, screen)
8893

8994
while True:
@@ -101,6 +106,8 @@ def game():
101106
snake1.tryToDirect('left')
102107
if e.key == pygame.K_RIGHT:
103108
snake1.tryToDirect('right')
109+
if e.key == pygame.K_RCTRL:
110+
snake1.ball.shot(snake1.body[0][0], snake1.body[0][1], snake1.direction)
104111

105112
#second player
106113
if e.key == pygame.K_w:
@@ -111,6 +118,8 @@ def game():
111118
snake2.tryToDirect('down')
112119
if e.key == pygame.K_d:
113120
snake2.tryToDirect('right')
121+
if e.key == pygame.K_r:
122+
snake2.ball.shot(snake2.body[0][0], snake2.body[0][1], snake2.direction)
114123

115124
#pause
116125
if e.key == pygame.K_p:
@@ -131,7 +140,16 @@ def game():
131140
snake2.fixDirection()
132141

133142
fl1 = snake1.die(snake2.body)
143+
if snake1.isAlive == False:
144+
fl1 = False
134145
fl2 = snake2.die(snake1.body)
146+
if snake2.isAlive == False:
147+
fl2 = False
148+
149+
if not snake1.ball.ready:
150+
snake1.ball.go()
151+
if not snake2.ball.ready:
152+
snake2.ball.go()
135153

136154
snake1.go()
137155
snake2.go()
@@ -174,6 +192,10 @@ def game():
174192

175193
snake1.draw()
176194
snake2.draw()
195+
if not snake1.ball.ready:
196+
snake1.ball.draw()
197+
if not snake2.ball.ready:
198+
snake2.ball.draw()
177199
yummy.draw()
178200

179201
text = FONT_low.render('Player1 score: ' + str(snake1.score), True, (255, 255, 0))

snake.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import copy, pygame
22
from constants import *
3+
from ball import Ball
34

45
class Snake:
56
def __init__(self, x, y, screen, color):
@@ -11,6 +12,7 @@ def __init__(self, x, y, screen, color):
1112
self.tail = [x, y]
1213
self.screen = screen
1314
self.color = color
15+
self.isAlive = True
1416

1517
def fixDirection(self):
1618
if self.dx == 1: self.direction = 'right'
@@ -48,7 +50,8 @@ def die(self, enemy):
4850

4951

5052
def go(self):
51-
53+
if self.isAlive == False:
54+
return 1
5255
self.tail = copy.deepcopy(self.body[-1])
5356
i = len(self.body) - 1
5457
while (i > 0):

0 commit comments

Comments
 (0)