def display_pause_message(): board_size = board.size() pause_line_1 = message_display( "Paused".upper(), screen, Position(board_size.width // 2, board_size.height // 2), white) message_display( "(Click to draw, press 'p' to continue, 'h' for rules/about)", screen, Position(board_size.width // 2, board_size.height // 2 + pause_line_1.height), white)
def menu(self): sb.gameDisplay.fill(sb.white) message_display("Welcome", sb.xres / 2, sb.yres / 2 + 100, 25, sb.gameDisplay) message_display("Press Enter to play or Esc to exit.", sb.xres / 2, sb.yres / 2 + 50, 25, sb.gameDisplay) pygame.display.update() for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_RETURN: self.showMenu = False self.gameRun = True if event.key == pygame.K_ESCAPE: pygame.quit() quit()
def draw_status_bar(self, screen): from message import Button pygame.display.set_caption(f"Conway's Game of Life") status_bar = pygame.Surface( (self.__size.width, self.status_bar_height)) status_bar.fill(dead_color) self.status_bar = status_bar self.pause_button() self.clear_button() self.randomize_button() message.message_display( f"Generation: {self._generation}", status_bar, Position(status_bar.get_rect().width, status_bar.get_rect().height), white) screen.blit(self.status_bar, (0, self.size().height))
def screen_update(): clock = pygame.time.Clock() sb.gameDisplay.blit(sb.backgroundImage, (0, 0)) pygame.draw.rect( sb.gameDisplay, sb.black, (sb.playerXpos, sb.playerYpos, sb.playerWidth, sb.playerHeight)) pygame.draw.circle(sb.gameDisplay, sb.black, (int( (2 * sb.playerXpos + sb.playerWidth) / 2), sb.playerYpos), sb.ballRadius) pygame.draw.rect(sb.gameDisplay, sb.black, (sb.oppoXpos, sb.oppoYpos, sb.oppoWidth, sb.oppoHeight)) pygame.draw.circle( sb.gameDisplay, sb.black, (int((2 * sb.oppoXpos + sb.oppoWidth) / 2), sb.oppoYpos + sb.oppoHeight), sb.ballRadius, ) pygame.draw.circle( sb.gameDisplay, sb.black, (int(sb.ballXpos), int(sb.ballYpos)), sb.ballRadius, ) pygame.draw.rect(sb.gameDisplay, sb.red, (0, 0, sb.xres, sb.oppoYpos)) pygame.draw.rect(sb.gameDisplay, sb.green, (0, sb.playerYpos + sb.playerHeight, sb.xres, sb.yres)) oppoScoreText = "Opponent: " + str(sb.oppoScore) playerScoreText = "Player: " + str(sb.playerScore) message_display(oppoScoreText, 50, sb.oppoYpos / 2, 15, sb.gameDisplay, sb.white) message_display(playerScoreText, 50, sb.xres - 220, 15, sb.gameDisplay, sb.black) clock.tick(60) pygame.display.update()
def crash(self, display): message_display("Car Crashed", display)
def game_loop(display): #all the function are called using this function global maxTime bkgy = 0 itMoved = False # Indice de la lista de direcciones en ejecución que ira aumentando para acceder a la siguiente lista (Genoma) currentDirection = 0 # Indice del obstáculo que irá aumentando para acceder al siguiente obstáculo obstaclesCounter = 0 # Matriz de tiempos donde cada elemento es lo que tarda en terminar el recorrido # una determinada lista de direcciones en una generación #timeList: rows are generations, columns are genomas timeList = [[0.0 for _ in range(sizePopulation)]] # Generación inicial currentGeneration = 0 time = 0 # Generamos la lista de obstaculos obstacleList = generateObstacles() sizeObstacles = len(obstacleList) # Tamaño de la lista de obstaculos # Creamos la población de direcciones inicial currentListDirections = [] currentListDirections.append(populate(sizeObstacles, sizePopulation)) #iteration for every genoma bumped = False while not bumped: #game is started for event in pygame.event.get(): #if any input is given if event.type==pygame.QUIT: #if quit input is given #bumped=True #game is stopped pygame.quit() quit() rel_y = bkgy % 1200 display.blit(bkg, (0, rel_y - 1200)) if rel_y < 600: display.blit(bkg, (0, rel_y)) bkgy += 10 time += 0.1 if currentDirection < sizePopulation: #MAX TIME if max(timeList[currentGeneration]) > maxTime: maxTime = math.trunc(max(timeList[currentGeneration])) if obstaclesCounter == sizeObstacles: timeList[currentGeneration][currentDirection] = time print("Generación: " + str(currentGeneration + 1)) print("Dirección (Genoma): " + str(currentDirection + 1)) print("Tiempo(Genoma): " + str(timeList[currentGeneration][currentDirection])) print("Tiempo maximo: " + str(maxTime)) print("obstaculos: " + str(obstaclesCounter)) time = 0 currentDirection += 1 obstaclesCounter = 0 obstacleList = generateObstacles() sizeObstacles = len(obstacleList) #Previous generation ended else: #MAX TIME if max(timeList[currentGeneration]) > maxTime: maxTime = math.trunc(max(timeList[currentGeneration])) #SELECTION HERE global parents parents = selection(sizePopulation, timeList[currentGeneration]) #CROSSOVER #newGenerationDirections = populate(sizeObstacles, sizePopulation) currentListDirections.append(crossover(parents, sizePopulation, currentListDirections[currentGeneration] )) currentGeneration += 1 #MUTATION global mutationRate currentListDirections[currentGeneration] = mutation(sizePopulation, currentListDirections[currentGeneration], mutationRate) #print(timeList[currentGeneration][currentDirection]) timeList.append([0.0 for _ in range(sizePopulation)]) time = 0 currentDirection = 0 obstaclesCounter = 0 obstacleList = generateObstacles() sizeObstacles = len(obstacleList) ########### MOVE CONDITION FOR THE CAR ########### if not itMoved: itMoved = carMoves(obstacleList[obstaclesCounter], theCar, currentListDirections[currentGeneration][currentDirection][obstaclesCounter]) ################################ ############# DISPLAYS ############# #background(display) #Para que me explique pepino la utilidad obstacleList[obstaclesCounter].y -= (obstacleList[obstaclesCounter].speed/4) obstacleList[obstaclesCounter].draw(display) obstacleList[obstaclesCounter].y += obstacleList[obstaclesCounter].speed generation_message_display("Generación: " + str(currentGeneration + 1), display) genoma_message_display("Genoma: " + str(currentDirection + 1), display) time_message_display("Tiempo: " + str(math.trunc(time)), display) maxtime_message_display("Tiempo máximo: " + str(maxTime), display) theCar.draw(display) ################################ ######## PASSED OBSTACLE WITHOUT CRASH ######## if obstacleList[obstaclesCounter].y > 600: obstaclesCounter += 1 itMoved = False if obstaclesCounter >= len(obstacleList): message_display("You Win", display) time.sleep(20) continue #TERMINAR AQUI ################################ #COLOCAR CONDICIÓN DE CREACIÓN DE GENERACIÓN SIGUIENTE ####### CRASH CONDITIONS ####### # Car crash with one of the asfalt limits crashedWithAsfalt = crashWithAsfaltLimit(theCar, asfaltLeftLimitX, asfaltRightLimitX) # Car crash with the obstacle crashedWithObstacle = crashWithObstacle(obstacleList[obstaclesCounter], theCar) #detect crashes if crashedWithAsfalt or crashedWithObstacle: theCar.crash(display) theCar.setX(475) itMoved = False if currentDirection == sizePopulation: """ ESTO NUNCA SE EJECUTA #continue with next generation print(timeList[currentGeneration][currentDirection]) timeList.append([0.0 for _ in range(sizePopulation)]) currentDirection = 0 obstaclesCounter = 0 currentGeneration += 1 newGenerationDirections = populate(sizeObstacles, sizePopulation) currentListDirections = newGenerationDirections time = 0 """ else: #continue with next genome timeList[currentGeneration][currentDirection] = time print("Generación: " + str(currentGeneration + 1)) print("Dirección (Genoma): " + str(currentDirection + 1)) print("Tiempo que tarda: " + str(timeList[currentGeneration][currentDirection])) print("Tiempo maximo: " + str(maxTime)) print("obstaculos: " + str(obstaclesCounter)) obstaclesCounter = 0 currentDirection += 1 time = 0 obstacleList = generateObstacles() sizeObstacles = len(obstacleList) crashedWithAsfalt = False crashedWithObstacle = False continue ################################ pygame.display.update() #update the display