def update(): stddraw.setFontFamily("WORDSOFLOVE") stddraw.setFontSize(70) stddraw.picture(pic7, 4.5, 5) stddraw.setFontSize(70) for i in range(9): for j in range(9): if game[i][j] == "strawberry": stddraw.picture(pic1, (j + 1) * 0.9, i + 0.5) elif game[i][j] == "lemon": stddraw.picture(pic2, (j + 1) * 0.9, i + 0.5) elif game[i][j] == "orange": stddraw.picture(pic3, (j + 1) * 0.9, i + 0.5) elif game[i][j] == "pear": stddraw.picture(pic4, (j + 1) * 0.9, i + 0.5) elif game[i][j] == "grapes": stddraw.picture(pic5, (j + 1) * 0.9, i + 0.5) elif game[i][j] == "watermelon": stddraw.picture(pic6, (j + 1) * 0.9, i + 0.5) stddraw.text(1, 9.5, ":Score: ") stddraw.text(4.4, 9.5, ":Moves: ") stddraw.text(8, 9.5, ":Goal: ") stddraw.setFontFamily("Blah blah bang") stddraw.setFontSize(40) stddraw.text(8.7, 9.5, "700") stddraw.text(1.8, 9.5, str(score)) stddraw.text(5.3, 9.5, str(number_of_moves)) if x: stddraw.show(0.0)
def draw_score(self, score): stddraw.setPenColor(self.boundary_color) # using boundary_color # set the pen radius stddraw.setPenRadius(self.box_thickness) # coordinates of the bottom left corner of the game grid pos_x, pos_y = self.grid_width - 0.5, -0.5 stddraw.rectangle(pos_x, pos_y, 3.5, self.grid_height) stddraw.setPenColor(Color(167, 160, 151)) stddraw.filledRectangle(pos_x + 0.03, pos_y + 0.09, 3.4, self.grid_height - 0.2) # set the text text_color = Color(0, 0, 0) stddraw.setFontFamily("Arial") stddraw.setFontSize(30) stddraw.setPenColor(text_color) text_to_display = "SCORE" text_to_display2 = "NEXT" stddraw.text(12 + 1.2, 5, text_to_display2) stddraw.text(self.grid_width + 1.2, 15, text_to_display) stddraw.text(self.grid_width + 1.2, 14, str(score)) # get the tetromino's type to create next tetromino to show in the next section tet_type = self.current_tetromino.get_type() if tet_type == 'I': width = 4 height = 11 elif tet_type == 'O': width = 2 height = 12 else: width = 3 height = 11 next_tetromino = Tetromino(tet_type, height, width) next_tetromino.draw_next_tet(self.current_tetromino) stddraw.setPenRadius() # reset the pen radius to its default value
def draw(self): # draw the tile as a filled square 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 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_game_over(grid_height, grid_width): background_color = Color(42, 69, 99) button_color = Color(25, 255, 228) text_color = Color(31, 160, 239) # clear the background canvas to background_color stddraw.clear(background_color) # get the directory in which this python code file is placed current_dir = os.path.dirname(os.path.realpath(__file__)) # path of the image file img_file = current_dir + "/menu_image.png" # center coordinates to display the image img_center_x, img_center_y = (grid_width - 1) / 2, grid_height - 7 # image is represented using the Picture class image_to_display = Picture(img_file) # display the image stddraw.picture(image_to_display, img_center_x, img_center_y) # dimensions of the start game button button_w, button_h = grid_width - 1.5, 2 # coordinates of the bottom left corner of the start game button button_blc_x, button_blc_y = img_center_x - button_w / 2, 4 # display the start game button as a filled rectangle stddraw.setPenColor(button_color) stddraw.filledRectangle(button_blc_x, button_blc_y, button_w, button_h) # display the text on the start game button stddraw.setFontFamily("Arial") stddraw.setFontSize(25) stddraw.setPenColor(text_color) text_to_display = "Game Over" stddraw.text(img_center_x, 5.5, text_to_display) stddraw.text(img_center_x, 4.5, "Score : " + str(grid.total_score)) # menu interaction loop while True: # display the menu and wait for a short time (50 ms) stddraw.show(50) # check if the mouse has been left-clicked if stddraw.mousePressed(): # get the x and y coordinates of the location at which the mouse has # most recently been left-clicked mouse_x, mouse_y = stddraw.mouseX(), stddraw.mouseY() if mouse_x >= button_blc_x and mouse_x <= button_blc_x + button_w: if mouse_y >= button_blc_y and mouse_y <= button_blc_y + button_h: return True
pic8 = p.Picture("heart.png") pic9 = p.Picture("star.png") pic10 = p.Picture("win.png") pic11 = p.Picture("lose.png") v_combo1 = pygame.mixer.Sound('v_combo1.ogg') v_combo2 = pygame.mixer.Sound('v_combo2.ogg') v_combo3 = pygame.mixer.Sound('v_combo3.ogg') v_combo4 = pygame.mixer.Sound('v_combo4.ogg') v_combo5 = pygame.mixer.Sound('v_combo5.ogg') win = pygame.mixer.Sound('win.ogg') lose = pygame.mixer.Sound('lose.ogg') bgm = pygame.mixer.Sound('bgm.ogg') meow = pygame.mixer.Sound('meow.ogg') switch = pygame.mixer.Sound('switch.ogg') match = pygame.mixer.Sound('Saint.ogg') stddraw.setFontFamily("WORDSOFLOVE") stddraw.setPenColor(stddraw.MAGENTA) x = False end = False combo = -1 level = 1 channel0 = pygame.mixer.Channel(0) channel1 = pygame.mixer.Channel(1) channel2 = pygame.mixer.Channel(2) channel3 = pygame.mixer.Channel(3) def update_without_score(): stddraw.setFontFamily("WORDSOFLOVE") stddraw.setFontSize(70) stddraw.picture(pic7, 4.5, 5)
# 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
#----------------------------------------------------------------------- # 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()
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)
#----------------------------------------------------------------------- # 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 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))
# 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