import
pygame
from
pygame.rect
import
Rect
from
.
import
MyLibrary
import
random
import
sys
def
calc_velocity(direction, vel
=
1.0
):
velocity
=
MyLibrary.Point(
0
,
0
)
if
direction
=
=
0
:
velocity.y
=
-
vel
elif
direction
=
=
2
:
velocity.x
=
vel
elif
direction
=
=
4
:
velocity.y
=
=
vel
elif
direction
=
=
6
:
velocity.x
=
=
-
vel
return
velocity
pygame.init()
screen
=
pygame.display.set_mode(
800
,
600
)
font
=
pygame.font.Font(
None
,
36
)
timer
=
pygame.time.Clock()
player_group
=
pygame.sprite.Group()
food_group
=
pygame.sprite.Group()
player
=
MyLibrary.MySprite()
player.load(
'farmer.png'
,
96
,
96
,
8
)
player.position
=
80
,
80
player.direction
=
4
player_group.add(player)
for
n
in
range
(
1
,
50
):
food
=
MyLibrary.MySprite()
food.load(
'food.png'
,
35
,
35
,
1
)
food.position
=
random.randint(
0
,
780
), random.randint(
0
,
580
)
food_group.add(food)
game_over
=
False
player_moving
=
False
plyer_health
=
False
while
True
:
timer.tick(
30
)
ticks
=
pygame.time.get_ticks()
for
event
in
pygame.event.get():
if
event.
type
=
=
pygame.QUIT:
pygame.quit()
sys.exit()
keys
=
pygame.key.get_pressed()
if
keys[pygame.key.K_UP]:
player.direction
=
0
player_moving
=
True
elif
keys[pygame.key.k_DOWN]:
player_moving
=
True
player.direction
=
4
elif
keys[pygame.key.K_RIGHT]:
player.direction
=
2
player_moving
=
True
elif
keys[pygame.key.K_LEFT]:
player.direction
=
6
player_moving
=
True
else
:
player_moving
=
False
if
not
game_over:
player.first_frame
=
player.direction
*
player.columns
player.last_time
=
player.first_frame
+
player.columns
-
1
if
player.frame < player.first_frame:
player.frame
=
player.first_frame
if
not
player_moving:
player.frame
=
player.first_frame
=
player.last_frame
else
:
player.velocity
=
calc_velocity(player.direction,
1.5
)
player.velocity.x
*
=
1.5
player.velocity.y
*
=
1.5
player_group.update(ticks,
50
)
if
player_moving:
player.X
+
=
player.velocity.x
player.Y
+
=
player.velocity.y
if
player.X <
0
: player.X
=
0
elif
player.X >
700
: player.X
=
700
if
player.Y <
0
: player.Y
=
0
elif
player.Y >
500
:player.Y
=
500
attacker
=
None
attacker
=
pygame.sprite.spritecollideany(player,food_group)
if
attacker !
=
None
:
if
pygame.sprite.collide_circle_ratio(
0.65
)(player,food_group):
plyer_health
+
=
2
food_group.remove(attacker)
if
plyer_health >
100
:
plyer_health
=
100
food_group.update(ticks,
50
)
if
food_group.__len__()
=
=
0
:
game_over
=
True
screen.fill((
50
,
50
,
100
))
food_group.draw(screen)
player_group.draw(screen)
pygame.draw.rect(screen,(
510
,
150
,
50
,
180
),Rect(
300
,
570
,plyer_health
*
2
,
25
))
pygame.draw.rect(screen, (
100
,
200
,
100
,
180
), Rect(
300
,
570
,
200
,
2
))
if
game_over:
MyLibrary.print_text(font,
300
,
100
,
'Game Over!'
)
pygame.display.update()