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
| import pygame
import sys
import random
# Initialize Pygame
# Инициализируем Pygame
pygame.init()
# Set screen dimensions
# Устанавливаем размеры экрана
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
# Set title of the window
# Устанавливаем заголовок окна
pygame.display.set_caption('Arcanoid')
# Define colors
# Определяем цвета
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
BLUE = (106, 159, 181)
RED = (255, 0, 0)
# Define font
# Определяем шрифт
font = pygame.font.Font(None, 30)
# Define button class
# Определяем класс кнопки
class Button:
def __init__(self, text, x, y, width, height, color, highlighted_color):
self.text = text
self.x = x
self.y = y
self.width = width
self.height = height
self.color = color
self.highlighted_color = highlighted_color
self.highlighted = False
def draw(self, screen):
if self.highlighted:
pygame.draw.rect(screen, self.highlighted_color, (self.x, self.y, self.width, self.height))
else:
pygame.draw.rect(screen, self.color, (self.x, self.y, self.width, self.height))
text_surface = font.render(self.text, True, WHITE)
screen.blit(text_surface, (self.x + 10, self.y + 10))
def is_hovered(self, mouse_pos):
mouse_x, mouse_y = mouse_pos
if self.x <= mouse_x <= self.x + self.width and self.y <= mouse_y <= self.y + self.height:
return True
return False
# Create buttons
# Создаём кнопки
start_button = Button("Start", 350, 300, 100, 50, BLUE, WHITE)
speed_button = Button("Speed", 350, 400, 100, 50, BLUE, WHITE)
# Main game loop
# Основной цикл игры
game_state = "menu"
speed = 1
# Game variables
# Переменные игры
ball_x = screen_width / 2
ball_y = screen_height / 2
ball_speed_x = 5
ball_speed_y = 5
paddle_x = screen_width / 2
paddle_y = screen_height - 50
paddle_speed = 10
attempts_left = 5
blocks = []
for _ in range(10):
x = random.randint(0, screen_width // 2)
y = random.randint(50, screen_height // 2)
blocks.append((x,y))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1: # Left mouse button
if start_button.is_hovered(event.pos):
game_state = "game"
ball_x=paddle_x+50
ball_y=paddle_y-10
if speed_button.is_hovered(event.pos):
speed += 1
keys=pygame.key.get_pressed()
if keys[pygame.K_SPACE] or keys[pygame.K_UP]:
if game_state=="menu":
game_state="game"
ball_x=paddle_x+50
ball_y=paddle_y-10
# Clear screen
# Очищаем экран
screen.fill(BLACK)
# Draw buttons
# Рисуем кнопки
if game_state=="menu":
start_button.draw(screen)
speed_button.draw(screen)
text_surface=font.render(f"Attempts left:{attempts_left}",True,RED)
screen.blit(text_surface,(screen_width-150,20))
# Game loop
# Цикл игры
if game_state=="game":
# Move the ball
# Двигаем мяч
ball_x+=ball_speed_x
ball_y+=ball_speed_y
# Collision with edges
# Столкновение с краями экрана
if ball_y<=0:
ball_speed_y*=-1
if ball_x<=0 or ball_x>=screen_width:
ball_speed_x*=-1
# Collision with paddle
# Столкновение с ракеткой
if ball_y+10>=paddle_y and ball_x>=paddle_x and ball_x<=paddle_x+100:
ball_speed_y*=-1
# Collision with blocks
# Столкновение с блоками
for block in blocks[:]:
if (block<=ball_x<=block+50) and (block<= ball_y<=block+20):
blocks.remove(block)
ball_speed_y*=-1
# Draw the ball
# Рисуем мяч
pygame.draw.circle(screen ,WHITE ,(int(ball_x),int(ball_y)),10)
# Draw the paddle
# Рисуем ракетку
pygame.draw.rect(screen ,WHITE ,(paddle_x,paddle_y ,100 ,10))
for block in blocks:
pygame.draw.rect(screen ,WHITE ,(block,block,50 ,20))
if ball_y>paddle_y+10:
attempts_left-=1
game_state="menu"
if attempts_left==0:
game_state="menu"
attempts_left=5
blocks=[]
for _ in range(10):
x=random.randint(0,screen_width//2)
y=random.randint(50,screen_height//2)
blocks.append((x,y))
keys=pygame.key.get_pressed()
if keys[pygame.K_LEFT] and paddle_x>0:
paddle_x-=paddle_speed
if keys [pygame.K_RIGHT] and paddle_x<screen_width-100:
paddle_x+=paddle_speed
# Update screen
# Обновляем экран
pygame.display.flip()
pygame.time.Clock().tick(60) |