使用Pygame库实现“Bob Catcher“游戏

闲来无事,做一个小游戏 ~~~ 

规则是:移动角色捕捉从屏幕上方掉落的炸弹

以下是代码的详细解释:​

  • 导入必要的库和模块。
  • 初始化Pygame。
  • 设置窗口标题和隐藏鼠标指针。
  • 定义窗口的宽度和高度。
  • 初始化得分和颜色。
  • 设置字体和渲染游戏结束的文本。
    import pygame, sys, random
    from pygame.locals import *
    pygame.init()
    pygame.display.set_caption("Bob Catcher游戏")
    pygame.mouse.set_visible(False)
    x_mode=1100
    y_mode=800
    score=0
    red=255,0,0
    myfont=pygame.font.SysFont("SimHei",200)
    myfont2=pygame.font.SysFont("SimHei",50)
    textImage=myfont.render('Game Over',True,red)
    
  • 初始化角色和炸弹的状态。
  • 设置角色和炸弹的属性,如宽度、颜色、位置、速度和半径。
  • 创建游戏窗口。
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type==pygame.KEYDOWN:
               Life=bobm1=True
               location_1=random.randint(radius,x_mode-radius)
               w=[0,0,0,0]
            if event.type == pygame.MOUSEMOTION:
                x,y=event.pos
                if x<0:
                    x=0
                if x>x_mode-150:
                    x=x_mode-150
                if y>y_mode-40 or y<y_mode-40:
                    y=y_mode-40
    
  • 处理事件,如退出游戏、按键按下和鼠标移动。
  • 根据事件更新角色位置,确保角色在屏幕内移动。

  • 填充屏幕背景色。
  • 渲染得分文本。
  • 绘制角色矩形。
  • 根据炸弹位置和速度更新炸弹位置。
  • 绘制炸弹并检查是否捕捉到炸弹。
  • 如果炸弹掉落到底部,游戏结束。
  • 如果游戏结束,显示游戏结束文本。
  • 更新屏幕显示。
        screen.fill(green)
        textImage2=myfont2.render('得分:%s'%str(score),True,red)
        screen.blit(textImage2,(900,0))
        pos=x,y,150,40
        pygame.draw.rect(screen,blue,pos,width)
        if w[3]==y_mode/2:
            location_1=random.randint(radius,x_mode-radius)
            w[0]=0
            bobm1=True
        if w[0]==y_mode/2:
            location_2=random.randint(radius,x_mode-radius)
            w[1]=0
            bobm2=True
        if w[1]==y_mode/2:
            location_3=random.randint(radius,x_mode-radius)
            w[2]=0
            bobm3=True
        if w[2]==y_mode/2:
            location_4=random.randint(radius,x_mode-radius)
            w[3]=0
            bobm4=True
        if bobm1:
            center_1=(location_1,w[0])
            pygame.draw.circle(screen,black,center_1,radius,width)
            w[0]+=speed
            if  w[0]==760 and x+150>=location_1>=x:
                bobm1=False
                score+=1
        if bobm2:
            center_2=(location_2,w[1])
            pygame.draw.circle(screen,black,center_2,radius,width)
            w[1]+=speed
            if w[1]==760 and x+150>=location_2>=x:
                bobm2=False
                score+=1
        if bobm3:
            center_3=(location_3,w[2])
            pygame.draw.circle(screen,black,center_3,radius,width)
            w[2]+=speed
            if w[2]==760 and x+150>=location_3>=x:
                bobm3=False
                score+=1
        if bobm4:
            center_4=(location_4,w[3])
            pygame.draw.circle(screen,black,center_4,radius,width)
            w[3]+=speed
            if w[3]==760 and x+150>=location_4>=x:
                bobm4=False
                score+=1
        if w[0]==800 or w[1]==800 or w[2]==800 or w[3]==800:
            bobm1=bobm2=bobm3=bobm4=False
            Life=False
        if Life==False:
            screen.fill(green)
            screen.blit(textImage,(100,200))
        pygame.display.update()
    

注意事项

  • 游戏窗口大小为1100x800。
  • 角色初始位置在屏幕中央下方。
  • 炸弹从顶部随机位置开始下落,速度为0.5。
  • 捕捉到炸弹后得分增加。
  • 游戏结束条件是炸弹掉落到角色下方。
  • 使用鼠标移动角色,角色只能在屏幕内移动。

 以下是全部代码:

import pygame, sys,random
from pygame.locals import *
pygame.init()
pygame.display.set_caption("Bob Catcher游戏")
pygame.mouse.set_visible(False)
x_mode=1100
y_mode=800
score=0
red=255,0,0
myfont=pygame.font.SysFont("SimHei",200)
myfont2=pygame.font.SysFont("SimHei",50)
textImage=myfont.render('Game Over',True,red)
# 角色属性
# 角色属性
Life=True
bobm1=True
bobm2=bobm3=bobm4=False
width=40
green=92,255,64
blue=0,0,255
x=x_mode/2-40
y=y_mode-40
# 炸弹属性
speed=0.5
black=0,0,0
radius=30
w=[0,0,0,0]
screen = pygame.display.set_mode((x_mode,y_mode))
location_1=random.randint(radius,x_mode-radius)
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        if event.type==pygame.KEYDOWN:
           Life=bobm1=True
           location_1=random.randint(radius,x_mode-radius)
           w=[0,0,0,0]
           score=0
        if event.type == pygame.MOUSEMOTION:
            x,y=event.pos
            if x<0:
                x=0
            if x>x_mode-150:
                x=x_mode-150
            if y>y_mode-40 or y<y_mode-40:
                y=y_mode-40
    screen.fill(green)
    textImage2=myfont2.render('得分:%s'%str(score),True,red)
    screen.blit(textImage2,(900,0))
    pos=x,y,150,40
    pygame.draw.rect(screen,blue,pos,width)
    if w[3]==y_mode/2:
        location_1=random.randint(radius,x_mode-radius)
        w[0]=0
        bobm1=True
    if w[0]==y_mode/2:
        location_2=random.randint(radius,x_mode-radius)
        w[1]=0
        bobm2=True
    if w[1]==y_mode/2:
        location_3=random.randint(radius,x_mode-radius)
        w[2]=0
        bobm3=True
    if w[2]==y_mode/2:
        location_4=random.randint(radius,x_mode-radius)
        w[3]=0
        bobm4=True
    if bobm1:
        center_1=(location_1,w[0])
        pygame.draw.circle(screen,black,center_1,radius,width)
        w[0]+=speed
        if  w[0]==760 and x+150>=location_1>=x:
            bobm1=False
            score+=1
    if bobm2:
        center_2=(location_2,w[1])
        pygame.draw.circle(screen,black,center_2,radius,width)
        w[1]+=speed
        if w[1]==760 and x+150>=location_2>=x:
            bobm2=False
            score+=1
    if bobm3:
        center_3=(location_3,w[2])
        pygame.draw.circle(screen,black,center_3,radius,width)
        w[2]+=speed
        if w[2]==760 and x+150>=location_3>=x:
            bobm3=False
            score+=1
    if bobm4:
        center_4=(location_4,w[3])
        pygame.draw.circle(screen,black,center_4,radius,width)
        w[3]+=speed
        if w[3]==760 and x+150>=location_4>=x:
            bobm4=False
            score+=1
    if w[0]==800 or w[1]==800 or w[2]==800 or w[3]==800:
        bobm1=bobm2=bobm3=bobm4=False
        Life=False
    if Life==False:
        screen.fill(green)
        screen.blit(textImage,(100,200))
    pygame.display.update()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值