def hangman(): reply="yes" while(reply=="yes" or reply=="y"): guesses_left = 8 wrong_guesses = [] words=[] #makes a list of words from lexicon file file=open(sys.argv[1],'r') for line in file: for word in line.split(): words.append(word) random_word=random.choice(words).lower() #choose a random word from words list letters=[] guessed_letter=None letters_guessed = [] y = len(random_word) if (y>=4): for i in random_word: letters.append("_") #fills up letters list containing as many number of dashes( _ ) as there are letters in the randomly chosen word. else: hangman() hidden_word=None while(guesses_left!=0 and '_' in letters): hidden_word = " ".join(letters) stddraw.setPenColor(stddraw.WHITE) stddraw.filledRectangle(-1, -0.8, 2, 0.5) hangman_module.word(hidden_word) # printing the word print("The Secret Word looks like: ",hidden_word) print("Attempts remaining: ",guesses_left) print("What's your next guess? \n") guessed_letter = str(input()).lower() if guessed_letter in letters_guessed: print("You have already made that guess. Make a new guess") continue else: pass letters_guessed.append(guessed_letter) if guessed_letter in random_word: hangman_module.draw_hangman(guesses_left) # drawing hangman hangman_module.word(hidden_word) # printing the word print("Nice guess") for j in range(len(random_word)): if guessed_letter == random_word[j]: letters[j]=guessed_letter if guessed_letter not in random_word : guesses_left-=1 print("Sorry, Wrong guess!") wrong_guesses.append(guessed_letter) w = " ".join(wrong_guesses) print("Wrong Guesses :", w) hangman_module.draw_hangman(guesses_left) # drawing hangman hangman_module.word(hidden_word) # printing the word hangman_module.wrong_guesses(" ".join(wrong_guesses)) #Printing wrong guesses if "_" not in letters: print("Congratulations! You guessed the secret word: \n") print(random_word) stddraw.setFontSize(40) stddraw.setPenColor(stddraw.GREEN) stddraw.text(-0.2, -0.3, "YOU WON !") hangman_module.correct_word(random_word) # printing the word else: print("Sorry, You lost the game. The secret word was: \n") print(random_word) stddraw.setFontSize(40) stddraw.setPenColor(stddraw.RED) stddraw.text(-0.2, -0.3, "YOU LOST") hangman_module.correct_word(random_word) # printing the word print("Do you want to play again?\n") reply=str(input())
def drawBoard(): stddraw.setFontSize(40) for row in range(9): for column in range(7): drawPiece(column, row, 0)
def win(score, startTime): t = round(time.time() - startTime, 2) winTime = "Time Elapsed: " + str(t) + " seconds" if (t <= 31 and score >= 73): rating = 7 text = "YOU ARE A CHAMPION -- YOU WIN!" elif (t <= 31 and score >= 69): rating = 6 text = "Incredible!! Masterfully done!!" elif (t <= 31): rating = 5 text = "Great job! Go get em' champ!!" elif (t <= 40): rating = 4 text = "Good. Not perfect, but not bad!" elif (t <= 48): rating = 3 text = "Sufficient, but could be better." elif (t <= 56): rating = 2 text = "What's taking so long, slowpoke?" elif (t <= 70): rating = 1 text = "Seriously, were you even trying?" else: rating = 0 text = "Exceptional. Exceptionally bad, that is." allErase() for i in range(210): showScore(score, 1 + i) stddraw.show(5) stddraw.show(1500) stddraw.setFontSize(40) stddraw.setPenColor(stddraw.DARK_GRAY) stddraw.text(0, 400, winTime) stddraw.show(1000) stddraw.setFontSize(60) stddraw.setPenColor(stddraw.BLACK) stddraw.text(0, 200, "Rating") stddraw._thickLine(-90, 167, 90, 167, 2) stddraw.show(1000) stddraw.setFontSize(30) stddraw.setPenColor(stddraw.DARK_GRAY) stddraw.text(0, -350, text) stddraw.setFontSize(300) if (rating >= 6): stddraw.setPenColor(stddraw.MAGENTA) stddraw.text(0, -50, str(rating) + "/5") elif (rating == 5): stddraw.setPenColor(stddraw.BLUE) stddraw.text(0, -50, str(rating) + "/5") elif (rating == 4): stddraw.setPenColor(stddraw.BOOK_BLUE) stddraw.text(0, -50, str(rating) + "/5") elif (rating == 3): stddraw.setPenColor(stddraw.YELLOW) stddraw.text(0, -50, str(rating) + "/5") elif (rating == 2): stddraw.setPenColor(stddraw.ORANGE) stddraw.text(0, -50, str(rating) + "/5") elif (rating == 1): stddraw.setPenColor(stddraw.RED) stddraw.text(0, -50, str(rating) + "/5") else: stddraw.setPenColor(stddraw.BOOK_RED) stddraw.text(0, -50, str(rating) + "/5") if (rating != 7): stddraw.show(1500) stddraw.setPenColor(stddraw.BLACK) stddraw.setFontSize(30) stddraw.text(170, -500, "Click anywhere to exit.") waitForClick() else: val = True count = 0 list = [stddraw.MAGENTA, stddraw.RED, stddraw.BLUE, stddraw.CYAN, stddraw.YELLOW, stddraw.GREEN] while (not stddraw.mousePressed()): count += 1 stddraw.setPenColor(stddraw.WHITE) stddraw.filledRectangle(-300, -200, 700, 300) stddraw.setPenColor(list[random.randint(0, 5)]) stddraw.text(0, -50, str(rating) + "/5") if (val and count >= 30): stddraw.setPenColor(stddraw.BLACK) stddraw.setFontSize(30) stddraw.text(170, -500, "Click anywhere to exit.") stddraw.setFontSize(300) val = False stddraw.show(45)
game_win = True count = 0 mouse_clicked = False game_playerOne_click = [] game_playertwo_click = [] while game_win == True: """playerOne turn""" mouse_clicked = False while mouse_clicked == False: mouse_clicked = stddraw.mousePressed() # return boolean stddraw.clear() stddraw.setFontSize(30) stddraw.text(1, 3.5, "Is playerOne's turn") draw_the_picture(game_array) if mouse_clicked: game_array = player_two_draw(game_array) game_playertwo_click = [] game_playertwo_click = expend_the_player_array(game_playertwo_click, 2) game_win = determine_the_winner(game_playertwo_click, 2, game_win) there_is_no_winner(game_playertwo_click, game_playerOne_click) stddraw.show(1) """playerOne turns"""
def show_points(points): stddraw.setFontSize(40) stddraw.setPenColor(stddraw.BLACK) stddraw.text(1, -0.5, "Score: " + str(points))
def show_end(): stddraw.setFontSize(125) stddraw.setPenColor(stddraw.BLACK) stddraw.text(3.5, 4.5, "Game Over") stddraw.show()
def drawWinThing(): fillWinThing() stddraw.setFontSize(20) stddraw.setPenColor(stddraw.BLACK) stddraw.text(5.65, 4.5, 'Score above 75') stddraw.text(5.55, 4, 'points to win!')
#----------------------------------------------------------------------- # bouncingball.py #----------------------------------------------------------------------- import time #import stddrawtkinter as stddraw import stddraw # Draw a bouncing ball. stddraw.createWindow() stddraw.setXscale(-1.0, 1.0) stddraw.setYscale(-1.0, 1.0) stddraw.setFontFamily() stddraw.setFontSize() rx = .480 ry = .860 vx = .015 vy = .023 radius = .05 dt = 20 start = time.time() t = 0 while True: # Update ball position and draw it there. if abs(rx + vx) + radius > 1.0: vx = -vx if abs(ry + vy) + radius > 1.0: vy = -vy rx = rx + vx ry = ry + vy
def drawInvalidMatch3(): stddraw.setFontSize(20) stddraw.setPenColor(stddraw.BLACK) stddraw.text(5.75, 8, 'You have to swap') stddraw.text(5.75, 7.5, ' to match a 3!') stddraw.show(1500)
def drawTurnCounter(turnCounter): fillTurn() stddraw.setFontSize(20) stddraw.setPenColor(stddraw.BLACK) stddraw.text(5.5, 5.5, 'Turns left:' + str(turnCounter))
def drawInvalidSwitch(): stddraw.setFontSize(20) stddraw.setPenColor(stddraw.BLACK) stddraw.text(5.5, 8, 'That is') stddraw.text(5.5, 7.5, ' an invalid swap!') stddraw.show(1500)
def X(score, x, y): if x > .625 and x <1: if y >.0625 and y < .375: stddraw.setPenColor(stddraw.PINK) stddraw.setFontSize(76) stddraw.text(.75,.25,"X") score[2][2] = 1 if y >.375 and y < .625: stddraw.setPenColor(stddraw.PINK) stddraw.setFontSize(76) stddraw.text(.75,.5,"X") score[1][2] = 1 if y >.625 and y < 1: stddraw.setPenColor(stddraw.PINK) stddraw.setFontSize(76) stddraw.text(.75,.75,"X") score[0][2] = 1 if x >.375 and x <.625: if y >.0625 and y <.375: stddraw.setPenColor(stddraw.PINK) stddraw.setFontSize(76) stddraw.text(.5,.25,"X") score[2][1] = 1 if y >.375 and y <.625: stddraw.setPenColor(stddraw.PINK) stddraw.setFontSize(76) stddraw.text(.5,.5,"X") score[1][1] = 1 if y >.625 and y < 1: stddraw.setPenColor(stddraw.PINK) stddraw.setFontSize(76) stddraw.text(.5,.75,"X") score[0][1] = 1 if x >0 and x <.375: if y > .0625 and y <0.325: stddraw.setPenColor(stddraw.PINK) stddraw.setFontSize(76) stddraw.text(.25,.25,"X") score[2][0] = 1 if y > .325 and y <.625: stddraw.setPenColor(stddraw.PINK) stddraw.setFontSize(76) stddraw.text(.25,.5,"X") score[1][0] = 1 if y > .625 and y < 1: stddraw.setPenColor(stddraw.PINK) stddraw.setFontSize(76) stddraw.text(.25,.75,"X") score[0][0] = 1
def player_two_turn(): stddraw.setFontSize(36) stddraw.setPenColor(stddraw.WHITE) stddraw.filledRectangle(0,0,1,.09) stddraw.setPenColor(stddraw.PINK) stddraw.text(0.5,.0625,"It's player two's turn!")
piece2 = p2Mod.getMove(board, COLUMNS, ROWS) sd.square(piece2[0] + .5, piece2[1] + .5, .5) sd.show(500) dif1 = (piece1[0] - piece2[0]) * (piece1[0] - piece2[0]) dif2 = (piece1[1] - piece2[1]) * (piece1[1] - piece2[1]) p2Mod.drawBoard(board, COLUMNS, ROWS, score, turn) if (dif1 == 0 and dif2 == 1) or (dif1 == 1 and dif2 == 0): validMove = True p2Mod.move(board, piece1, piece2) sd.clear() p2Mod.drawBoard(board, COLUMNS, ROWS, score, turn) sd.show(200) if not p2Mod.winCheck(board, COLUMNS, ROWS): p2Mod.move(board, piece1, piece2) p2Mod.drawBoard(board, COLUMNS, ROWS, score, turn) sd.show(500) sd.clear() score += p2Mod.fill(board, COLUMNS, ROWS) p2Mod.drawBoard(board, COLUMNS, ROWS, score, turn) sd.show(1) combo = True while combo: combo = p2Mod.winCheck(board, COLUMNS, ROWS) p2Mod.drawBoard(board, COLUMNS, ROWS, score, turn) sd.show(500) score += p2Mod.fill(board, COLUMNS, ROWS) sd.show(1) sd.setFontSize(70) sd.text(3.5, 4.5, "Game Over") sd.show()
def clickHelp(): clickHelpClear() stddraw.setFontSize(20) stddraw.setPenColor(stddraw.BLACK) stddraw.text(2, 0.5, 'Made a Wrong click? Reclick the same tile to try again')
# banner.py #----------------------------------------------------------------------- import stddraw import sys # Accept string command-line argument s. Draw s, and move it across # the screen, left-to-right, wrapping around when it reaches the border. s = sys.argv[1] # Remove the 5% border. stddraw.createWindow() stddraw.setXscale(1.0/22.0, 21.0/22.0) stddraw.setYscale(1.0/22.0, 21.0/22.0) # Set the font. stddraw.setFontFamily('Arial') stddraw.setFontSize(60) stddraw.setPenColor(stddraw.BLACK) i = 0.0 while True: stddraw.clear() stddraw.text((i % 1.0), 0.5, s) stddraw.text((i % 1.0) - 1.0, 0.5, s) stddraw.text((i % 1.0) + 1.0, 0.5, s) stddraw.sleep(60) stddraw.show() i += 0.01
def drawScore(x, y): stddraw.setPenColor(stddraw.BLACK) stddraw.setFontSize(20) stddraw.text(x - 6.2, y + 2.5, 'Score:')
def display(self, delay=0): # clear the background canvas to empty_cell_color stddraw.clear(self.background_color) # draw a box around the game grid self.draw_boundaries() # draw the game grid self.draw_grid() # draw the current (active) tetromino ghost if self.current_ghost != None: self.current_ghost.draw() # draw the current (active) tetromino if self.current_tetromino != None: self.current_tetromino.draw() # draw the normal game cycle GUI if not self.game_over: # set pen color based on game mode if self.gamemode == "tetris": stddraw.setPenColor(stddraw.WHITE) else: stddraw.setPenColor(self.boundary_color) # set font and its size stddraw.setFontFamily("Arial") stddraw.setFontSize(24) # draw score if self.gamemode == "2048" and self.reached_2048: stddraw.boldText(13.75, 19, "Congrats!") stddraw.text(13.75, 17.75, "Score") stddraw.boldText(13.75, 16.75, str(self.score)) else: stddraw.text(13.75, 19, "Score") stddraw.boldText(13.75, 18, str(self.score)) # draw upcoming tetrominoes stddraw.text(13.75, 15, "Upcoming") stddraw.text(13.75, 14, "Tetrominoes") stddraw.setPenColor(self.boundary_color) stddraw.filledRectangle(12, -0.25, 3.5, 13.5) stddraw.setPenRadius(0.001) if self.gamemode == "tetris": stddraw.setPenColor(stddraw.DARK_GRAY) else: stddraw.setPenColor(self.empty_cell_color) stddraw.line(12.25, 8.75, 15.25, 8.75) stddraw.line(12.25, 4.25, 15.25, 4.25) self.next_tetromino1.copy( blcx=(14.25 - (self.next_tetromino1.column_count / 2)), blcy=9.5 + (4 - self.next_tetromino1.row_count) / 2, trim=True).draw() self.next_tetromino2.copy( blcx=(14.25 - (self.next_tetromino2.column_count / 2)), blcy=5 + (4 - self.next_tetromino2.row_count) / 2, trim=True).draw() self.next_tetromino3.copy( blcx=(14.25 - (self.next_tetromino3.column_count / 2)), blcy=0.5 + (4 - self.next_tetromino3.row_count) / 2, trim=True).draw() # show the canvas stddraw.show(delay) # draw the game over GUI else: # set pen color based on game mode if self.gamemode == "tetris": stddraw.setPenColor(stddraw.WHITE) else: stddraw.setPenColor(self.boundary_color) # set font and its size stddraw.setFontFamily("Arial") stddraw.setFontSize(24) # draw game over text and and final score stddraw.text(13.75, 12, "Game Over!") stddraw.text(13.75, 10, "Final Score:") stddraw.boldText(13.75, 9, str(self.score)) # draw the high score conclusion stddraw.setFontSize(18) if self.score > self.old_high_score: stddraw.text(13.75, 7.75, "New High Score!") self.new_high_score = self.score else: stddraw.text(13.75, 7.75, "High Score:") stddraw.boldText(13.75, 7, str(self.old_high_score)) # draw the after-game controls stddraw.setFontSize(16) stddraw.text(13.75, 2, "Press R to") stddraw.text(13.75, 1.5, "restart the game,") stddraw.text(13.75, 1, "or press Enter to") stddraw.text(13.75, 0.5, "return to the") stddraw.text(13.75, 0, "main manu.") # show the canvas stddraw.show(delay)
def drawTotalScore(x, y, score): stddraw.setFontSize(20) stddraw.setPenColor(stddraw.BLACK) stddraw.setFontSize(20) stddraw.text(x - 6.2, y + 2, str(score))
def show_turns(turn): stddraw.setFontSize(40) stddraw.setPenColor(stddraw.BLACK) stddraw.text(4, -0.5, "Turn: " + str(turn))
def eraseScore(x, y): stddraw.setFontSize(0.5) stddraw.setPenColor(stddraw.WHITE) stddraw.filledCircle(x - 6.2, y + 2, 0.25)
stddraw.setFontSize(300) val = False stddraw.show(45) # declare variables r = 40.0 notDone = True # set scale of drawing stddraw.setXscale(-350, 350) stddraw.setYscale(-550, 550) stddraw.setCanvasSize(560, 880) # Tells user what the game is, and waits for a click on the window stddraw.setFontSize(160) stddraw.setPenColor(stddraw.BLUE) stddraw.text(0, 350, "TAFFY") stddraw.setPenColor(stddraw.BOOK_BLUE) stddraw.text(0, 180, "TANGLE") stddraw.setPenColor(stddraw.BLACK) stddraw.setFontSize(40) stddraw.text(0, 20, "Each taffy is 1 point.") stddraw.text(0, -60, "Reach 60 points as") stddraw.text(0, -120, "fast as you can!") stddraw.setPenColor(stddraw.DARK_GRAY) stddraw.setFontSize(30) while (not stddraw.mousePressed()): stddraw.text(0, -300, "Click anywhere to start!") stddraw.show(350) stddraw.setPenColor(stddraw.WHITE)
def Rguess(blank_array): stddraw.setFontSize(30) stddraw.setPenColor(stddraw.GREEN) for i in range(len(blank_array)): stddraw.text(i - 5, -8.5, ''.join(blank_array[i])) stddraw.show(500)
import stddraw import sys # Accept string command-line argument s. Draw s, and move it across # the screen, left-to-right, wrapping around when it reaches the border. s = sys.argv[1] # Remove the 5% border. stddraw.setXscale(1.0 / 22.0, 21.0 / 22.0) stddraw.setYscale(1.0 / 22.0, 21.0 / 22.0) # Set the font. stddraw.setFontFamily('Arial') stddraw.setFontSize(60) stddraw.setPenColor(stddraw.BLACK) i = 0.0 while True: stddraw.clear() stddraw.text((i % 1.0), 0.5, s) stddraw.text((i % 1.0) - 1.0, 0.5, s) stddraw.text((i % 1.0) + 1.0, 0.5, s) stddraw.show(60.0) i += 0.01
def draw(self): # draw the tile as a filled square if (self.number == 2): self.background_color = Color(238, 228, 218) # background (tile) color if (self.number == 4): self.background_color = Color(237, 224, 200) # background (tile) color if (self.number == 8): self.background_color = Color(242, 177, 121) # background (tile) color if (self.number == 16): self.background_color = Color(245, 149, 99) # background (tile) color if (self.number == 32): self.background_color = Color(246, 124, 95) # background (tile) color if (self.number == 64): self.background_color = Color(246, 94, 59) # background (tile) color if (self.number == 128): self.background_color = Color(237, 207, 114) # background (tile) color if (self.number == 256): self.background_color = Color(237, 204, 97) # background (tile) color if (self.number == 512): self.background_color = Color(237, 200, 80) # background (tile) color if (self.number == 1024): self.background_color = Color(237, 197, 63) # background (tile) color if (self.number == 2048): self.background_color = Color(237, 194, 46) # background (tile) color #used proper colors until 2048 #used remainder in order to get a proper color for every number #with this the value will never exceed 255 however better solution may be proposed if (self.number > 2048): self.background_color = Color( (self.number % 255), (self.number % 20), (self.number % 255)) stddraw.setPenColor(self.background_color) stddraw.filledSquare(self.position.x, self.position.y, 0.5) # draw the bounding box of the tile as a square stddraw.setPenColor(self.boundary_color) stddraw.setPenRadius(Tile.boundary_thickness) stddraw.square(self.position.x, self.position.y, 0.5) stddraw.setPenRadius() # reset the pen radius to its default value # draw inner lines x = self.position.x y = self.position.y stddraw.setPenColor(self.foreground_color) stddraw.square(x, y, 0.3) stddraw.setPenRadius(Tile.boundary_thickness) stddraw.line(x - 0.5, y - 0.5, x + 0.5, y + 0.5) stddraw.line(x - 0.5, y + 0.5, x + 0.5, y - 0.5) #draw inner box stddraw.setPenRadius() stddraw.setPenColor(self.background_color) stddraw.filledSquare(self.position.x, self.position.y, 0.3) ############################### #stddraw.setPenColor(stddraw.RED) #stddraw.filledCircle(0.5, 0.5, 0.3) #stddraw.filledCircle(-0.5,0.5,0.3) # draw the number on the tile stddraw.setPenColor(self.foreground_color) stddraw.setFontFamily(Tile.font_family) stddraw.setFontSize(Tile.font_size) stddraw.boldText(self.position.x, self.position.y, str(self.number))
def display(playing_field, x, y, lent, totalscore, finalscore, clickcoords, clicklocal, attempts, level, background, win): stddraw.clear() image(background) stddraw.setXscale(-lent, (2 * lent * x) - lent) stddraw.setYscale(-(2 * lent * y) - 4 * lent, lent) stddraw.setPenColor(stddraw.PINK) stddraw.filledRectangle(-lent, -(2 * lent * y) - 4 * lent, (2 * lent * x), 5 * lent) stddraw.setPenColor(stddraw.BOOK_LIGHT_BLUE) stddraw.filledRectangle(-lent, -(2 * lent * y) - 3 * lent, (2 * lent * x), lent) stddraw.setPenColor(stddraw.BOOK_LIGHT_BLUE) stddraw.filledRectangle(-lent, -(2 * lent * y) - 1.5 * lent, (2 * lent * x), lent) stddraw.setPenColor(stddraw.GRAY) stddraw.filledRectangle(-lent, -(2 * lent * y) - 2.2 * lent, (2 * lent * x), lent) if totalscore < finalscore: stddraw.setPenColor(stddraw.GREEN) stddraw.filledRectangle(-lent, -(2 * lent * y) - 2.2 * lent, ((2 * lent * x) - lent) / 1.7 * (totalscore / finalscore), lent) if totalscore >= finalscore: stddraw.setPenColor(stddraw.GREEN) stddraw.filledRectangle(-lent, -(2 * lent * y) - 2.2 * lent, ((2 * lent * x) - lent), lent) stddraw.setPenColor(stddraw.BOOK_LIGHT_BLUE) stddraw.filledRectangle(x * lent + lent, -(2 * lent * y) - 3.5 * lent, (lent * x), 3.5 * lent) stddraw.setPenRadius(0.010) stddraw.setPenColor(stddraw.BOOK_LIGHT_BLUE) stddraw.rectangle(-lent, -(2 * lent * y) - 4 * lent, (2 * lent * x), 5 * lent) stddraw.setFontSize(25) stddraw.setPenColor(stddraw.DARK_GREEN) stddraw.text((((2 * lent * x) - lent) / 1.3), -((2 * lent * y) + lent), 'Points: ' + str(totalscore) + ' / ' + str(finalscore)) stddraw.setPenColor(stddraw.RED) stddraw.setFontSize(20) stddraw.text((((2 * lent * x) - lent) / 1.3), -((2 * lent * y) + 2.7 * lent), 'Attempts Left: ' + str(attempts)) stddraw.setPenColor(stddraw.BLUE) stddraw.setFontSize(25) stddraw.text((((2 * lent * x) - lent) / 20), -((2 * lent * y) - 0.08 * lent), 'Level: ' + str(level)) for i in range(len(clickcoords) - 1): found = False reps = -1 while found == False and not reps >= (x * y - 1): reps += 1 if clicklocal[reps][0] < clickcoords[i] < clicklocal[reps][ 1] and clicklocal[reps][2] < clickcoords[ i + 1] < clicklocal[reps][3]: spotx = playing_field[reps][0] spoty = playing_field[reps][1] stddraw.setPenColor(stddraw.DARK_RED) stddraw.setPenRadius(0.05) stddraw.filledSquare(spotx, spoty, lent) found = True for i in range(x * y): spotx = playing_field[i][0] spoty = playing_field[i][1] if playing_field[i][2] == 1: stddraw.setPenColor(stddraw.MAGENTA) stddraw.setPenRadius(0.05) stddraw.filledCircle(spotx, spoty, lent / 1.2) if playing_field[i][2] == 2: stddraw.setPenColor(stddraw.YELLOW) stddraw.setPenRadius(0.05) stddraw.filledSquare(spotx, spoty, lent / 1.2) if playing_field[i][2] == 3: stddraw.setPenColor(stddraw.BLUE) stddraw.setPenRadius(0.05) stddraw.filledPolygon([(spotx - lent) + 0.2, (spotx - lent) + 0.3, (spotx - lent) + 0.7, (spotx - lent) + 0.9, (spotx - lent) + 0.6], [(spoty - lent) + 0.6, (spoty - lent) + 0.9, (spoty - lent) + 0.9, (spoty - lent) + 0.6, (spoty - lent) + 0.1]) if playing_field[i][2] == 4: stddraw.setPenColor(stddraw.GREEN) stddraw.setPenRadius(0.05) stddraw.filledPolygon([ (spotx - lent) + 0.1, (spotx - lent) + 0.4, (spotx - lent) + 0.9, (spotx - lent) + 0.4, ], [(spoty - lent) + 0.4, (spoty - lent) + 0.1, (spoty - lent) + 0.4, (spoty - lent) + 0.9]) if playing_field[i][2] == 5: stddraw.setPenColor(stddraw.BLACK) stddraw.setPenRadius(0.05) stddraw.filledPolygon([(spotx - lent) + 0.5, (spotx - lent) + 0.1, (spotx - lent) + 0.9], [(spoty - lent) + 0.8, (spoty - lent) + 0.1, (spoty - lent) + 0.1]) if playing_field[i][2] == 6: stddraw.setPenColor(stddraw.CYAN) stddraw.setPenRadius(0.05) stddraw.filledPolygon([(spotx - lent) + 0.5, (spotx - lent) + 0.1, (spotx - lent) + 0.9, (spotx - lent) + 0.3, (spotx - lent) + 0.6], [(spoty - lent) + 0.9, (spoty - lent) + 0.7, (spoty - lent) + 0.7, (spoty - lent) + 0.1, (spoty - lent) + 0.1]) if attempts == 0 and totalscore < finalscore: while stddraw.mousePressed() == False: stddraw.setPenColor(stddraw.BLACK) stddraw.filledRectangle(-lent, -(lent * y) - 2 * lent, (2 * lent * x), 3.5 * lent) stddraw.setFontSize(40) stddraw.setPenColor(stddraw.RED) stddraw.text(lent * x - lent, -(lent * y), 'YOU LOSE, CLICK TO EXIT') stddraw.show(0) if stddraw.mousePressed() == True: win = True return win if totalscore >= finalscore: while stddraw.mousePressed() == False: stddraw.setPenColor(stddraw.BOOK_BLUE) stddraw.filledRectangle(-lent, -(lent * y) - 2 * lent, (2 * lent * x), 3.5 * lent) stddraw.setFontSize(40) stddraw.setPenColor(stddraw.GREEN) stddraw.text(lent * x - lent, -(lent * y), 'YOU WIN, CLICK TO PROCEED') stddraw.show(0) if stddraw.mousePressed() == True: win = False return win stddraw.show(250) return win
import stddraw import global_variables_p1 stddraw.setCanvasSize(450, 450) stddraw.setFontSize(24) def draw_board(): stddraw.line((1 / 3), 1, (1 / 3), 0) stddraw.line((2 / 3), 1, (2 / 3), 0) stddraw.line(0, (2 / 3), 1, (2 / 3)) stddraw.line(0, (1 / 3), 1, (1 / 3)) def draw_tie(): stddraw.text((1 / 3) + (1 / 3) / 2, 0.98, "TIED GAME!") stddraw.show() def draw_win(turn_number): if turn_number % 2 == 0: #player two won stddraw.text((1 / 3) + (1 / 3) / 2, 0.98, "PLAYER 2 WON!") else: #player one won stddraw.text((1 / 3) + (1 / 3) / 2, 0.98, "PLAYER 1 WON!") stddraw.show() def draw_turn(turn_number): stddraw.setPenColor(stddraw.WHITE)
if board[row][column] == board[row+2][column]: #D if board[row][column] == board[row+1][column-1] or board[row][column] == board[row+1][column+1]: return False return True def checkScoreTime(time): if time != 9999: if time >= 25: return True return False stddraw.setXscale(0,7) stddraw.setYscale(0,11) stddraw.setCanvasSize(420, 660) #700*0.6, 1100*0.6 stddraw.setFontSize(50) stddraw.setPenColor(stddraw.DARK_BLUE) stddraw.text(3.5, 9.5, "How do you") stddraw.text(3.5, 8.5, "want to play?") stddraw.filledRectangle(1, 5, 5, 2) stddraw.filledRectangle(1, 2, 5, 2) stddraw.setPenColor(stddraw.WHITE) stddraw.text(3.5, 6, "Timed") stddraw.text(3.5, 3, "Perfection") while True: stddraw.show(0.0) #pause for .1 second if stddraw.mousePressed():
# returns game board, columnOfFallenTile, rowOfFallenTile fall_tuple = cells_fall(a) draw_game_board(a) # checks if a combination exists at the cell the tile fell to and deletes if True # if fall_tuple[1] is not None and fall_tuple[2] is not None: if is_combination(a, fall_tuple[1], fall_tuple[2]) is True: delete_combination(a, fall_tuple[1], fall_tuple[2]) draw_game_board(a) generateTuple = generate_tile(a) score += generateTuple[3] stddraw.setFontSize(25) stddraw.setPenColor(color.WHITE) stddraw.picture(images.scoreOverlay, 327, 725) stddraw.text(327, 725, str(score)) draw_game_board(a) if generateTuple[1] is not None and generateTuple[2] is not None: if is_combination(a, generateTuple[1], generateTuple[2]) is True: delete_combination(a, generateTuple[1], generateTuple[2]) draw_game_board(a) """ Program checks if score exceeds winning condition """ if score >= 100: game_on = False stddraw.picture(images.youWinBackground, 250, 400)
""" This program is so simple that I don't need the usual DISCLAIMER... Example of using the Color and Picture data types from the booksite library. """ from color import Color from picture import Picture import stddraw turquoise = Color(64, 224, 208) stddraw.setPenColor(turquoise) stddraw.setPenRadius(0.025) stddraw.rectangle(.1, .1, .8, .8) my_picture = Picture('karel.png') stddraw.picture(my_picture, 0.5, 0.6) stddraw.setPenColor(stddraw.BLACK) stddraw.setFontSize(64) stddraw.text(0.5, 0.25, 'I live!') stddraw.show() help(stddraw)
def P2(): stddraw.setPenColor(stddraw.BLACK) if x == 1 or x == 2: stddraw.text(0.5, 0.8, " GAME OVER") stddraw.text(0.5, 0.8, " Player 1's Turn") stddraw.setFontSize(46) # B1 if (mx < -0.2 and mx > -0.6): if (my > -0.6 and my < -0.2): if array[0][0] == 0: stddraw.text(-0.4, -0.4, "X") array[0][0] = 2 count += 1 # B2 if (mx > -0.2 and mx < 0.2): if (my > -0.6 and my < -0.2): if array[0][1] == 0: stddraw.text(0, -0.4, "X") array[0][1] = 2 count += 1 # B3 if (mx > 0.2 and mx < 0.6): if (my > -0.6 and my < -0.2): if array[0][2] == 0: stddraw.text(0.4, -0.4, "X") array[0][2] = 2 count += 1 # B4 if (mx > -0.6 and mx < -0.2): if (my > -0.2 and my < 0.2): if array[1][0] == 0: stddraw.text(-0.4, 0, "X") array[1][0] = 2 count += 1 # B5 if (mx > -0.2 and mx < 0.2): if (my > -0.2 and my < 0.2): if array[1][1] == 0: stddraw.text(0, 0, "X") array[1][1] = 2 count += 1 # B6 if (mx > 0.2 and mx < 0.6): if (my > -0.2 and my < 0.2): if array[1][2] == 0: stddraw.text(0.4, 0, "X") array[1][2] = 2 count += 1 # B7 if (mx > -0.6 and mx < -0.2): if (my > 0.2 and my < 0.6): if array[2][0] == 0: stddraw.text(-0.4, 0.4, "X") array[2][0] = 2 count += 1 # B8 if (mx > -0.2 and mx < 0.2): if (my > 0.2 and my < 0.6): if array[2][1] == 0: stddraw.text(0, 0.4, "X") array[2][1] = 2 count += 1 # B9 if (mx > 0.2 and mx < 0.6): if (my > 0.2 and my < 0.6): if array[2][2] == 0: stddraw.text(0.4, 0.4, "X") array[2][2] = 2 count += 1
#----------------------------------------------------------------------- # shapestext.py #----------------------------------------------------------------------- import stddraw # Draw some shapes and some text. stddraw.createWindow() stddraw.square(.2, .8, .1) stddraw.filledSquare(.8, .8, .2) stddraw.circle(.8, .2, .2) xd = [.1, .2, .3, .2] yd = [.2, .3, .2, .1] stddraw.filledPolygon(xd, yd) stddraw.setFontFamily('Times') stddraw.setFontSize(40) stddraw.text(.2, .5, 'black') stddraw.setPenColor(stddraw.WHITE) stddraw.setFontFamily('Courier') stddraw.setFontSize(30) stddraw.text(.8, .8, 'white') stddraw.show() stddraw.wait()