def __init__(self, gameSettings = None): ### --------- initialise pygame and set up the window pygame.init() pygame.display.set_caption(Config.PAGE_TITLE) Config.screen.fill(Config.BACKGROUND_COLOUR) self.gameScore = 0 self.userEscape = False # User ends game by ESC self.gameOver = False # Game over by death self.freezeActiveBallsTimer = 0 self.clock = pygame.time.Clock() ### --------- Generate the snake self.snake = Snake_Module.Snake() self.snakeSprite = pygame.sprite.Group() self.snakeSprite.add(self.snake.get_sections()) # needed for clear/draw self.snakeSections = self.snake.get_sections() ### --------- Initial food self.foodGroup = pygame.sprite.Group() # config['food']['standard']['initial'] for n in range(Config.INITIAL_FOOD_NUM): self.foodGroup.add(Food_Module.make_food(self.snake)) for n in range(Config.INITIAL_FOOD_SUPER_NUM): self.foodGroup.add(Food_Module.make_food(self.snake,'FoodSuper')) for n in range(Config.INITIAL_FOOD_MYSTERIOUS_NUM): self.foodGroup.add(Food_Module.make_food(self.snake, 'FoodMysterious')) for n in range(Config.INITIAL_FOOD_CURSE_NUM): self.foodGroup.add(Food_Module.make_food(self.snake, 'FoodCurse')) ### --------- Initial balls self.ballGroup = pygame.sprite.Group() for n in range(Config.INITIAL_BALL_NUM): self.ballGroup.add(Ball_Module.BallStandard(HelperFunctions.generateSafeXY(self.snake, self.ballGroup, None, self.foodGroup))) self.ballKillerGroup = pygame.sprite.Group() if Config.INITIAL_BALL_KILLER_NUM != 0: self.ballKillerGroup.add(Ball_Module.BallKiller(HelperFunctions.generateSafeXY(self.snake, self.ballGroup, None, self.foodGroup)))
def handleRandoms(self): time = pygame.time.get_ticks() if ((time % Config.RANDOM_FOOD_MYSTERIOUS_CHANCE) == 0): print "Random Melon of Mystery generated" self.foodGroup.add(Food_Module.FoodMysterious(None, None, None, HelperFunctions.generateSafeXY(self.snake, self.ballGroup, self.ballKillerGroup, self.foodGroup))) if ((time % Config.RANDOM_FOOD_CURSE_CHANCE) == 0): print "Random Berries of Bane generated" self.foodGroup.add(Food_Module.FoodCurse(None, None, None, HelperFunctions.generateSafeXY(self.snake, self.ballGroup, self.ballKillerGroup, self.foodGroup)))
def handleCollisions(self): snakeHead = self.snake.get_head() #### FOOD ballKiller = killerFromGroup(self.ballKillerGroup) collisionsFood = pygame.sprite.spritecollide(snakeHead, self.foodGroup, True) if collisionsFood: # get the collided food item, then recreate it in a new random, position properties = collisionsFood[0].get_properties() if properties['autoRespawn']: self.foodGroup.add(Food_Module.make_food(self.snake, properties['type'], properties['colour'], properties['size'], properties['effect'])) if properties['effect']['curse'] == True: self.snake.curse_tail() if properties['effect']['spawnKiller']: # 40% chance t = pygame.time.get_ticks() if self.ballKillerSpawned() == False and (t % 5 == 0 or t % 5 == 1): # maybe make the stimulus for a killer ball to be spawned more complicated self.ballKillerGroup.add(Ball_Module.BallKiller(HelperFunctions.generateSafeXY(self.snake, self.ballGroup, ballKiller, self.foodGroup))) if properties['effect']['spawnStandard']: if len(self.ballGroup) + 1 < Config.MAX_BALLS: self.ballGroup.add(Ball_Module.BallStandard(HelperFunctions.generateSafeXY(self.snake, self.ballGroup, ballKiller, self.foodGroup))) if properties['effect']['freezeBall'] > 0: # then no hard coded values, works based on the FPS *not* faster/slower hardware values self.freezeActiveBallsTimer += Config.FPS * properties['effect']['freezeBall'] print "Balls Frozen for " + str(self.freezeActiveBallsTimer) if properties['effect']['removeStandard']: print "Removed ball" # bad way of removing the first element, but it works and isn't too complicated for b in self.ballGroup: self.ballGroup.remove(b) break if properties['effect']['removeKiller']: print "Removed killer ball" if self.ballKillerSpawned(): self.ballKillerGroup.empty() self.snake.adjust_tail_size(properties['effect']['size']) # generate the score based on the score base per the food, how long is left on the food before it expires, # and then divide it by the FPS (as time is in FPS), then apply difficulty bonus _S = (float) ((properties['effect']['score'] * properties['time'])) self.gameScore += int((round(_S / Config.FPS)) * Config.DIFFICULTY_BONUS) self.setWindowTitle(str(self.gameScore)) # update these so we can then redraw them later - only update once we've got a larger snake, otherwise we're wasting CPU! self.snakeSprite = pygame.sprite.Group() self.snakeSprite.add(self.snake.get_sections()) self.snakeSections = self.snake.get_sections() # only work on collisions with ballKiller if we've got one in play # if len(self.ballKillerGroup) > 0: ballKiller = killerFromGroup(self.ballKillerGroup) if ballKiller != None: collisionsFoodKiller = pygame.sprite.spritecollide(ballKiller, self.foodGroup, True) if collisionsFoodKiller: for f in collisionsFoodKiller: properties = collisionsFoodKiller[0].get_properties() if properties['autoRespawn']: self.foodGroup.add(Food_Module.make_food(self.snake, properties['type'], properties['colour'], properties['size'], properties['effect'])) # don't remove balls, just bounce collisionsBallsKiller = pygame.sprite.spritecollide(ballKiller, self.ballGroup, False) if collisionsBallsKiller: for b in collisionsBallsKiller: b.collision(ballKiller) # handle this more quickly allBalls = pygame.sprite.Group() allBalls.add(self.ballGroup) allBalls.add(self.ballKillerGroup) if len(allBalls) > 0: collisionsBall = pygame.sprite.spritecollide(snakeHead, allBalls, False) if collisionsBall: self.gameOver = True