当前位置:首页 » 编程语言 » python期末

python期末

发布时间: 2025-01-09 09:54:01

㈠ 求python大神给个200行左右的代码,多点注释,做期末大作业

#-*-coding:utf-8-*-

importcurses#引入curses模块,curses是一个在Linux/Unix下广泛应用的图形函数库.,作用是可以绘制在DOS下的用户界面和漂亮的图形。
fromrandomimportrandrange,choice#从random模块引入randrange,choice这两个类
#从collections引入defaultdict这个类

letter_codes=[ord(ch)forchin'WASDRQwasdrq']#ord函数是把字符转换成对应的数字
actions=['Up','Left','Down','Right','Restart','Exit']#上,左,下,右,重启,退出
actions_dict=dict(zip(letter_codes,actions*2))#把字母与动作对应起来。zip是把元组中的值对应起来。

#############################
WUp
ALeft
SDown
DRight
RRestart
QExit

wUp
aLeft
sDown
dRight
rRestart
QExit
##############################################
defget_user_action(keyboard):
char="N"#char的初始值为Nwhilecharnotinactions_dict:
char=keyboard.getch()returnactions_dict[char]#阻塞+循环,直到获得用户有效输入才返回对应行为deftranspose(field):return[list(row)forrowinzip(*field)]#zip函数里边加*号,是把行变列,列变行。所以这句代码是行列转置definvert(field):return[row[::-1]forrowinfield]#这句代码是把列表前后颠倒classGameField(object):#创建一个叫做GameField的类,用来创建棋盘def__init__(self,height=4,width=4,win=2048):这个类三个参数self.height=height#高self.width=width#宽self.win_value=win#过关分数self.score=0#当前分数self.highscore=0#最高分self.reset()#重置棋盘defreset(self):#定义一个reset函数ifself.score>self.highscore:#如果当前分数大于最高分,那么把当前分数赋值给最高分self.highscore=self.scoreself.score=0#当前分数恢复到0分self.field=[[0foriinrange(self.width)]forjinrange(self.height)]#横纵坐标恢复到(0,0)self.spawn()#调用spawn这个函数self.spawn()defmove(self,direction):#定义move函数defmove_row_left(row):#向左移deftighten(row):#squeesenon-zeroelementstogether把零散的非零单元挤到一块
new_row=[iforiinrowifi!=0]#如果i不等于零,把他们赋值到new_row这个元组中
new_row+=[0foriinrange(len(row)-len(new_row))]#其余位置用0补充returnnew_row#返回这个元组defmerge(row):#定义merge函数,用来合并单元
pair=False#pair初始值为假
new_row=[]#new_row初始值为空foriinrange(len(row)):#让i在格子里循环ifpair:如果pair为真
new_row.append(2*row[i])#那么把把row【i】的值乘以2,追加到new_row后边self.score+=2*row[i]#并且得分为row【i】的值乘以2
pair=False#pair重新赋值为假else:如果pair为真ifi+1<len(row)androw[i]==row[i+1]:#如果i+1还没到边界,并且此时的row【i】=row【i+1】
pair=True#那么pair为真
new_row.append(0)#new_row后追加零else:
new_row.append(row[i])#否则追加row【i】
assertlen(new_row)==len(row)#提醒两者长度一致returnnew_rowreturntighten(merge(tighten(row)))#反复合并,知道不能合并为止

