《Python游戏编程入门》4.5-3习题

题目:给bomb catching游戏增加些难度,添加一个vel_x变量并且使用它(和当前的vel_y一起)来移动炸弹,让炸弹按照一定的角度落下!,确保使用一个较小的vel_x值,以免炸弹还没有到达底部就跑出屏幕的左边界和右边界之外了。

没什么好说的,为了避免倾斜出界得不了分顺便加了个反弹。

#Bomb Catcher Game
#Chapter 4
import sys,random,time,pygame,math
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
line_x=bomb_x
line_y=bomb_y-70
vel_y=0.6
vel_x=0.1
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
        line_y+=vel_y
        line_x+=vel_x
        bomb_x+=vel_x
        r=random.randint(0,255)
        g=random.randint(0,255)
        b=random.randint(0,255)

        #炸弹x轴方向进行转向
        if bomb_x>570 or bomb_x<30:
            vel_x=-vel_x

        #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
                line_x=bomb_x
                line_y=bomb_y-70
          
        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
                line_x=bomb_x
                line_y=bomb_y-70
        
        

        color=r,g,b
        #draw the bomb
        start_angle = math.radians(135)
        end_angle = math.radians(180)
        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)
        pygame.draw.arc(screen,color,(line_x,int(line_y),100,100),start_angle,end_angle,5)

        #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()


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值