def update_snake(ske: Snake): if not ske.dead: if ske.update_counter >= ske.speed: ske.move_snake() ske.update_counter = 0 else: ske.update_counter += 1
def eat(self): self.audio.food() self.eatedApples += 1 if (self.eatedApples >= 10): self.nextLevel() self.isPause = True self.snake = Snake() else: self.snake.addBody() self.setApple()
def restart(self): #clear variables self.isFail = False self.isPause = False self.isWin = False self.eatedApples = 0 #first level self.curLevel = 1 self.level = Level(self.curLevel) #restart game elements self.snake = Snake() self.setApple() #music controller self.audio = Audio()
def __init__(self, curLevel=1): #game elements self.snake = Snake() self.level = Level(curLevel) self.setApple() self.curLevel = curLevel self.eatedApples = 0 #player loose self.isFail = False #pause self.isPause = False #is win game self.isWin = False #music controller self.audio = Audio()
def create_host(): game_name = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(5)) game_name = "abvd" create_game(game_name) res, _ = HostScreen("Your host data", game_name, width, height).run(game_name) # вот здесь надо от юзеров получать змейки и кидать в массив # По идее, надо просто передать змейку хосту и там уже всё запускать snakes = [ Snake(name, width=width, height=height) for name in get_game(game_name)["snakes"] ] for ind, snake in enumerate(snakes): body, direction = body_choice(ind, width, height) if body is not None and direction is not None: snake.body = body snake.head_pos = body[0] snake.direction = direction game = Game(snakes, width=width, height=height, speed=speed, host_addr=game_name) game.scores = [0 for i in game.scores] update_game_var(game_name, "state", game.to_dict()) update_game_var(game_name, "status", PLAYING) play(host=game_name, player_name="HOST")
TILE_WIDTH = 24 TILE_HEIGHT = 24 stuff = { 0: EmptyTile, 1: BorderTile, } for i in range(len(EMPTY_BOARD)): EMPTY_BOARD[i] = [stuff[c]() for c in EMPTY_BOARD[i]] # create objects of the classes snake = Snake(65, 5, "Left", (0, 255, 0, 255)) snake_2 = Snake(1, 7, "Right", (0, 0, 255, 255)) robot = Snake(32, 30, "Up", (255, 0, 0, 255)) SNAKES = [snake, snake_2, robot] game = Game() # copy of the empty board board = deepcopy(EMPTY_BOARD) '''Robot functions''' # front check # list of positions in front of the robot
class Game: def __init__(self, curLevel=1): #game elements self.snake = Snake() self.level = Level(curLevel) self.setApple() self.curLevel = curLevel self.eatedApples = 0 #player loose self.isFail = False #pause self.isPause = False #is win game self.isWin = False #music controller self.audio = Audio() def nextLevel(self): self.audio.passed() self.audio.pause() self.eatedApples = 0 self.curLevel += 1 self.level = Level(self.curLevel) if (not self.level.levelExist): self.isWin = True def changePause(self): self.isPause = not self.isPause if (self.isPause): self.audio.pause() else: self.audio.unpause() def setApple(self): while 1: newApple = Apple(randint(APPLE_SIZE, W - APPLE_SIZE), randint(APPLE_SIZE, H - APPLE_SIZE)) if (not self.level.checkCollisionWith(newApple)): break self.apple = newApple def loose(self): self.isFail = True self.audio.fail() def restart(self): #clear variables self.isFail = False self.isPause = False self.isWin = False self.eatedApples = 0 #first level self.curLevel = 1 self.level = Level(self.curLevel) #restart game elements self.snake = Snake() self.setApple() #music controller self.audio = Audio() def changeStatus(self): if (self.isFail or self.isWin): self.restart() else: self.changePause() self.audio.unpause() def eat(self): self.audio.food() self.eatedApples += 1 if (self.eatedApples >= 10): self.nextLevel() self.isPause = True self.snake = Snake() else: self.snake.addBody() self.setApple() def update(self): if (not self.isFail and not self.isPause): self.snake.update() #check if snake collidarate with apple if (self.snake.rect.colliderect(self.apple.rect)): self.eat() if (self.snake.checkCollision() or self.level.checkCollisionWith(self.snake)): self.audio.pause() self.loose() def draw(self, sc): if (not self.isFail and not self.isWin): self.snake.draw(sc) if (self.apple): self.apple.draw(sc) self.level.draw(sc) self.drawScore(sc) elif (self.isFail): self.drawMessage(sc, 'You loose') else: self.drawMessage(sc, 'YOU WIN!!!') def drawMessage(self, sc, msg): font = pygame.font.SysFont('Arial', 36) font_surf = font.render(msg, 1, (TEXT_COLOR)) font_rect = font_surf.get_rect(center=(W // 2, H // 2)) sc.blit(font_surf, font_rect) pygame.display.update() def drawScore(self, sc): font = pygame.font.SysFont('Arial', 20) font_surf = font.render(str(self.eatedApples), 1, (TEXT_COLOR)) font_rect = font_surf.get_rect(topright=(W - 10, 0)) sc.blit(font_surf, font_rect) pygame.display.update()
def start_game(self): pygame.font.init() snake = Snake((4, 4), (0, 0, 0), (255, 255, 255), (pygame.K_a, pygame.K_d, pygame.K_w, pygame.K_s), 4, 4, menu=Menu()) snake.run_snake()
def run_snake(self): self.app_state = 'snake' snake = Snake((4, 4), (0, 0, 0), (255, 255, 255), (pygame.K_a, pygame.K_d, pygame.K_w, pygame.K_s), 4, 4, menu=None) snake.run_snake()