Python小游戏程序锦集_第1页
Python小游戏程序锦集_第2页
Python小游戏程序锦集_第3页
Python小游戏程序锦集_第4页
Python小游戏程序锦集_第5页
已阅读5页,还剩19页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、可拖动地图的放大镜"""Demonstration of a draggable magnifier on a map"""import simplegui# 1521x1818 pixel map of native American language# Source - Gutenberg projectMAP_WIDTH = 1521MAP_HEIGHT = 1818map_image = simplegui.load_image("# ConstantsMAP_SCALE = 3CANVAS_WIDTH = MAP_

2、WIDTH / MAP_SCALECANVAS_HEIGHT = MAP_HEIGHT / MAP_SCALEMAGNIFIER_SIZE = 120# Event handlersdef click(pos): """ Reset center of magnifier pane to current click position """ magnifier_center0 = pos0 magnifier_center1 = pos1def drag(pos): """ Reset center of

3、 magnifier pane to current click position """ magnifier_center0 = pos0 magnifier_center1 = pos1def draw(canvas): """ Draw handler - draws map, magnifier pane, and box around magnifier """ # Draw map canvas.draw_image(map_image, (MAP_WIDTH / 2, MAP_HEIGHT

4、/ 2), (MAP_WIDTH, MAP_HEIGHT), (CANVAS_WIDTH / 2, CANVAS_HEIGHT / 2), (CANVAS_WIDTH, CANVAS_HEIGHT) # Draw magnifier source_center = (MAP_SCALE * magnifier_center0, MAP_SCALE * magnifier_center1) canvas.draw_image(map_image, source_center, MAGNIFIER_SIZE, MAGNIFIER_SIZE, magnifier_center, MAGNIFIER_

5、SIZE, MAGNIFIER_SIZE) # Draw outline around magnifier mag_left = magnifier_center0 - MAGNIFIER_SIZE / 2 mag_right = magnifier_center0 + MAGNIFIER_SIZE / 2 mag_top = magnifier_center1 - MAGNIFIER_SIZE / 2 mag_bottom = magnifier_center1 + MAGNIFIER_SIZE / 2 mag_topleft = (mag_left, mag_top) mag_toprig

6、ht = (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") # event handler for timerdef tick(): """ Move center of magnifie

7、r pane slowly down/right """ magnifier_center0 += 1 magnifier_center1 += 1# Create frame for mapframe = simplegui.create_frame("Map magnifier", CANVAS_WIDTH, CANVAS_HEIGHT)frame.set_draw_handler(draw)frame.set_mouseclick_handler(click)frame.set_mousedrag_handler(drag)# Creat

8、e timer that slowly slides the magnifier panetimer = simplegui.create_timer(60.0,tick)# Start timer and frame animationmagnifier_center = CANVAS_WIDTH / 2, CANVAS_HEIGHT / 2timer.start()frame.start()骰子骰子游戏"""Dice game "Craps" - uses only control elements plus prints to conso

9、le"""import simpleguiimport randomdef gen_roll(): """ Helper function that rolls two dice and prints/returns the results """ die1 = random.randrange(0, 6) + 1 die2 = random.randrange(0, 6) + 1 print "You rolled", str(die1), "and", str(

10、die2) return die1 + die2def process_roll(): """ Handler for roll, encodes basic logic for Craps""" global point, point_set, bankroll # print out message for new game" if not point_set: print print "New game. Your bet is", str(bet) # logic for first roll r

11、oll = gen_roll() if not point_set: if roll = 7 or roll = 11: bankroll += bet print "You won. Your bankroll is", str(bankroll) elif roll = 2 or roll =3 or roll = 12: bankroll -= bet print "You lost. Your bankroll is", str(bankroll) else: point = roll point_set = True print "Y

12、our point is", str(point) # logic for subsequent rolls elif roll = 7: bankroll -= bet point_set = False print "You crapped out! Your bankroll is", str(bankroll) elif roll = point: bankroll += bet point_set = False print "You made your point! Your bankroll is", str(bankroll)

13、def set_bet(inp): """ Input handler for changing bet size via input, forfeits current bet """ global bet, bankroll, point_set print if point_set: point_set = False bankroll -= bet print "Forfeiting current bet. Your bankroll is", str(bankroll) bet = int(inp) p

14、rint "New bet size is", str(bet)# create frame and UI elementsframe = simplegui.create_frame("Craps", 200, 200)frame.add_button("Roll", process_roll, 200)frame.add_input("Set bet", set_bet, 200)# initialize global variables used in gamepoint_set = Falsebet = 1

15、0bankroll = 1000print "Your initial bankroll is", bankrollprint "Your initial bet size is", betprint "Click roll to start the game"frame.start()雪碧动画片"""Animation of explosion using 2D sprite sheet"""import simplegui# load 9 x 9 frame sprite

16、 sheet for explosion - image generated by phaedy explosion generator, source is EXPLOSION_CENTER = 50, 50EXPLOSION_SIZE = 100, 100EXPLOSION_DIM = 9, 9explosion_image = simplegui.load_image("# define draw handlerdef draw(canvas): """ Draw handler for simple animation using 2D spri

