import
pygame
from
pygame.
locals
import
*
from
random
import
randint
pygame.init()
window
=
pygame.display.set_mode((
600
,
600
))
clock
=
pygame.time.Clock()
direction_a
=
1
direction_b
=
1
player_rect
=
Rect(
100
,
100
,
50
,
50
)
player_rect2
=
Rect(
200
,
200
,
50
,
50
)
speed_x1
=
5
speed_y1
=
4
speed_x2
=
5
speed_y2
=
4
run
=
True
while
run:
clock.tick(
60
)
if
player_rect.left <
=
20
or
player_rect.right >
=
580
:
direction_a
*
=
-
1
speed_x1
=
randint(
0
,
8
)
speed_y1
=
randint(
0
,
8
)
if
speed_x1
=
=
0
and
speed_y1
=
=
0
:
speed_x1
=
randint(
2
,
8
)
speed_y1
=
randint(
2
,
8
)
if
player_rect.top <
=
20
or
player_rect.bottom >
=
580
:
direction_a
*
=
-
1
speed_x1
=
randint(
0
,
8
)
speed_y1
=
randint(
0
,
8
)
if
speed_x1
=
=
0
and
speed_y1
=
=
0
:
speed_x1
=
randint(
2
,
8
)
speed_y1
=
randint(
2
,
8
)
if
player_rect2.left <
=
20
or
player_rect2.right >
=
580
:
direction_b
*
=
-
1
speed_x2
=
randint(
0
,
8
)
speed_y2
=
randint(
0
,
8
)
if
speed_x2
=
=
0
and
speed_y2
=
=
0
:
speed_x2
=
randint(
2
,
8
)
speed_y2
=
randint(
2
,
8
)
if
player_rect2.top <
=
20
or
player_rect2.bottom >
=
580
:
direction_b
*
=
-
1
speed_x2
=
randint(
0
,
8
)
speed_y2
=
randint(
0
,
8
)
if
speed_x2
=
=
0
and
speed_y2
=
=
0
:
speed_x2
=
randint(
2
,
8
)
speed_y2
=
randint(
2
,
8
)
player_rect.left
+
=
speed_x1
*
direction_a
player_rect.top
+
=
speed_y1
*
direction_a
player_rect2.left
+
=
speed_x2
*
direction_b
player_rect2.top
+
=
speed_y2
*
direction_b
collide
=
pygame.Rect.colliderect(player_rect, player_rect2)
if
collide:
direction_a
*
=
-
1
direction_b
*
=
-
1
pygame.draw.rect(window, (
0
,
255
,
0
), player_rect)
pygame.draw.rect(window, (
0
,
0
,
255
), player_rect2)
pygame.display.update()
window.fill((
255
,
255
,
255
))