-
Notifications
You must be signed in to change notification settings - Fork 28
/
Food.py
40 lines (28 loc) · 1.06 KB
/
Food.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
import random
import pygame
class Food(object):
"""
A Food object.
Members:
x, y: store the position of the food.
size: size of the food.
"""
def __init__(self, x, y, size=10):
""" Initialize block at given x, y position """
self.x = x
self.y = y
self.size = size
def generate_food(self, width, height):
""" Generate food at random location within the width and height """
self.x = round(random.randrange(0, width-210) / 10.0) * 10
self.y = round(random.randrange(0, height) / 10.0) * 10
return (self.x, self.y)
def draw(self, game_display, color):
""" Draws the food at x y position """
pygame.draw.rect(game_display, color, (self.x, self.y, self.size, self.size))
def get_pos(self):
""" Returns the position of food as a tuple """
return (self.x, self.y)
def get_rect(self):
""" Return rectangle object of the food for collision detection """
return pygame.Rect(self.x, self.y, self.size, self.size)