import
pygame
import
random
pygame.init()
window_width
=
800
window_height
=
600
window
=
pygame.display.set_mode((window_width, window_height))
pygame.display.set_caption(
'抓青蛙游戏'
)
white
=
(
255
,
255
,
255
)
black
=
(
0
,
0
,
0
)
green
=
(
0
,
128
,
0
)
mole_radius
=
20
mole_x
=
random.randint(mole_radius, window_width
-
mole_radius)
mole_y
=
random.randint(mole_radius
+
100
, window_height
-
mole_radius)
ear_radius
=
5
score
=
0
clock
=
pygame.time.Clock()
spawn_timer
=
0
spawn_delay
=
1500
def
show_score():
font
=
pygame.font.SysFont(
None
,
36
)
score_text
=
font.render(f
'Score: {score}'
,
True
, black)
window.blit(score_text, (
10
,
10
))
def
draw_mole():
pygame.draw.circle(window, green, (mole_x, mole_y), mole_radius)
pygame.draw.circle(window, black, (mole_x
-
mole_radius
/
/
2
, mole_y
-
mole_radius
/
/
2
-
ear_radius), ear_radius)
pygame.draw.circle(window, black, (mole_x
+
mole_radius
/
/
2
, mole_y
-
mole_radius
/
/
2
-
ear_radius), ear_radius)
def
show_objects():
window.fill(white)
draw_mole()
pygame.draw.circle(window, black, pygame.mouse.get_pos(),
20
)
show_score()
pygame.display.update()
def
draw_hammer(position):
pygame.draw.circle(window, hammer_color, position, hammer_radius,
3
)
running
=
True
while
running:
for
event
in
pygame.event.get():
if
event.
type
=
=
pygame.QUIT:
running
=
False
spawn_timer
+
=
clock.get_time()
if
spawn_timer >
=
spawn_delay:
mole_x
=
random.randint(mole_radius, window_width
-
mole_radius)
mole_y
=
random.randint(mole_radius
+
100
, window_height
-
mole_radius)
spawn_timer
=
0
mouse_x, mouse_y
=
pygame.mouse.get_pos()
if
pygame.mouse.get_pressed()[
0
]:
if
(mole_x
-
mouse_x)
*
*
2
+
(mole_y
-
mouse_y)
*
*
2
<
=
mole_radius
*
*
2
:
score
+
=
1
mole_x
=
random.randint(mole_radius, window_width
-
mole_radius)
mole_y
=
random.randint(mole_radius
+
100
, window_height
-
mole_radius)
show_objects()
clock.tick(
60
)
pygame.quit()