-
Notifications
You must be signed in to change notification settings - Fork 0
/
jogo.py
218 lines (185 loc) · 5.24 KB
/
jogo.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import turtle
import random
import pygame
import threading
import time
pygame.mixer.init()
pygame.mixer.music.load("musc_ELN5cFaO.mp3")
pygame.mixer.music.play(-1)
death_sound = pygame.mixer.Sound("morte_5VPs8zuv.mp3")
def draw_spiral():
h = 0
w = 0
turtle.hideturtle()
turtle.bgcolor("black")
turtle.speed(0)
turtle.pencolor("green")
turtle.penup()
turtle.goto(0, 200)
turtle.pendown()
while True:
turtle.forward(h)
turtle.right(w)
h += 3
w += 1
if w == 200:
turtle.penup()
turtle.goto(0, 0)
turtle.color("white")
turtle.write("Bem vindos!", align="center", font=("Arial", 80, "normal"))
turtle.ontimer(turtle.clear, 300)
break
if __name__ == "__main__":
draw_spiral()
pygame.mixer.init()
# Definição das constantes e variáveis globais
WIDTH = 750
HEIGHT = 750
FOOD_SIZE = 20
DELAY = 100
BONUS_FOOD_INTERVAL = 20
bonus_food_time = time.time() + BONUS_FOOD_INTERVAL
bonus_food_pos = None
bonus_food_active = False
offsets = {
"up": (0, 20),
"down": (0, -20),
"left": (-20, 0),
"right": (20, 0)
}
score = 0
is_alive = True
# Função para aumentar a pontuação
def increase_score():
global score
if is_alive:
score += 1
update_score()
# Função para atualizar o placar na tela
def update_score():
score_pen.clear()
score_pen.write("Score: {}".format(score), align="center", font=("Arial", 16, "normal"))
# Função para resetar o jogo
def reset():
global snake, snake_direction, food_pos, is_alive, score
is_alive = True
score = 0
update_score()
snake = [[0, 0], [0, 20], [0, 40], [0, 50], [0, 60]]
snake_direction = "up"
food_pos = get_random_food_pos()
food.goto(food_pos)
draw_snake()
move_snake()
# Função para desenhar a cobra
def draw_snake():
for segment in snake:
pen.goto(segment[0], segment[1])
pen.stamp()
# Função para movimentar a cobra
def move_snake():
global snake_direction, is_alive
new_head = snake[-1].copy()
new_head[0] = snake[-1][0] + offsets[snake_direction][0]
new_head[1] = snake[-1][1] + offsets[snake_direction][1]
if new_head in snake[:-1]:
is_alive = False
game_over()
else:
snake.append(new_head)
if not food_collision():
snake.pop(0)
else:
increase_score()
# eating_sound.play() # Reproduz o som de comer quando a comida é coletada
if snake[-1][0] > WIDTH / 2:
snake[-1][0] -= WIDTH
elif snake[-1][0] < - WIDTH / 2:
snake[-1][0] += WIDTH
elif snake[-1][1] > HEIGHT / 2:
snake[-1][1] -= HEIGHT
elif snake[-1][1] < -HEIGHT / 2:
snake[-1][1] += HEIGHT
pen.clearstamps()
draw_snake()
screen.update()
if is_alive:
turtle.ontimer(move_snake, DELAY)
# Função para exibir "Game Over" e oferecer a opção de reiniciar o jogo
def game_over():
pen.clear()
pen.goto(0, 0)
pen.write("Game Over", align="center", font=("Arial", 24, "normal"))
pen.goto(0, -30)
pen.write("Pressione R para reiniciar", align="center", font=("Arial", 16, "normal"))
pygame.mixer.music.stop() # Parar a música de fundo
death_sound.play() # Reproduzir a música de morte
# Função para limpar a tela
def clear_screen():
pen.clear()
# Função para verificar se houve colisão com a comida
def food_collision():
global food_pos
head_x, head_y = snake[-1]
food_x, food_y = food_pos
if abs(head_x - food_x) < 1 * FOOD_SIZE and abs(head_y - food_y) < 1 * FOOD_SIZE:
food_pos = get_random_food_pos()
food.goto(food_pos)
return True
return False
# Função para gerar uma posição aleatória para a comida
def get_random_food_pos():
x = random.randint(-WIDTH // 2 + FOOD_SIZE, WIDTH // 2 - FOOD_SIZE)
y = random.randint(-HEIGHT // 2 + FOOD_SIZE, HEIGHT // 2 - FOOD_SIZE)
return (x, y)
# Funções para movimentar a cobra nas direções
def go_up():
global snake_direction
if snake_direction != "down":
snake_direction = "up"
def go_right():
global snake_direction
if snake_direction != "left":
snake_direction = "right"
def go_down():
global snake_direction
if snake_direction != "up":
snake_direction = "down"
def go_left():
global snake_direction
if snake_direction != "right":
snake_direction = "left"
def restart():
if not is_alive:
reset()
clear_screen()
pygame.mixer.music.play(-1) # Reproduzir a música de fundo novamente
screen = turtle.Screen()
screen.setup(WIDTH, HEIGHT)
screen.title("Snake Game")
screen.bgcolor("#024959")
screen.tracer(0)
snake_color = "green"
snake_shape = "square"
pen = turtle.Turtle(snake_shape)
pen.penup()
pen.color(snake_color)
food = turtle.Turtle()
food.shape("circle")
food.color("red")
food.shapesize(FOOD_SIZE / 20)
food.penup()
score_pen = turtle.Turtle()
score_pen.penup()
score_pen.color("white")
score_pen.goto(0, HEIGHT // 2 - 40)
score_pen.hideturtle()
update_score()
screen.listen()
screen.onkey(go_up, "Up")
screen.onkey(go_right, "Right")
screen.onkey(go_down, "Down")
screen.onkey(go_left, "Left")
screen.onkey(restart, "r")
reset()
turtle.done()