def begin(self): self.initEntities() while self.running: # step = step + 1 for event in pygame.event.get(): if event.type == pygame.QUIT: self.running = False if event.type == pygame.KEYDOWN: key = event.dict['key'] if key == 32 or key == 271 or key == 13: self.updating = True if event.type == pygame.KEYUP: if event.dict['key'] == 32: self.updating = False if event.type == pygame.MOUSEBUTTONDOWN: if event.dict['button'] == 1 and checkBoundaryList( event.dict['pos'], self.bounds): self.addFood(Food(startingpos=event.dict['pos'])) if event.dict['button'] == 3: if self.foodgrowing: self.foodgrowing = False else: self.foodgrowing = True if not self.checkgameover() and self.updating: self.spawnfood() self.updateEntitites() self.updateSurface() self.clock.tick(self.tickrate)
def __init__(self): GuiState.__init__(self) gameWorld = World() definition.apply() for i in xrange(self.familes): q = Drone(gameWorld) fam = q.getFamily() home = q.getPosition() f = Food(gameWorld, None, randomizePosition(home, 100)) foodFam = f.getFamily() for j in xrange(10): Drone(gameWorld, fam, randomizePosition(home, 50)) for j in xrange(15): Food(gameWorld, foodFam, randomizePosition(home, 100)) self.add(gameWorld)
def get_all_food(): """ Get all food in the database :return: A JSON structure in the form of {"food":[<list of JSON objects representing food records in the database and their nutrition facts>]} """ all_food = Food().all() for food in all_food: food['nutrition'] = NutritionalFact().find_by_id(food['fk_nfact_id']) or dict() return jsonify({"food": all_food})
def test_cooking_new_food(): food = Food(surface) for i in range(0, 10): oldPosition = food.get_food_location() food.update_food() pygame.display.flip() assert food.get_food_location() != oldPosition
def get_food_by_name(food_name): """ Get all foods that have the food name :param food_name: The food name to search for :return: A JSON structure in the form of {"food":[<list of JSON objects representing food records in the database and their nutrition facts>]} """ food = Food().find_by_attribute("food_name", food_name, limit=-1) if not food: return jsonify({"error": "No food with name {} found".format(food_name)}), 404 for f in food: f['nutrition'] = NutritionalFact().find_by_id(f['fk_nfact_id']) or dict() return jsonify({"food": food})
def get_food_by_id(id): """ Get a single food record by its id :param id: The numeric id of the food record to find :return: A JSON object representation of the food record along with its nutritional facts """ food = Food().find_by_id(id) if not food: return jsonify({"error": "No food with id {} found".format(id)}), 404 food['nutrition'] = NutritionalFact().find_by_id(food['fk_nfact_id']) or dict() return jsonify(food)
def __init__(self, surface, username, numberOfPlayers, soundsOn): # Initialize variables self.__clock = pygame.time.Clock() self.__surface = surface self.__isRunning = True self.__username = username self.__numberOfPlayers = numberOfPlayers self.__soundsOn = soundsOn # Initialize the game entities self.__ball = Ball(self.__surface) self.__score = self.__ball.get_score() self.__playfield = Playfield(self.__surface, self.__username, self.__score) self.__surface = self.__playfield.get_surface() self.__snake = Snake(self.__surface, self.__isRunning) self.__food = Food(self.__surface) # Check if multiplayer or not if self.__numberOfPlayers == 0: self.__paddle = Paddle(self.__surface) else: self.__paddle = Paddle(self.__surface, True)
def preloadFoodSprites(): foodspritelist = {} basedir = 'sprites\\foods' iconlist = os.listdir(basedir) # returns list basesize = (3, 3) for icon in iconlist: foodtypename = ''.join(icon.split('.')[0:-1]) try: foodtype = getattr(Foodtype, foodtypename) except: continue amount, _ = Food()._getfoodatts(foodtype) size = [el * amount for el in basesize] foodspritelist[foodtype] = pygame.transform.scale( pygame.image.load(basedir + '\\' + icon), size) return foodspritelist
def test_creating_food(): food = Food(surface) assert food.get_food_location() != None
class Game: def __init__(self, surface, username, numberOfPlayers, soundsOn): # Initialize variables self.__clock = pygame.time.Clock() self.__surface = surface self.__isRunning = True self.__username = username self.__numberOfPlayers = numberOfPlayers self.__soundsOn = soundsOn # Initialize the game entities self.__ball = Ball(self.__surface) self.__score = self.__ball.get_score() self.__playfield = Playfield(self.__surface, self.__username, self.__score) self.__surface = self.__playfield.get_surface() self.__snake = Snake(self.__surface, self.__isRunning) self.__food = Food(self.__surface) # Check if multiplayer or not if self.__numberOfPlayers == 0: self.__paddle = Paddle(self.__surface) else: self.__paddle = Paddle(self.__surface, True) def start_game(self): while self.__isRunning: # Check if game is being closed manually for event in pygame.event.get(): if event.type == pygame.QUIT: self.__isRunning = False # Check if game is done if self.__snake.game_over(): self.__isRunning = False mixer.music.stop() self.__play_gameover_sound() Tk().wm_withdraw() # Pop-up screen using tkinter library messagebox.showinfo( 'GAME OVER - You a dead snake bruv', 'I admit that I touched myself/walls :\'(') # Check if snake eats the food elif self.__snake.get_head().colliderect(self.__food.show_food()): self.__snake.set_length() self.__play_touch_sound() self.__food.update_food() # Check if the paddle touches the ball elif self.__paddle.get_paddle().colliderect( self.__ball.get_ball()): self.__ball.bounce() self.__play_touch_sound() # Check if one of the segments of the snake touches the ball for segment in self.__snake.get_snake(): if segment.colliderect(self.__ball.get_ball()): self.__ball.bounce() self.__play_touch_sound() # Continously update the game with the new values/info self.__update_game() def __update_game(self): self.__surface.fill((0, 0, 0)) self.__score = self.__ball.get_score() Playfield(self.__surface, self.__username, self.__score) self.__snake.update_snake() self.__food.look_for_food() self.__paddle.update_paddle() self.__ball.update_ball() pygame.display.flip() self.__clock.tick(60) # Check if sounds are activated or not and then play them accordingly def __play_touch_sound(self): if self.__soundsOn: touch = mixer.Sound("assets/touch_sound.wav") mixer.Sound.play(touch) def __play_gameover_sound(self): if self.__soundsOn: gameover = mixer.Sound("assets/game_over_sound.wav") mixer.Sound.play(gameover) # Code for testing the Game class def get_username(self): return self.__username def get_numberOfPlayers(self): return self.__numberOfPlayers
def createFoods(self): for _ in range(self.startingfoodcount): self.addFood(Food(startingpos=self.getRandomPos())) pass
def spawnfood(self): self.foodrespawncountdown -= 1 if self.foodrespawncountdown == 0: self.foodrespawncountdown = self.foodrespawntickperiod if len(self.foodlist) < self.spawnedfoodcap and self.foodgrowing: self.addFood(Food(startingpos=self.getRandomPos()))
def food_update_create(): """ Take in a JSON object in the form of { "food": [ { "food_id": 1, "food_name": "Chicken and Rice", "in_fridge": <boolean value>, "fk_nfact_id": <integer equal to this food's nutritional fact record>, "nutrition": <a JSON object with similar structure to the nutritional_fact schema> }, ... ] } Where for each entry in "food" if there is a "food_id" attribute, it is assumed that we are updating an existing food (because we wouldn't have the id otherwise), and the values given will update the record and its ingredients links in the database. Otherwise, if there is no "food_id" for an object, the system will assume the values given are for a new record in the database and will create a new record. :return: A JSON object of {"food":[<list of JSON objects representing food items updated or created>]} """ if not request.json or len(request.json) == 0: return jsonify({"error": "No JSON supplied"}), 400 id_column = Food.__keys__[0] ret_val = [] f = Food() j = request.json if not j.get('food', None): return jsonify({"error": "Invalid schema"}), 400 for food in j['food']: food_id = food.get(id_column, None) if food_id: f.find_by_id(food_id) f.update(**food) f.flush() else: f.create(**food) if j.get('nutrition', None): try: f.nutrition = j['nutrition'] except TypeError: return jsonify({"error": "Nutrition entries must be objects similar to nutritional_fact schema"}), 400 ret_val.append(deepcopy(f.data)) return jsonify({"food": ret_val})
def fridge(): """ Gets all food records that have their in_fridge attribute set to true :return: A JSON object of {"fridge": [<a list of food records in the form of JSON objects>]} """ return jsonify({"fridge": [food for food in Food().all(comparisons={"in_fridge": ["=", True]})]})