Python小游戏程序锦集Word文档下载推荐.docx
- 文档编号:18644257
- 上传时间:2022-12-30
- 格式:DOCX
- 页数:31
- 大小:22.75KB
Python小游戏程序锦集Word文档下载推荐.docx
《Python小游戏程序锦集Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《Python小游戏程序锦集Word文档下载推荐.docx(31页珍藏版)》请在冰豆网上搜索。
#Drawoutlinearoundmagnifier
mag_left=magnifier_center[0]-MAGNIFIER_SIZE/2
mag_right=magnifier_center[0]+MAGNIFIER_SIZE/2
mag_top=magnifier_center[1]-MAGNIFIER_SIZE/2
mag_bottom=magnifier_center[1]+MAGNIFIER_SIZE/2
mag_topleft=(mag_left,mag_top)
mag_topright=(mag_right,mag_top)
mag_botleft=(mag_left,mag_bottom)
mag_botright=(mag_right,mag_bottom)
box=[mag_topleft,mag_botleft,mag_botright,
mag_topright,mag_topleft]
canvas.draw_polyline(box,4,"
Blue"
)
#eventhandlerfortimer
deftick():
Movecenterofmagnifierpaneslowlydown/right
magnifier_center[0]+=1
magnifier_center[1]+=1
#Createframeformap
frame=simplegui.create_frame("
Mapmagnifier"
CANVAS_WIDTH,CANVAS_HEIGHT)
frame.set_draw_handler(draw)
frame.set_mouseclick_handler(click)
frame.set_mousedrag_handler(drag)
#Createtimerthatslowlyslidesthemagnifierpane
timer=simplegui.create_timer(60.0,tick)
#Starttimerandframeanimation
magnifier_center=[CANVAS_WIDTH/2,CANVAS_HEIGHT/2]
timer.start()
frame.start()
骰子骰子游戏
Dicegame"
Craps"
-usesonlycontrolelementsplusprintstoconsole
importrandom
defgen_roll():
Helperfunctionthatrollstwodiceandprints/returnstheresults
die1=random.randrange(0,6)+1
die2=random.randrange(0,6)+1
print"
Yourolled"
str(die1),"
and"
str(die2)
returndie1+die2
defprocess_roll():
Handlerforroll,encodesbasiclogicforCraps"
globalpoint,point_set,bankroll
#printoutmessagefornewgame"
ifnotpoint_set:
Newgame.Yourbetis"
str(bet)
#logicforfirstroll
roll=gen_roll()
ifroll==7orroll==11:
bankroll+=bet
Youwon.Yourbankrollis"
str(bankroll)
elifroll==2orroll==3orroll==12:
bankroll-=bet
Youlost.Yourbankrollis"
else:
point=roll
point_set=True
Yourpointis"
str(point)
#logicforsubsequentrolls
elifroll==7:
point_set=False
Youcrappedout!
Yourbankrollis"
elifroll==point:
Youmadeyourpoint!
defset_bet(inp):
Inputhandlerforchangingbetsizeviainput,forfeitscurrentbet
globalbet,bankroll,point_set
ifpoint_set:
Forfeitingcurrentbet.Yourbankrollis"
bet=int(inp)
Newbetsizeis"
#createframeandUIelements
200,200)
frame.add_button("
Roll"
process_roll,200)
frame.add_input("
Setbet"
set_bet,200)
#initializeglobalvariablesusedingame
point_set=False
bet=10
bankroll=1000
print"
Yourinitialbankrollis"
bankroll
Yourinitialbetsizeis"
bet
Clickrolltostartthegame"
雪碧动画片
Animationofexplosionusing2Dspritesheet
#load9x9framespritesheetforexplosion-imagegeneratedbyphaedyexplosiongenerator,sourceis
EXPLOSION_CENTER=[50,50]
EXPLOSION_SIZE=[100,100]
EXPLOSION_DIM=[9,9]
explosion_image=simplegui.load_image("
#definedrawhandler
Drawhandlerforsimpleanimationusing2Dspritesheet
globalcounter
explosion_index=[counter%EXPLOSION_DIM[0],counter//EXPLOSION_DIM[0]]
canvas.draw_image(explosion_image,
[EXPLOSION_CENTER[0]+explosion_index[0]*EXPLOSION_SIZE[0],
EXPLOSION_CENTER[1]+explosion_index[1]*EXPLOSION_SIZE[1]],
EXPLOSION_SIZE,EXPLOSION_CENTER,EXPLOSION_SIZE)
counter=(counter+1)%(EXPLOSION_DIM[0]*EXPLOSION_DIM[1])
#createframeandsizeframebasedon100x100pixelsprite
Asteroidsprite"
EXPLOSION_SIZE[0],EXPLOSION_SIZE[1])
#setdrawhandlerandcanvasbackgroundusingcustomHTMLcolor
frame.set_canvas_background("
#initializecounterforanimationandstartframe
counter=0
俄罗斯方块
#FallingblocksasinTetris(BUTNOTREALLY),usearrow\
#keystomovesidewaysandstack
importtime
#Playerpreferences
start_level=0
left_movement="
left"
right_movement="
right"
clockwise_rotation="
up"
cc_rotation="
down"
soft_drop="
x"
hard_drop="
z"
#StandardTetrisgridis10x22blocks,toptworoware
#hidden
#Defineeachblockas20pixels
#settinguptheguistuff
width=10
height=22
block_size=20
frame_width=16
frame_height=22
#scoring
num_cleared=0
score=0
level=0
#defineinitialgrid
grid=[[7forjinrange(height)]foriinrange(width)]
#definedictionarytolookupcolorfromgridvalues
color_Dict={0:
Aqua"
1:
Orange"
2:
3:
Purple"
\
4:
Red"
5:
Lime"
6:
Yellow"
7:
White"
8:
Black"
}
#definehelpers
defdraw_block(c,pos,color):
drawsablockwithpositionposonthecanvasc"
c.draw_polygon([[pos[0],pos[1]],[pos[0]+block_size,\
pos[1]],[pos[0]+block_size,\
pos[1]+block_size],[pos[0],\
pos[1]+block_size]],1,"
color)
#definecallbacks
defdraw(c):
callbackfordrawhandler,drawblocksrepresented\
bygrid"
globalframe_height
globalblock_size
globalpos_list
globalnum_cleared
globalscore
globallevel
globalstart_level
c.draw_line((10*block_size,0),\
(10*block_size,frame_height*block_size),\
15,"
c.draw_text("
NextBlock:
(11*block_size,1*block_size),\
12,"
LinesCleared:
(11*block_size,6*block_size),\
c.draw_text(str(num_cleared),\
(13*block_size,7*block_size),\
Score:
(11*block_size,9*block_size),\
c.draw_text(str(score),(13*block_size,10*block_size),\
Level:
(11*block_size,12*block_size),\
c.draw_text(str(level+start_level),\
(13*block_size,13*block_size),\
#drawingnextblock
globalwidth
next_piece_offset=[(width-2)*block_size,\
(2)*block_size]
#printnext_piece_offset
forposinpos_list.piece_dict[pos_list.next_piece]:
draw_block(c,[pos[0]*block_size+\
next_piece_offset[0],\
pos[1]*block_size+\
next_piece_offset[1]],\
color_Dict[pos_list.next_piece])
foriinrange(width):
forjinrange(height):
draw_block(c,[i*block_size,j*block_size],\
color_Dict[grid[i][j]])
classControls:
def__init__(self,left_movement,right_movement,\
clockwise_rotation,cc_rotation,\
soft_drop,hard_drop):
requiredinitfunction"
self.previous_key=None
self.left_movement=left_movement
self.right_movement=right_movement
self.clockwise_rotation=clockwise_rotation
self.cc_rotation=cc_rotation#stillneedtodo
self.soft_drop=soft_drop
self.hard_drop=hard_drop#stillneedtodo
defkeydown_handler(self,key):
Thekeydownhandler"
ifself.previous_key==None:
self.previous_key=key
self.key=key
self.keydown()#sothatholdingdownisnot
#necessary
self.timer=simplegui.create_timer(1000.0/7,\
self.keydown)
self.timer.start()
defkeydown(self):
keyhandlerthatcontrolsidewaysmotionof
blocks"
globalpos
#findingthesidepieces
lowest_val=width
highest_val=0
left_blocks=[]
right_blocks=[]
forposinpos_list.piece:
ifpos[0]<
lowest_val:
lowest_val=pos[0]
left_blocks.append(pos)
ifpos[0]==highest_val:
ifpos[0]>
highest_val:
highest_val=pos[0]
right_blocks.append(pos)
fall=check_fall()
right,left=check_sideways()
ifself.key==simplegui.KEY_MAP\
[self.left_movement]and\
left_blocks[0][0]!
=0andleft:
#updateoldsquarestobewhite
forblockinpos_list.piece:
grid[block[0]][block[1]]=7
pos_list.move_piece([-1,0])
elifself.key==simplegui.KEY_MAP\
[self.right_movement]and\
right_blocks[0][0]!
=width-1andright:
pos_list.move_piece([1,0])
[self.hard_drop]andfall:
whilefall:
#updateoldsquares
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Python 小游戏 程序
![提示](https://static.bdocx.com/images/bang_tan.gif)