Python–2048小游戏

在一本书上找到的小游戏实例,自己运行起来感觉比较有意思就发出来看看.

其中有很多地方不是很懂,等到自己研究透彻了会更新带全部注释版本的.

对于pygame模块还是在探索阶段,大家要有什么好的学习渠道可以在评论区留言,不胜感谢

import randomimport pygameimport timeimport sysfrom pygame.locals import *import threading # 多线程模块SIZE = 4 # 2048的小格式4*4,尺寸暂定为4LENGTH = 130 # 每个小格的边长长度SCORE_HEIGTH = 130 # 计分区占用高度DEFAULT = (205, 193, 180) # 背景颜色C_FONT = (120, 111, 102) # 字体颜色C_2 = (238, 228, 218)C_4 = (237, 224, 200)C_8 = (242, 177, 121)C_16 = (245, 149, 99)C_32 = (246, 94, 59)C_64 = (246, 94, 59)C_128 = (237, 207, 114)C_256 = (237, 204, 98)C_512 = (237, 200, 80)C_1024 = (237, 197, 63)C_2048 = (225, 187, 0)'''建立获取颜色的函数'''def get_color(n): n_t = 0 for i in range(1, 12): if n >> i == 1: n_t = i color = [ DEFAULT, C_2, C_4, C_8, C_16, C_32, C_64, C_128, C_256, C_512, C_1024, C_2048, C_FONT] return color[n_t]'''画面内容控制'''class Map: def __init__(self, size): self.size = size self.map = [[0 for i in range(size)]for i in range(size)] self.score = 0 self.is_move = 0 self.add() self.add() def add(self): while True: pos = random.randint(0, self.size * self.size - 1) flag = self.map[pos // self.size][pos % self.size] if flag == 0: num = random.randint(0, 3) n = 2 if num == 0: n = 4 self.map[pos // self.size][pos % self.size] = n self.score += n break def failed(self): for i in self.map: for j in i: if j == 0: return False for i in range(0, self.size): for j in range(0, self.size): if (i - 1 > 0 and self.map[i][j] == self.map[i - 1][j]) or (j - 1 >= 0 and self.map[i][j] == self.map[i][j - 1]) or (i + 1 < self.size and self.map[i][j] == self.map[i + 1][j]) or (j + 1 < self.size and self.map[i][j] == self.map[i][j + 1]): return False return True def check(self, num): for i in self.map: for j in i: if j == num: return True return False def move_to_left(self): changed = False for a in self.map: b = [] last = 0 for v in a: if v != 0: if v != last: b.append(v) last = v else: b.append(b.pop() * 2) last = 0 b += [0] * (self.size - len(b)) for i in range(0, self.size): if a[i] != b[i]: changed = True a[:] = b return changed def change(self): self.map = [[self.map[i][j] for i in reversed(range(self.size))]for j in range(self.size)] def move_left(self): if self.move_to_left(): self.is_move = 1 self.add() def move_up(self): self.change() self.change() self.change() if self.move_to_left(): self.is_move = 1 self.add() self.change() def move_right(self): self.change() self.change() if self.move_to_left(): self.is_move = 1 self.add() self.change() self.change() def move_down(self): self.change() if self.move_to_left(): self.is_move = 1 self.add() self.change() self.change() self.change()'''显示控制部分'''def display(map, screen): block_font = pygame.font.Font(None, 86) score_font = pygame.font.Font(None, 86) screen.fill(DEFAULT) for i in range(map.size): for j in range(map.size): block = pygame.Surface((LENGTH, LENGTH)) block.fill(get_color(map.map[i][j])) font_surf = block_font.render(str(map.map[i][j]), True, C_FONT) font_rect = font_surf.get_rect() font_rect.center = ( j * LENGTH + LENGTH / 2, LENGTH * i + LENGTH / 2) screen.blit(block, (j * LENGTH, i * LENGTH)) if map.map[i][j] != 0: screen.blit(font_surf, font_rect) score_surf = score_font.render( 'score:' + str(map.score), True, C_FONT) score_rect = score_surf.get_rect() score_rect.center = ( LENGTH * SIZE / 2, LENGTH * SIZE + SCORE_HEIGTH / 2) screen.blit(score_surf, score_rect) pygame.display.update()# 定义主程序def main(): pygame.init() # 初始化界面 screen = pygame.display.set_mode( (LENGTH * SIZE, LENGTH * SIZE + SCORE_HEIGTH)) # 屏幕尺寸设置 pygame.display.set_caption("2048小游戏") # 设置游戏标题 clock = pygame.time.Clock() # 用于控制帧率 map = Map(SIZE) display(map, screen) while not map.failed(): # 检测是否退出游戏 clock.tick(5) # 帧率为5 for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() elif event.type == KEYDOWN: keys = pygame.key.get_pressed() map.is_move = 0 if keys[K_UP]: map.move_up() elif keys[K_DOWN]: map.move_down() elif keys[K_RIGHT]: map.move_right() elif keys[K_LEFT]: map.move_left() elif event.type == KEYUP: t = threading.Thread(target=display,args=(map,screen)) t.setDaemon(True) t.start() if map.is_move==1: if map.check(2048): break time.sleep(0.01) result="YOU LOST!!!" if map.check(2048): result="YOU WIN!!!" screen.fill(DEFAULT) map_font=pygame.font.Font(None,86) font_surf=map_font.render(result,True,C_FONT) font_rect=font_surf.get_rect() font_rect.center=(SIZE*LENGTH/2,SIZE*LENGTH/2) screen.blit(font_surf,font_rect) pygame.display.update()# 测试调用主程序if __name__ == "__main__": main()

郑重声明:本文内容及图片均整理自互联网,不代表本站立场,版权归原作者所有,如有侵权请联系管理员(admin#wlmqw.com)删除。
(0)
用户投稿
上一篇 2022年6月28日
下一篇 2022年6月28日

相关推荐

联系我们

联系邮箱:admin#wlmqw.com
工作时间:周一至周五,10:30-18:30,节假日休息