-
Notifications
You must be signed in to change notification settings - Fork 0
/
powerups.py
72 lines (58 loc) · 2.51 KB
/
powerups.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
import pygame
import random
from base import SNAKE_BLOCK, COLORS, WIN
class PowerUp:
def __init__(self, color, effect, effect_duration=5000, disappear_time=3000):
self.position = self.generate_position()
self.color = color
self.effect = effect
self.effect_duration = effect_duration
self.disappear_time = disappear_time
self.active = False
# Set timer for power-up disappearance after 3 seconds
pygame.time.set_timer(pygame.USEREVENT + 1, self.disappear_time)
def generate_position(self):
return [
round(random.randrange(0, WIN.get_width() - SNAKE_BLOCK) / 20.0) * 20.0,
round(random.randrange(0, WIN.get_height() - SNAKE_BLOCK) / 20.0) * 20.0,
]
def draw(self):
pygame.draw.circle(WIN, self.color, (self.position[0] + SNAKE_BLOCK // 2, self.position[1] + SNAKE_BLOCK // 2), SNAKE_BLOCK // 2)
def activate(self, game):
if not self.active:
self.effect(game)
self.active = True
# Set timer for power-up effect duration
pygame.time.set_timer(pygame.USEREVENT + 2, self.effect_duration)
# Remove the power-up from the game visually
game.power_up = None
pygame.time.set_timer(pygame.USEREVENT + 1, 0) # Stop the disappearance timer
def deactivate(self, game):
# Reset game state after power-up expires
game.settings["speed"] = game.settings["DEFAULT_SPEED"]
game.snake.invincible = False
game.multiplier = 1
self.active = False
pygame.time.set_timer(pygame.USEREVENT + 2, 0) # Stop the effect timer
class SlowDownPowerUp(PowerUp):
def __init__(self):
super().__init__(COLORS["POWER_UP_BLUE"], self.slow_down)
def slow_down(self, game):
game.settings["speed"] = game.settings["DEFAULT_SPEED"] // 2
class ShrinkPowerUp(PowerUp):
def __init__(self):
super().__init__(COLORS["POWER_UP_PURPLE"], self.shrink)
def shrink(self, game):
if game.snake.length > 3:
game.snake.body = game.snake.body[:-3]
game.snake.length -= 3
class ScoreMultiplierPowerUp(PowerUp):
def __init__(self):
super().__init__(COLORS["POWER_UP_GREEN"], self.multiply_score)
def multiply_score(self, game):
game.multiplier = 2
class InvincibilityPowerUp(PowerUp):
def __init__(self):
super().__init__(COLORS["POWER_UP_STAR"], self.invincible)
def invincible(self, game):
game.snake.invincible = True