Bomb Catcher游戏

本文介绍了一个使用Python和Pygame库实现的简易弹球游戏。游戏包含基本的碰撞检测、得分统计及生命值功能。玩家通过鼠标控制底部挡板左右移动来反弹小球,避免其落入底部导致失去生命值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

# -*- coding: utf-8 -*-
"""
Created on Tue Jun 30 23:42:49 2015

@author: liuchang
"""


import pygame,sys,random
from pygame.locals import *

pygame.init()
color= 255,255,0
#绘制圆心的位置
positionx=300
positiony=0
radius = 30
width=5

vel_x = 2
vel_y = 2

rec_x=250
rec_y=500
rec_a=100
rec_b=20

mouse_x=0
mouse_y=0

lives = 10
score = 0
#判断是否需要重启游戏
game_over =False 

i=0
font1 =pygame.font.Font(None,30) 
screen= pygame.display.set_mode((600,520))

pygame.display.set_caption("bomb game")


def print_text(font,x,y,text,color=(255,255,255)):
    imgText=font.render(text,True,color)
    screen.blit(imgText,(x,y))


while True:
     
     for event in pygame.event.get():
         if event.type in (QUIT,KEYDOWN):
             sys.exit()
         elif event.type== MOUSEMOTION:
             mouse_x,mouse_y = event.pos
     #print (positionx)  
     positionx +=vel_x
     positiony+=vel_y
         
     if positionx<radius or positionx>600-radius:
             vel_x =- vel_x#将+x转换成-x,非常巧妙
     if positiony<0 or positiony>500-radius:
             vel_y =- vel_y    
     screen.fill((0,0,200))  
     #draw boom
     pygame.draw.circle(screen,color ,(positionx,positiony),radius,0)
     #draw rectangle
     rec_x = mouse_x
     if rec_x <0:
         rec_x=0
     elif rec_x > 500:
         rec_x = 500
     pygame.draw.rect(screen,color,(rec_x,rec_y,rec_a,rec_b),0)
     
     #score and lives 
     if positiony == 500-radius :
         if positionx>rec_x and positionx<rec_x+100:
             score+=1
             positionx = random.randint(0,500)
             positiony=0
         else: 
             lives -=1
             positionx = random.randint(0,500)#重新出球
             positiony=0
             if lives == 0:
                 game_over =True
                 print ("game over")
     if game_over :
      print_text(font1,100,200,"<click to play>")
      lives=0
      score = 0             
     #draw score and lives
     print_text(font1,0,0,"lives: "+str(lives))
     print_text(font1,500,0,"score :"+str(score))
     for i in range(100):#调节速率
        i+=1
        print(i)
     pygame.display.update()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值