def gameRestart(self): # Erase the Board self._display_surf = pygame.display.set_mode( self.size, pygame.HWSURFACE | pygame.DOUBLEBUF) self._display_surf.fill(BLACK) self._running = True # Recreate the Snake self.snake = [ Snake(WHITE, 10, 10, 150, 260), Snake(WHITE, 10, 10, 140, 260), Snake(WHITE, 10, 10, 130, 260) ] # Create Score Board self.score = 0 self.displayScore(self.score, 45) # Create Initial Food self.initFood = Food(RED, 10, 10) self._display_surf.blit(self.initFood.image, self.initFood.rect) # set current move to nothing self.move = '' # draw in the boarder for i in range(len(self.boarder)): self._display_surf.blit(self.boarder[i].image, self.boarder[i].rect) # display the initial Snake array for i in range(len(self.snake)): self._display_surf.blit(self.snake[i].image, self.snake[i].rect) pygame.display.update()
def eatFood(self): # Create a new Food at random location and display it self.initFood = Food(RED, 10, 10) self._display_surf.blit(self.initFood.image, self.initFood.rect) # Create Score Board self.score += 1 self.displayScore(self.score, 45) # for i in range(len(self.snake)): # self._display_surf.blit(self.snake[i].image, self.snake[i].rect) # # Store the last and second to last blocks of the snake lastSnakeBlock = self.snake[-1] secondToLastBlock = self.snake[-2] # if the last two blocks are on the same horizontal line and the last block is to the left of the # second to last block, add a block to the left side of the last block if lastSnakeBlock.rect.y == secondToLastBlock.rect.y and lastSnakeBlock.rect.x < secondToLastBlock.rect.x: newX = lastSnakeBlock.rect.x - 10 newSnakeBlock = Snake(lastSnakeBlock.color, lastSnakeBlock.width, lastSnakeBlock.height, newX, lastSnakeBlock.rect.y) self.snake.append(newSnakeBlock) # if the last two blocks are on the same horizontal line and the last block is to the right of the # second to last block, add a block to the right side of the last block if lastSnakeBlock.rect.y == secondToLastBlock.rect.y and lastSnakeBlock.rect.x > secondToLastBlock.rect.x: newX = lastSnakeBlock.rect.x + 10 newSnakeBlock = Snake(lastSnakeBlock.color, lastSnakeBlock.width, lastSnakeBlock.height, newX, lastSnakeBlock.rect.y) self.snake.append(newSnakeBlock) # if the last two blocks are on the same vertical line and the last block is above the # second to last block, add a block above the last block if lastSnakeBlock.rect.x == secondToLastBlock.rect.x and lastSnakeBlock.rect.y < secondToLastBlock.rect.y: newY = lastSnakeBlock.rect.y - 10 newSnakeBlock = Snake(lastSnakeBlock.color, lastSnakeBlock.width, lastSnakeBlock.height, lastSnakeBlock.rect.x, newY) self.snake.append(newSnakeBlock) # if the last two blocks are on the same vertical line and the last block is below the # second to last block, add a block below the last block if lastSnakeBlock.rect.x == secondToLastBlock.rect.x and lastSnakeBlock.rect.y > secondToLastBlock.rect.y: newY = lastSnakeBlock.rect.y + 10 newSnakeBlock = Snake(lastSnakeBlock.color, lastSnakeBlock.width, lastSnakeBlock.height, lastSnakeBlock.rect.x, newY) self.snake.append(newSnakeBlock) for i in range(len(self.snake)): self._display_surf.blit(self.snake[i].image, self.snake[i].rect)
def __init__(self): self._running = True self._display_surf = None self.size = self.weight, self.height = 600, 700 # create the boarder self.boarder = self.generateBoard() # Initial Snake array with 3 Snake Blocks starting at (50, 50) and going left self.snake = [ Snake(WHITE, 10, 10, 150, 260), Snake(WHITE, 10, 10, 140, 260), Snake(WHITE, 10, 10, 130, 260) ]
def __init__(self, wsize): pygame.init() self.surface = pygame.display.set_mode(wsize) self.map = Map.Map() self.player = Player.Player() self.info = Info.Info(self.player.lifes, 0) self.clock = pygame.time.Clock() self.background = pygame.image.load( "Assets/main_menu_bg.png").convert() self.level = self.map.load_level() self.handle = True self.next_s = 1 self.creatures = [Fly.Fly([1120, 288]), Snake.Snake([1408, 352])]
def generateBoard(self): boardCorners = [] boardTop = [] boardSide1 = [] boardSide2 = [] boardBottom = [] # Makes (0,0) of board = (100, 210) # top left corner boardCorners.append(Snake(CYAN, 10, 10, 90, 200)) # top right corner boardCorners.append(Snake(CYAN, 10, 10, 500, 200)) # bottom left corner boardCorners.append(Snake(CYAN, 10, 10, 90, 610)) # bottom right corner boardCorners.append(Snake(CYAN, 10, 10, 500, 610)) # top and bottom sides topCoord = 100 for i in range(40): boardTop.append(Snake(CYAN, 10, 10, topCoord, 200)) boardBottom.append(Snake(CYAN, 10, 10, topCoord, 610)) topCoord += 10 # sides of board sideCoord = 210 for i in range(40): boardSide1.append(Snake(CYAN, 10, 10, 90, sideCoord)) boardSide2.append(Snake(CYAN, 10, 10, 500, sideCoord)) sideCoord += 10 # combine all parts allBoarder = boardCorners + boardTop + boardSide1 + boardSide2 + boardBottom # return list of blocks return allBoarder
from Classes import Snake, Food from SetupScreen import WINDOW_HEIGHT, WINDOW_WIDTH, setup_window import curses W = setup_window() S = Snake(Window=W) F = Food(Window=W, Snake=S) i = 0 while True: i += 1 W.clear() W.border(0) # Write the game score on the screen W.addstr(WINDOW_HEIGHT - 1, int(WINDOW_WIDTH * 1 / 10), S.game_score) W.getch() if i > 10: break print("{}".format(S.head_coords)) curses.endwin()