《python游戏编程入门》4.5-1习题
2021/8/6 17:06:00
本文主要是介绍《python游戏编程入门》4.5-1习题,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
题目要求:当炸弹碰到底部时,让程序暂停片刻,显示出一条“Boom!”的消息。
作者建议使用前面keyboard中的演示程序中的定时代码,在炸弹爆炸后暂停数秒然后再继续。
添加等待标志位waiting=False,和等待时间seconds=2
当触底时获取起始时间,clock_start=time.perf_counter()
然后进入waiting=true的判断,不断获取已经经过去的时间current=time.perf_counter()-clock_start
如果限定的时间还有剩余seconds-current>0,那么就显示BOOM!和还有多长时间产生新的炸弹
否则waiting=False退出判断,并且重新产生一个炸弹
bomb_x=random.randint(0, 500)
bomb_y=-50
#Bomb Catcher Game #Chapter 4 import sys,random,time,pygame from pygame.locals import * def print_text(font,x,y,text,color=(255,255,255)): imgText = font.render(text,True,color) screen.blit(imgText,(x,y)) #main program pygame.init() screen=pygame.display.set_mode((600,500)) pygame.display.set_caption("Bomb Catcher Game") font1=pygame.font.Font(None, 24) font2=pygame.font.Font(None, 50) pygame.mouse.set_visible(False) white=255,255,0 red=220,50,50 yellow=230,230,50 black=0,0,0 lives=3 score=0 game_over=True mouse_x=mouse_y=0 pos_x=300 pos_y=460 bomb_x=random.randint(0, 500) bomb_y=-50 vel_y=0.7 waiting=False seconds=2 #repeating loop while True: for event in pygame.event.get(): if event.type==QUIT: sys.exit() elif event.type==MOUSEMOTION: mouse_x,mouse_y=event.pos move_x,move_y=event.rel elif event.type==MOUSEBUTTONUP: if game_over: game_over=False lives=3 score=0 keys=pygame.key.get_pressed() if keys[K_ESCAPE]: sys.exit() screen.fill((0,0,100)) if game_over: print_text(font1, 100, 200, "<Click to play>") else: #move the bomb bomb_y+=vel_y #has the player miss the bomb? if bomb_y>500 and waiting==False: lives-=1 waiting=True clock_start=time.perf_counter()#炸弹触底后进行起始计时 if lives ==0: game_over=True #see if player has catch the bomb elif bomb_y>pos_y and waiting==False: if bomb_x>pos_x and bomb_x<pos_x+120: score+=10 bomb_x=random.randint(0, 500) bomb_y=-50 if waiting==True: current=time.perf_counter()-clock_start if seconds-current>0: print_text(font2, 250, 230, "BOOM!",yellow) print_text(font1, 250, 270, "wait for "+str(round(seconds-current,1))+" s") else: waiting=False bomb_x=random.randint(0, 500) bomb_y=-50 #draw the bomb pygame.draw.circle(screen, black, (bomb_x-4,int(bomb_y)-4), 30,0) pygame.draw.circle(screen, yellow, (bomb_x,int(bomb_y)), 30,0) #set basket postion pos_x=mouse_x if pos_x<0: pos_x=0 elif pos_x>500: pos_x=500 #draw the basket pygame.draw.rect(screen,black,(pos_x-4,pos_y-4,120,40),0) pygame.draw.rect(screen,red,(pos_x,pos_y,120,40),0) #print of lives print_text(font1, 0, 0, "lives:"+str(lives)) #print score print_text(font1, 500, 0, "score:"+str(score)) pygame.display.update()
这篇关于《python游戏编程入门》4.5-1习题的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-20Python编程入门指南
- 2024-12-20Python编程基础与进阶
- 2024-12-19Python基础编程教程
- 2024-12-19python 文件的后缀名是什么 怎么运行一个python文件?-icode9专业技术文章分享
- 2024-12-19使用python 把docx转为pdf文件有哪些方法?-icode9专业技术文章分享
- 2024-12-19python怎么更换换pip的源镜像?-icode9专业技术文章分享
- 2024-12-19Python资料:新手入门的全面指南
- 2024-12-19Python股票自动化交易实战入门教程
- 2024-12-19Python股票自动化交易入门教程
- 2024-12-18Python量化入门教程:轻松掌握量化交易基础知识