合成2048小遊戲小程序?在一本書上找到的小遊戲實例,自己運行起來感覺比較有意思就發出來看看.,接下來我們就來聊聊關于合成2048小遊戲小程序?以下内容大家不妨參考一二希望能幫到您!
在一本書上找到的小遊戲實例,自己運行起來感覺比較有意思就發出來看看.
其中有很多地方不是很懂,等到自己研究透徹了會更新帶全部注釋版本的.
對于pygame模塊還是在探索階段,大家要有什麼好的學習渠道可以在評論區留言,不勝感謝
import random
import pygame
import time
import sys
from pygame.locals import *
import threading # 多線程模塊
SIZE = 4 # 2048的小格式4*4,尺寸暫定為4
LENGTH = 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()
更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!