moves={}
moves['Left']=lambdafield:
[move_row_left(row)forrowinfield]#做移动
moves['Right']=lambdafield:
invert(moves['Left'](invert(field)))#invert是逆转
moves['Up']=lambdafield:
transpose(moves['Left'](transpose(field)))#transpose是转置
moves['Down']=lambdafield:
transpose(moves['Right'](transpose(field)))ifdirectioninmoves:ifself.move_is_possible(direction):#如果移动方向在四个方向上,self.field=moves[direction](self.field)那么调用moves函数self.spawn()#产生随机数returnTrueelse:returnFalsedefis_win(self):returnany(any(i>=self.win_valueforiinrow)forrowinself.field)defis_gameover(self):returnnotany(self.move_is_possible(move)formoveinactions)defdraw(self,screen):
help_string1='(W)Up(S)Down(A)Left(D)Right'
help_string2='(R)Restart(Q)Exit'
gameover_string='GAMEOVER'
win_string='YOUWIN!'defcast(string):
screen.addstr(string+' ')defdraw_hor_separator():
line='+'+('+------'*self.width+'+')[1:]
separator=defaultdict(lambda:line)ifnothasattr(draw_hor_separator,"counter"):
draw_hor_separator.counter=0
cast(separator[draw_hor_separator.counter])
draw_hor_separator.counter+=1defdraw_row(row):
cast(''.join('|{:^5}'.format(num)ifnum>0else'|'fornuminrow)+'|')

screen.clear()
cast('SCORE:'+str(self.score))if0!=self.highscore:
cast('HGHSCORE:'+str(self.highscore))forrowinself.field:
draw_hor_separator()
draw_row(row)
draw_hor_separator()ifself.is_win():
cast(win_string)else:ifself.is_gameover():
cast(gameover_string)else:
cast(help_string1)
cast(help_string2)defspawn(self):
new_element=4ifrandrange(100)>89else2
(i,j)=choice([(i,j)foriinrange(self.width)forjinrange(self.height)ifself.field[i][j]==0])self.field[i][j]=new_elementdefmove_is_possible(self,direction):defrow_is_left_movable(row):
defchange(i):#trueifthere'llbechangeini-thtileifrow[i]==0androw[i+1]!=0:#MovereturnTrueifrow[i]!=0androw[i+1]==row[i]:#(change(i)foriinrange(len(row)-1))

check={}
check['Left']=lambdafield:
any(row_is_left_movable(row)forrowinfield)

check['Right']=lambdafield:
check['Left'](invert(field))

check['Up']=lambdafield:
check['Left'](transpose(field))

check['Down']=lambdafield:
check['Right'](transpose(field))ifdirectionincheck:returncheck[direction](self.field)else:returnFalsedefmain(stdscr):definit():#重置游戏棋盘
game_field.reset()return'Game'defnot_game(state):#画出GameOver或者Win的界面
game_field.draw(stdscr)#读取用户输入得到action,判断是重启游戏还是结束游戏
action=get_user_action(stdscr)
responses=defaultdict(lambda:state)#默认是当前状态,没有行为就会一直在当前界面循环
responses['Restart'],responses['Exit']='Init','Exit'#对应不同的行为转换到不同的状态returnresponses[action]defgame():#画出当前棋盘状态
game_field.draw(stdscr)#读取用户输入得到action
action=get_user_action(stdscr)ifaction=='Restart':return'Init'ifaction=='Exit':return'Exit'ifgame_field.move(action):#movesuccessfulifgame_field.is_win():return'Win'ifgame_field.is_gameover():return'Gameover'return'Game'


state_actions={'Init':init,'Win':lambda:not_game('Win'),'Gameover':lambda:not_game('Gameover'),'Game':game
}

curses.use_default_colors()
game_field=GameField(win=32)


state='Init'#状态机开始循环whilestate!='Exit':
state=state_actions[state]()

curses.wrapper(main)

热点内容
casejava 发布:2025-01-09 22:56:56 浏览:698
oracle如何导出数据库 发布:2025-01-09 22:55:13 浏览:771
编程ppm 发布:2025-01-09 22:49:25 浏览:729
蒸汽之都侦探安卓按键在哪里 发布:2025-01-09 22:48:30 浏览:819
怎么修改域账号的配置文件 发布:2025-01-09 22:30:44 浏览:478
49葫芦娃怎么改密码 发布:2025-01-09 22:30:00 浏览:844
博德之门安卓安装哪里 发布:2025-01-09 22:14:40 浏览:316
服务器电脑用关机吗 发布:2025-01-09 21:53:01 浏览:464
机顶盒用户和密码是什么 发布:2025-01-09 21:52:24 浏览:385
什么游戏配置要求高 发布:2025-01-09 21:42:55 浏览:493