17、te sheet """ global counter explosion_index = counter % EXPLOSION_DIM0, counter / EXPLOSION_DIM0 canvas.draw_image(explosion_image, EXPLOSION_CENTER0 + explosion_index0 * EXPLOSION_SIZE0, EXPLOSION_CENTER1 + explosion_index1 * EXPLOSION_SIZE1, EXPLOSION_SIZE, EXPLOSION_CENTER, EXPLOSI

18、ON_SIZE) counter = (counter + 1) % (EXPLOSION_DIM0 * EXPLOSION_DIM1) # create frame and size frame based on 100x100 pixel spriteframe = simplegui.create_frame("Asteroid sprite", EXPLOSION_SIZE0, EXPLOSION_SIZE1)# set draw handler and canvas background using custom HTML colorframe.set_draw_

19、handler(draw)frame.set_canvas_background("Blue")# initialize counter for animation and start framecounter = 0frame.start()俄罗斯方块# Falling blocks as in Tetris(BUT NOT REALLY), use arrow #keys to move sideways and stackimport simpleguiimport randomimport time#Player preferencesstart_level = 0

20、left_movement = "left"right_movement = "right"clockwise_rotation = "up"cc_rotation = "down"soft_drop = "x"hard_drop = "z"# Standard Tetris grid is 10x22 blocks, top two row are # hidden# Define each block as 20 pixels#setting up the gui stu

21、ffwidth = 10height = 22block_size = 20frame_width = 16frame_height = 22#scoringnum_cleared = 0score = 0level = 0#define initial gridgrid = 7 for j in range(height) for i in range(width)# define dictionary to lookup color from grid valuescolor_Dict = 0:"Aqua", 1:"Orange", 2:"

22、Blue", 3:"Purple", 4:"Red", 5:"Lime", 6:"Yellow", 7:"White", 8: "Black"# define helpersdef draw_block(c,pos,color): """ draws a block with position pos on the canvas c """ c.draw_polygon(pos0,pos1,pos0+blo

23、ck_size, pos1,pos0+block_size, pos1+block_size,pos0, pos1+block_size,1,"White",color)# define callbacksdef draw(c): """ callback for draw handler, draw blocks represented by grid """ global frame_height global block_size global pos_list global num_cleared glob

24、al score global level global start_level c.draw_line(10*block_size,0), (10*block_size, frame_height*block_size), 15, "Black") c.draw_text("Next Block:", (11*block_size, 1*block_size) , 12, "Black") c.draw_text("Lines Cleared:", (11*block_size, 6*block_size), 1

25、2, "Black") c.draw_text(str(num_cleared), (13*block_size, 7*block_size) , 12, "Black") c.draw_text("Score:", (11*block_size, 9*block_size), 12, "Black") c.draw_text(str(score), (13*block_size, 10*block_size), 12, "Black") c.draw_text("Level:&quo

26、t;, (11*block_size, 12*block_size), 12, "Black") c.draw_text(str(level+start_level), (13*block_size, 13*block_size) , 12, "Black") #drawing next block global width next_piece_offset = (width-2)*block_size, (2)*block_size #print next_piece_offset for pos in pos_list.piece_dictpos_

27、list.next_piece: draw_block(c, pos0*block_size + next_piece_offset0, pos1*block_size + next_piece_offset1, color_Dictpos_list.next_piece) for i in range(width): for j in range(height): draw_block(c,i*block_size,j*block_size, color_Dictgridij)class Controls: def _init_(self, left_movement, right_move

28、ment, clockwise_rotation, cc_rotation, soft_drop, hard_drop): """required init function""" self.previous_key = None self.left_movement = left_movement self.right_movement = right_movement self.clockwise_rotation = clockwise_rotation self.cc_rotation = cc_rotation #still

29、 need to do self.soft_drop = soft_drop self.hard_drop = hard_drop #still need to do def keydown_handler(self, key): """The keydown handler""" if self.previous_key = None: self.previous_key = key self.key = key self.keydown() #so that holding down is not #necessary self.

30、timer = simplegui.create_timer(1000.0/7, self.keydown) self.timer.start() def keydown(self): """ key handler that control sideways motion of blocks """ global pos_list global pos #finding the side pieces lowest_val = width highest_val = 0 left_blocks = right_blocks = fo

31、r pos in pos_list.piece: if pos0 < lowest_val: lowest_val = pos0 left_blocks = left_blocks.append(pos) if pos0 = highest_val: left_blocks.append(pos) if pos0 > highest_val: highest_val = pos0 right_blocks = right_blocks .append(pos) if pos0 = highest_val: right_blocks .append(pos) fall = check

32、_fall() right, left = check_sideways() if self.key =simplegui.KEY_MAP self.left_movement and left_blocks00 != 0 and left: #update old squares to be white for block in pos_list.piece: gridblock0block1= 7 pos_list.move_piece(-1,0) elif self.key = simplegui.KEY_MAP self.right_movement and right_blocks0

33、0 != width-1 and right: #update old squares to be white for block in pos_list.piece: gridblock0block1= 7 pos_list.move_piece(1,0) elif self.key = simplegui.KEY_MAP self.hard_drop and fall: while fall: #update old squares to be white for block in pos_list.piece: gridblock0block1= 7 pos_list.move_piec

34、e(0,1) fall = check_fall() elif self.key = simplegui.KEY_MAP self.soft_drop and fall: #update old squares to be white for block in pos_list.piece: gridblock0block1= 7 pos_list.move_piece(0,1) elif self.key = simplegui.KEY_MAP self.clockwise_rotation: pos_list.rotate(True) elif self.key = simplegui.K

35、EY_MAP self.cc_rotation: pos_list.rotate(False) def keyup(self, key): if key = self.previous_key: self.timer.stop() self.previous_key = Noneclass Blocks: def _init_(self): #0:"I", 1:"L", 2:"J", 3:"T", 4:"Z", 5:"S", 6:"O" #O is a c

36、haracter not an int #self.block_dict = 0:"Aqua", 1:"Orange", #2:"Blue", 3:"Purple", 4:"Red", #5:"Lime", 6:"Yellow" self.choice = random.randint(0,6) #last number #should be 6 and first 0 self.next_piece = random.randint(0,6) self.

37、piece_dict = 0:3,0,4,0,5,0,6,0, 1:4,1,5,1,6,1,6,0, 2:4,0,4,1,5,1,6,1, 3:4,1,5,1,6,1,5,0, 4:4,0,5,0,5,1,6,1, 5:5,0,6,0,4,1,5,1, 6:4,0,5,0,4,1,5,1 def create_piece(self): """Puts all 4 blocks together at the initial drop zone""" self.choice = self.next_piece self.next_pie

38、ce = random.randint(0,6) self.piece = self.piece_dictself.choice if self.choice = 0: self.piece = 3,0,4,0,5,0,6,0 self.rotation_offset = 2,-1,1,0,0,1,-1,2 self.cc_rotation_offset = 1,2,0,1,-1,0,-2,-1 if self.choice = 1: self.piece = 4,1,5,1,6,1,6,0 self.rotation_offset = 1,-1,0,0,-1,1,0,2 self.cc_ro

39、tation_offset = 1,1,0,0,-1,-1,-2,0 if self.choice = 2: self.piece = 4,0,4,1,5,1,6,1 self.rotation_offset = 2,0,1,-1,0,0,-1,1 self.cc_rotation_offset = 0,2,1,1,0,0,-1,-1 if self.choice = 3: self.piece = 4,1,5,1,6,1,5,0 self.rotation_offset = 1,-1,0,0,-1,1,1,1 self.cc_rotation_offset = 1,1,0,0,-1,-1,-

40、1,1 if self.choice = 4: self.piece = 4,0,5,0,5,1,6,1 self.rotation_offset = 2,0,1,1,0,0,-1,1 self.cc_rotation_offset = 0,2,-1,1,0,0,-1,-1 if self.choice = 5: self.piece = 5,0,6,0,4,1,5,1 self.rotation_offset = 1,1,0,2,1,-1,0,0 self.cc_rotation_offset = -1,1,-2,0,1,1,0,0 if self.choice = 6: self.piec

41、e = 4,0,5,0,4,1,5,1 self.rotation_offset = 0,0,0,0,0,0,0,0 self.cc_rotation_offset = 0,0,0,0,0,0,0,0 self.move_piece(0,0) #draws the piece on #the starting point def move_piece(self, offset): """This function updates the piece's blocks' position the offset is a list with two e

42、lements. ex 0,1""" global grid tmp = self.piece for block_idx in range(len(tmp): self.pieceblock_idx = tmpblock_idx0+ offset0, tmpblock_idx1+ offset1 for block in self.piece: #coloring gridblock0block1 = self.choice def rotate(self, CW): """Rotates the piece clockwise C

43、W = True if clockwise, False otherwise""" global grid if CW = True: rot_offset = self.rotation_offset else: rot_offset = self.cc_rotation_offset if check_rotation(CW): #update old squares to be white for block in self.piece: gridblock0block1= 7 #applying the offset tmp_piece = self.pi

44、ece for pos_idx in range(len(tmp_piece): self.piecepos_idx0 =self.piecepos_idx 0 + rot_offsetpos_idx0 self.piecepos_idx1 =self.piecepos_idx 1 + rot_offsetpos_idx1 #change rotation offsets for next time if CW: for i in self.rotation_offset, self.cc_rotation_offset: rot_tmp = i for rot_idx in range(len(rot_tmp): irot_idx1 = irot_idx1 * (-1) irot_idx.reverse() else: for i in self.rotation_offset, self.cc_rotation_offset: rot_tmp = i for rot_idx in range(len(rot_tmp): irot_idx0

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论