-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.py
135 lines (110 loc) · 4.79 KB
/
Game.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# importing libraries
import pygame
from ColorClass import Color
from FruitClass import Fruit
from PlayerClass import Player
from RulesClass import Rules
from ScoreClass import Score
from ScreenClass import Screen
from SnakeClass import Snake
snake = Snake()
screen = Screen()
color = Color()
player = Player()
score = Score()
rules = Rules()
pygame.init()
# Initialise game window
pygame.display.set_caption('Snake Game')
game_window = pygame.display.set_mode((screen.x, screen.y))
# FPS (frames per second) controller
fps = pygame.time.Clock()
# Creating the fruits and giving them an image
bonus = Fruit(screen.x, screen.y, pygame.image.load('images/bonus.png'))
bonus.new_position(screen.x, screen.y)
malus = Fruit(screen.x, screen.y, pygame.image.load('images/malus.png'))
malus.new_position(screen.x, screen.y)
if player.difficulty != 3:
health = Fruit(screen.x, screen.y, pygame.image.load('images/hp.webp'))
health.new_position(screen.x, screen.y)
change_to = snake.direction
while True:
# handling key events
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
change_to = 'UP'
if event.key == pygame.K_DOWN:
change_to = 'DOWN'
if event.key == pygame.K_LEFT:
change_to = 'LEFT'
if event.key == pygame.K_RIGHT:
change_to = 'RIGHT'
# Prevents the snake from moving in multiple directions if multiple keys are pressed
if change_to == 'UP' and snake.direction != 'DOWN':
snake.direction = 'UP'
if change_to == 'DOWN' and snake.direction != 'UP':
snake.direction = 'DOWN'
if change_to == 'LEFT' and snake.direction != 'RIGHT':
snake.direction = 'LEFT'
if change_to == 'RIGHT' and snake.direction != 'LEFT':
snake.direction = 'RIGHT'
# Moving the snake
if snake.direction == 'UP':
snake.position[1] -= 10
if snake.direction == 'DOWN':
snake.position[1] += 10
if snake.direction == 'LEFT':
snake.position[0] -= 10
if snake.direction == 'RIGHT':
snake.position[0] += 10
# Different possibilities if fruits and snake collide
snake.body.insert(0, list(snake.position))
if snake.position[0] == bonus.position[0] and snake.position[1] == bonus.position[1]:
score.point, snake.speed = bonus.get_bonus(score.point, player.difficulty, snake.speed)
elif snake.position[0] == malus.position[0] and snake.position[1] == malus.position[1]:
score.point, snake.speed = malus.get_malus(score.point, player.difficulty, snake.speed)
elif player.difficulty != 3 and snake.position[0] == health.position[0] and snake.position[1] == health.position[1]:
player.hp = health.get_health(player.hp)
else:
snake.body.pop()
bonus.fruit_spawn(screen.x, screen.y)
malus.fruit_spawn(screen.x, screen.y)
if player.difficulty != 3:
health.fruit_spawn(screen.x, screen.y)
game_window.fill(color.background)
for pos in snake.body:
pygame.draw.rect(game_window, color.snake, pygame.Rect(
pos[0], pos[1], 10, 10))
pygame.draw.rect(game_window, color.background, pygame.Rect(
bonus.position[0], bonus.position[1], 10, 10))
pygame.draw.rect(game_window, color.background, pygame.Rect(
malus.position[0], malus.position[1], 10, 10))
if player.difficulty != 3:
pygame.draw.rect(game_window, color.background, pygame.Rect(
health.position[0], health.position[1], 10, 10))
# Game Over conditions
if snake.position[0] < 0 or snake.position[0] > screen.x - 10:
rules.game_over(score.point, color.red, screen.x, screen.y, game_window)
if snake.position[1] < 0 or snake.position[1] > screen.y - 10:
rules.game_over(score.point, color.red, screen.x, screen.y, game_window)
if player.hp == 0:
rules.game_over(score.point, color.red, screen.x, screen.y, game_window)
if score.point < 0:
score.point = 0
rules.game_over(score.point, color.red, screen.x, screen.y, game_window)
# Touching the snake body
for block in snake.body[1:]:
if snake.position[0] == block[0] and snake.position[1] == block[1]:
rules.game_over(score.point, color.red, screen.x, screen.y, game_window)
# displaying score and hps continuously
score.show_score(1, color.white, 'times new roman', 20, score.point, game_window)
player.show_hp(1, color.white, 'times new roman', 20, player.hp, game_window, screen.x, 0)
bonus.show_fruit(game_window, color.background)
malus.show_fruit(game_window, color.background)
if player.difficulty != 3:
health.show_fruit(game_window, color.background)
# Refresh game screen
pygame.display.update()
# Frame Per Second /Refresh Rate
fps.tick(snake.speed)