盘点那些适合新手入门python的小项目或小例子!
【惊雷】python 新手想做一个项目???那必须的是2048小游戏啊?自己写完还能玩!
小朋友来看一看,跟着敲起来。
import random
import math
__mataclass__ = type # 使用新式类
# 此类为地图模块封装的类
class map2048():
# 重新设置游戏数据
def reset(self):
self.__row = 4 # 行数
self.__col = 4 # 列数
self.data = [
[0 for x in range(self.__col)]
for y in range(self.__row)
]
# self.data = [[x + 4 * y for x in range(self.__col)]
# for y in range(self.__row)]
# self.data = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]
self.fill2()
self.fill2()
def __init__(self):
self.reset()
# 获取没有数字的位置的个数
def get_space_count(self):
'''
获取没有数字的方格的数量
'''
count = 0
for r in self.data:
count += r.count(0)
return count
# 获取游戏的得数。
def get_score(self):
s = 0
for r in self.data:
for c in r:
s += 0 if c (3, 0)):
from tkinter import *
from tkinter import messagebox
else:
from Tkinter import *
game = map2048()
keymap = {
'a': game.left,
'd': game.right,
'w': game.up,
's': game.down,
'Left': game.left,
'Right': game.right,
'Up': game.up,
'Down': game.down,
'q': exit,
}
game_bg_color = '#bbada0'
mapcolor = {
0: ('#cdc1b4', '#776e65'),
2: ('#eee4da', '#776e65'),
4: ('#ede0c8', '#f9f6f2'),
8: ('#f2b179', '#f9f6f2'),
16: ('#f59563', '#f9f6f2'),
32: ('#f67c5f', '#f9f6f2'),
64: ('#f65e3b', '#f9f6f2'),
128: ('#edcf72', '#f9f6f2'),
256: ('#edcc61', '#f9f6f2'),
512: ('#e4c02a', '#f9f6f2'),
1024: ('#e2ba13', '#f9f6f2'),
2048: ('#ecc400', '#f9f6f2'),
4096: ('#ae84a8', '#f9f6f2'),
8192: ('#b06ca8', '#f9f6f2'),
}
# 游戏各方块的lable数据
map_labels = []
# 鼠标按下处理函数
def on_mouse_down(event):
print('clicked at', event.x, event.y)
# 键盘按下处理函数
def on_key_down(event):
keysym = event.keysym
if keysym in keymap:
if keymap[keysym]():
game.fill2()
update_ui()
if game.is_gameover():
mb = messagebox.askyesno(title='gameover', message='游戏结束!\n是否退出游戏!')
if mb:
exit()
else:
game.reset()
update_ui()
# 刷新界面函数
def update_ui():
# 更改各个Label的设置
for r in range(len(game.data)):
for c in range(len(game.data[0])):
number = game.data[r][c]
label = map_labels[r][c]
label['text'] = str(number) if number else ''
label['bg'] = mapcolor[number][0]
label['foreground'] = mapcolor[number][1]
label_score['text'] = str(game.get_score())
# 以下为2048的界面
root = Tk()
root.title('2048')
# root.iconbitmap('./favicon.ico') # 48x48 ico bitmap
frame = Frame(root, width=300, height=300, bg=game_bg_color)
frame.grid(sticky=N+E+W+S)
# 设置焦点能接收按键事件
frame.focus_set()
frame.bind('', on_key_down)
# 以下绑定鼠标按下事件
# frame.bind('', on_mouse_down)
# 以下绑定鼠标移动事件
# frame.bind('', on_mouse_down)
# 以下绑定鼠标抬起事件
frame.bind('', on_mouse_down)
# 初始化图形界面
for r in range(len(game.data)):
row = []
for c in range(len(game.data[0])):
value = game.data[r][c]
text = '' if 0 == value else str(value)
label = Label(frame, text=text, width=4, height=2,
font=('黑体', 30, 'bold'))
label.grid(row=r, column=c, padx=5, pady=5, sticky=N+E+W+S)
row.append(label)
map_labels.append(row)
bottom_row = len(game.data)
print('button', str(bottom_row))
label = Label(frame, text='分数', font=('黑体', 30, 'bold'),
bg='#bbada0', fg='#eee4da')
label.grid(row=bottom_row, column=0, padx=5, pady=5)
label_score = Label(frame, text='0', font=('黑体', 30, 'bold'),
bg='#bbada0', fg='#ffffff')
label_score.grid(row=bottom_row, columnspan=2, column=1, padx=5, pady=5)
def reset_game():
game.reset()
update_ui()
# restart_button = Button(frame, text='重新开始', command=reset_game)
restart_button = Button(frame, text='重新开始', font=('黑体', 16, 'bold'),
# width=4, height=2,
bg='#8f7a66', fg='#f9f6f2', command=reset_game)
restart_button.grid(row=bottom_row, column=3, padx=5, pady=5)
update_ui()
root.mainloop()
![微信截图_20200326153758.png](https://ucc.alicdn.com/pic/developer-ecology/208abc9ccf6c4e6e948ee18184f7ae9b.png)
赞0
踩0