Answered step by step
Verified Expert Solution
Question
1 Approved Answer
It shows me: ValueError: max() arg is an empty sequence. I've marked this part, I don't know how to solve it. The other two problems
It shows me: ValueError: max() arg is an empty sequence. I've marked this part, I don't know how to solve it. The other two problems are
File "D:pycharm2048.py", line 257, in
game_start()
File "D:pycharm2048.py", line 222, in game_start
draw_best(Background)
Here are my codes:
import mathimport osimport randomimport redisimport sysimport timeimport numpy as npimport pygame as py# Preload the movement logicdef pre_move(): numberpos = {} for num in range(1, 17): row1, row2 = divmod(num, 4) row = row1 + np.sign(row2) column = [row2 if row2 != 0 else 4][0] numberpos['{}'.format([row, column])] = num return numberpos# numberPos here is actually {' {1,1} ': 1,' {1,2} ': 2...... }. And of course if you want to design a 5x5 or a 6x6# you just change the 17 and 4 in the loop to 25 and 5 or 36 and 6. Main program# Preload the scoring picture.def pre_load_image(background): internalWidth = 0.5 # here I define as the internalwidth =0.5 imageList = {} imagePath = './score' image_filename = [i for i in os.listdir(imagePath)] # returns the list of all files and directories in the specified path width = math.floor(background.width * (1 - internalWidth) / 4) for name in image_filename: image = py.transform.smoothscale(py.image.load(imagePath + name).convert_alpha(), (width, width)) imageList[name.replace('.png', '')] = image return imageList# Load the scoring picturedef draw_image(score_list, image_list, pos_list): for pos_num in score_list: score = score_list[pos_num] py.font.init() basicFont = py.font.SysFont('Comic Sans MS', 30) # here I define as basicFont value as py.font.sysfont('value', number) scoreSurf = basicFont.render('{}'.format(score), True, (0, 0, 0)) scoreRect = scoreSurf.get_rect() if score 0: screen.blit(scoreSurf, scoreRect)# List of image positions, expressed as (x,y)# Used to determine the display point of the loaded fraction image.def image_pos_list(background): pre_x = background.topleft[0] pre_y = background.topleft[-1] internalLong = math.ceil(internalWidth / 5 * background.width) imageLong = math.floor((1 - internalWidth) / 4 * background.width) posList = dict(zip(list(range(1, 17)), [''] * 16)) for num in range(1, 17): row1, row2 = divmod(num, 4) row = row1 + np.sign(row2) column = [row2 if row2 != 0 else 4][0] image_x = pre_x + internalLong * column + imageLong * (column - 1) image_y = pre_y + internalLong * row + imageLong * (row - 1) posList['{}'.format(num)] = (image_x, image_y) return posList# Logic of movementdef number_move(number_pos, move_input, score_list): values = list(number_pos.values()) keys = list(number_pos.keys()) numberPosReverse = dict(zip(values, keys)) newScorelist = score_list.copy() oldScorelist = {} while newScorelist != oldScorelist: oldScorelist = newScorelist.copy() for num in range(1, 17): pos = eval(numberPosReverse[num]) x, y = pos[0] + move_input[0], pos[1] + move_input[1] pos[0] = [x if 1 set/updates value for the single key py.draw.rect(screen, bg, [0, 0, screen.get_width(), screen.get_height()], 0) BasicFont02 = py.font.SysFont('./Sunydate - PERSONAL USE ONLY/Sunydate-PERSONAL USE ONLY.ttf', 40) overSurf = BasicFont01.render('Game Over', True, (0, 0, 0)) overRect = overSurf.get_rect() overRect.center = (math.floor(screen.get_width() / 2), math.floor(screen.get_height() / 2)) scoreSurf = BasicFont02.render('Final Score: ', True, (0, 0, 0)) scoreRect = scoreSurf.get_rect() scoreRect.center = (math.floor(screen.get_width() / 2), math.floor(screen.get_height() * 0.6)) numberSurf = BasicFont02.render('{}'.format(score), True, (0, 0, 0)) numberRect = numberSurf.get_rect() numberRect.center = (math.floor(screen.get_width() / 2), math.floor(screen.get_height() * 0.7)) time.sleep(5) sys.exit()# Keyboard control functiondef keyboard_ctrl(event): move_output = [0, 0] if event.key == py.K_UP: move_output = [-1, 0] elif event.key == py.K_DOWN: move_output = [1, 0] elif event.key == py.K_RIGHT: move_output = [0, 1] elif event.key == py.K_LEFT: move_output = [0, -1] return move_output# Random generation of blocks.def random_score(score_list): values = list(score_list.values()) pro = [2] * (2 + values.count(2)) + [4] * (1 + values.count(4)) # The probability is the frequency of 2 or 4 in the current fraction plot blank = [[i if score_list[i] == 0 else 0][0] for i in range(1, 17)] blank = list(set(blank)) blank.remove(0) if not blank: return 'Game over' else: score_list[random.choice(blank)] = random.choice(pro) return score_list# Count and record the current scoredef record_score(score_list, background): totalScore = 0 values = list(score_list.values()) for i in values: totalScore += i scoreSurf = BasicFont01.render('Score?{}'.format(totalScore), True, (0, 0, 0)) scoreRect = scoreSurf.get_rect() scoreRect.topleft = (math.floor(0.1 * screen.get_width()), math.floor(0.05 * screen.get_height())) scoreRect.width = math.floor((rate - 0.15) / 2 * screen.get_width()) scoreRect.height = math.floor((1 - rate2) / 3 * 2 * screen.get_height()) py.draw.rect(screen, background, [scoreRect.topleft[0], scoreRect.topleft[1], scoreRect.width, scoreRect.height], 0) screen.blit(scoreSurf, scoreRect) return totalScore# Plot the highest score in historydef draw_best(background): ip = 'redis-16784.c89.us-east-1-3.ec2.cloud.redislabs.com' password = 1206 r = redis.Redis(host=ip, password = password, port=16784, db=0, decode_responses = True) scores = [eval(i) for i in list(r.hgetall('2048').values())] best_scores = max(scores) scoreSurf = BasicFont01.render('Top score?{}'.format(best_scores), True, (0, 0, 0)) scoreRect = scoreSurf.get_rect() scoreRect.width = math.floor((rate - 0.15) / 2 * screen.get_width()) scoreRect.height = math.floor((1 - rate2) / 3 * 2 * screen.get_height()) scoreRect.topright = (math.floor(0.9 * screen.get_width()), math.floor(0.05 * screen.get_height())) py.draw.rect(screen, background, [scoreRect.topleft[0], scoreRect.topleft[1], scoreRect.width, scoreRect.height], 0) screen.blit(scoreSurf, scoreRect)# Main programdef game_start(): global screen, rate py.init() clock = py.time.Clock() screen_x = 500 # Resize. Adjust the size of the home screen. screen_y = math.ceil(screen_x * rate / rate2) screen = py.display.set_mode((screen_x, screen_y), depth=32) py.display.set_caption("2048") Background = [128, 128, 128] Icon = py.image.load('./sucaipython/game.png').convert_alpha() py.display.set_icon(Icon) screen.fill(color=Background) width = math.floor(screen_x * rate) bg2 = py.image.load('./sucaipython/background.png').convert_alpha() bg2 = py.transform.smoothscale(bg2, (width, width)) bg2rect = bg2.get_rect() bg2rect.topleft = math.floor(screen_x * (1 - rate) / 2), math.floor(screen_y * (1 - rate2)) # Preloading data draw_best(Background) posList = image_pos_list(bg2rect) imageList = pre_load_image(bg2rect) scoreList = dict(zip(list(range(1, 17)), [0] * 15 + [2])) # Table of scores numberPos = pre_move() scoreList = random_score(scoreList) totalScore = 0 # Main circulation while True: screen.blit(bg2, bg2rect) # Refresh the screen if scoreList == 'GameOver': game_over(totalScore, Background) draw_image(scoreList, imageList, posList) # Plot the score totalScore = record_score(scoreList, Background) key = py.key.get_pressed() if key[py.K_ESCAPE]: exit() for event in py.event.get(): if event.type == py.QUIT: sys.exit() elif event.type == py.KEYDOWN: move_input = keyboard_ctrl(event) # Push the button scoreList = number_move(numberPos, move_input, scoreList) # Moving the numbers scoreList = random_score(scoreList) # Generates a new number after the key is pressed py.display.update() clock.tick(FPS)if __name__ == "__main__": py.font.init() BasicFont01 = py.font.Font('./thesignature/Thesignature.ttf', 30) screen = py.display.set_mode((500, 500)) rate = 0.95 # The ratio of the width under the main game interface to the width of the entire game interface rate2 = 0.7 # The ratio of the height under the main game interface to the height of the entire game interface internalWidth = 0.1 # Ratio of clearance FPS = 60 # Game frame rate game_start()
6.png 183 7.png 184 8.png 9.png 185 py.draw.rect(screen, background, [scoreRect.topleft[0], scoreRect.topleft[1], scoreRect.width, score Rect.height], ) screen.blit(scoreSurf, scoreRect) return totalScore 186 10.png 11.png 187 12.png 188 sucaipython 189 background.png 190 game.png 191 Sunydate - PERSONAL USE ONLY 192 Note of the author.txt 193 # Plot the highest score in history #def draw_best (background): ip = 'redis-16784.c89.us-east-1-3.ec2.cloud.redislabs.com' password = 1206 r = redis.Redis(host=ip, password = 1206, port=16784, db=0, decode_responses = True) scores = [eval(i) for i in list(r.hgetall('2048').values())] best_scores = max(scores) Sunydate.png 194 Sunydate-PERSONAL USE ONLY.otf Sunydate-PERSONAL USE ONLY.ttf 195 scoreSurf = BasicFont01.render('Top score: {}'.format(best_scores), True, (0, 0, 0)) scoreRect = scoreSurf.get_rect() 196 thesignature More Info.txt Read Me.pdf Thesignature.jpg Thesignature.otf Thesignature.ttf 197 198 199 200 scoreRect.width = math.floor((rate - 0.15) / 2 * screen.get_width()) scoreRect.height = math.floor((1 - rate2) / 3 * 2 * screen.get_height()) scoreRect.topright = (math.floor(0.9 * screen.get_width()), math.floor(0.05 * screen.get_height())) py.draw.rect(screen, background, [scoreRect.topleft[0], scoreRect.topleft[1], scoreRect.width, scoreRect.height], ) screen.blit(scoreSurf, scoreRect) 201 > venv library root 202
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started