def plotcv(self, unit):#, id, seed): scale = 20 img = draw.drawBoard(self.width, self.height, scale) for cell in self.filled: draw.drawCell(img, (255,0,0), cell.x, cell.y, scale) for cell in unit.members: draw.drawCell(img, (0,0,255), cell.x, cell.y, scale) draw.drawPivot(img, (0,255,0), unit.pivot.x, unit.pivot.y, scale) #name = 'Maps/Map_24_Units/' + str(id) + '.png' #cv2.imwrite(name, img) k = '0' """ 1: south-west 3: south-east 4: west 5: nothing 6: east 7: rotate counterclockwise 9: rotate clockwise q: exit """ while not (k in [ord('1'),ord('3'),ord('4'),ord('5'),ord('6'),ord('7'),ord('9'),ord('q')]): cv2.imshow('board', img) k = cv2.waitKey(1) return k
def main(): userInput = input( "Enter 5 or greater for a board of that size, or 0 to randomize the board. " ) gameBoard = board.Board( userInput) # builds a Board() object and assigns it to gameBoard bfs = evaluate.evaluate( gameBoard.boardBuilt, gameBoard.boardSize) # creates the bfs object for the original board hillBoard = hill.Hill( gameBoard) # creates a hill-climbing object with the original board aStar = AStarEval.AStarEval( gameBoard.boardBuilt, gameBoard.boardSize) # create an A* object for the original board gene = genetic.Genetic(gameBoard) print("Score for the board: " + str(bfs.value) + "; time for BFS (ms): " + str(bfs.evalTime)) # prints the score for the original board # create a loop for user input to make selections to view the original board, etc while True: print() userInput = input( "Type '1' for the original board, '2' for number of steps on original board, '3' to implement Hill Climbing, '4' to view the Hill Board, '5' to view steps on Hill Board, " + "'6' to view A* score, 'q' to exit. ") if userInput == '1': draw.drawBoard( gameBoard.boardBuilt, gameBoard.boardSize ) # takes the created board and draws it with turtle, as visual output print("> Original game board displayed. Score: " + str(bfs.value)) elif userInput == '2': draw.drawBoard(bfs.steps, gameBoard.boardSize) # uses the draw module print("> Number of steps to reach a tile displayed.") elif userInput == '3': userInput = int( input( "> How many iterations would you like the hill-climbing to try? " )) while userInput < 0: userInput = input( "> How many iterations would you like the hill-climbing to try? " ) hillBoard.climb(userInput) print("> New board score: " + str(hillBoard.score) + "; time for hill-climb (ms): " + str(hillBoard.evalTime)) elif userInput == '4': draw.drawBoard( hillBoard.puzzle.boardBuilt, gameBoard.boardSize ) # takes the created board and draws it with turtle, as visual output print("> Hill-CLimbing game board displayed. Score: " + str(hillBoard.score)) elif userInput == '5': hillBFS = evaluate.evaluate( hillBoard.puzzle.boardBuilt, hillBoard.puzzle.boardSize ) # creates a new bfs object for the hill-climbing board draw.drawBoard(hillBFS.steps, hillBoard.puzzle.boardSize) print("> Hill-Climbing board number of steps displayed") elif userInput == '6': #draw.drawBoard(aStar.steps, gameBoard.boardSize) print("> A* steps displayed. \nBFS Score, Time: " + str(bfs.value) + ", " + str(bfs.evalTime) + "\nA* Score, Time: " + str(aStar.value) + ", " + str(aStar.evalTime)) elif userInput == '7': userInput = int( input("> How many iterations would you like genetic to try? ")) while userInput < 0: userInput = input( "> How many iterations would you like genetic to try? ") gene.run(bfs, userInput) print(gene.score) elif userInput == 'q': break
hover_chip = Chip(board.turn) all_hover_chips.add(hover_chip) #sets mouse to invisible pygame.mouse.set_visible(0) #sets done to false done = False #sets clock clock = pygame.time.Clock() #loops until done while not done: for event in pygame.event.get(): draw.drawBoard(screen, light_orange, black) if event.type == pygame.QUIT: done = True if event.type == pygame.MOUSEBUTTONDOWN: if board.win != True: try: #try to place a chip if mouse click is detected and win is not true board.placeChip(board.turn) except: #passes if chip is placed out of range pass else: #if mouse click is detected and someone has won... #gets mouse position
from pygame.locals import * from time import sleep import draw import ships import control import pygame import sys owncolor = draw.OWNSHIPCOLOR enemycolor = draw.ENEMYSHIPCOLOR draw.drawBoard() draw.drawBoard(360) ownship = ships.workShip() enemyship = ships.workShip() OwnListShip = ownship.createListShips() EnemyListShip = enemyship.createListShips() draw.drawAllShip(ownship.korabli, owncolor, 'own') #draw.drawAllShip(enemyship.korabli, enemycolor, 'enemy') for i in enemyship.korabli: print i.cells while True: for event in pygame.event.get(): if event.type == QUIT: pygame.quit()
def runOnce(): ROWS_CLEARED = 0 # Define the colors we will use in RGB format black = [ 0, 0, 0] white = [255,255,255] blue = [ 0, 0,255] green = [ 0,255, 0] red = [255, 0, 0] # Import a library of functions called 'pygame' import pygame from boardClass import board from draw import drawBoard from getRandomPiece import writeToFile # Initialize the game engine pygame.init() # Set the height and width of the screen NUMROWS = 25 NUMCOLS = 10 SQUARE_SIZE = 25 size=[NUMCOLS*SQUARE_SIZE,NUMROWS*SQUARE_SIZE] screen=pygame.display.set_mode(size) pygame.display.set_caption("Tetris") board = board( NUMROWS, NUMCOLS, size ) #Loop until the user clicks the close button. done=False clock = pygame.time.Clock() counter = 0 SIDE_DELAY = 5 #delay in loops before it starts moving left/right left_counter = 0 #how long left key has been held in game loops right_counter = 0 #how long right key has been held clock_speed = 20 while done==False: counter +=1 #how many times loop has been run total # This limits the while loop to a max of 10 times per second. # Leave this out and we will use all CPU we can. clock.tick(clock_speed) for event in pygame.event.get(): # User did something if event.type == pygame.QUIT: # If user clicked close done=True # Flag that we are done so we exit this loop if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: #pressed left, move left board.movePieceLeft() if event.key == pygame.K_RIGHT: #pressed right, move right board.movePieceRight() if event.key == pygame.K_UP: #pressed up, so rotate the block board.rotatePiece() if event.key == pygame.K_p: ##print "enter new clock speed: " clock_speed = int(raw_input("enter new clock speed: ")) ##If you hold down left/right, have a brief delay then move quickly L/R keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: ##Left arrow is held left_counter +=1 if left_counter > 25: ##delay for .25s bfore moving quickly board.movePieceLeft() else: left_counter = 0 ##not held, reset for delay if keys[pygame.K_RIGHT]: right_counter +=1 if right_counter > 5: board.movePieceRight() else: right_counter = 0 ##If you hold down down, move down rapidly with no delay if keys[pygame.K_DOWN]: board.movePieceDown() if counter % 1 == 0: ##every other second but stagger with moveDowns if board.numRotations != 0: board.rotatePiece() board.numRotations-=1 elif board.numLeft != 0: board.movePieceLeft() board.numLeft-=1 elif board.numRight !=0: board.movePieceRight() board.numRight-=1 ##force the square to move down every .5 seconds board.movePieceDown() if board.hasLost: ##Flagged upon losing condition. print "You have lost. You have cleared this many rows: ", board.numRowsCleared ROWS_CLEARED = board.numRowsCleared writeToFile() #store the sequence of randgen numbers pygame.quit() return ROWS_CLEARED #done = True # Clear the screen and set the screen background screen.fill(white) #draw the board drawBoard( screen, board, pygame ) # Go ahead and update the screen with what we've drawn. # This MUST happen after all the other drawing commands. pygame.display.flip() # Be IDLE friendly pygame.quit ()