succi - 揭小球2.0

# 导入专门做游戏的工具包
import pygame
import sys  # 系统工具包
import random

# 1。加载中,初始化
pygame.init()
# 2.设置一个窗口大小   dis=分开  play=玩  display展示
screen_height = 600  # 窗口高度
screen_width = 700  # 窗口宽度
screen = pygame.display.set_mode((screen_width, screen_height))
# 3.设置标题
pygame.display.set_caption("接下球游戏")

# 两个变量
ball_x = 350
ball_y = 300
# 板的位置信息
ban_w, ban_h = 120, 50
ban_x, ban_y = screen_width/2, screen_height - ban_h

# 文字1.0 设置文字字体
font = pygame.font.Font("FZSJ-OXKAJW.TTF", 24)

# 分数变量
fen_shu = 0

# 5.无限循环
while True:
    # 电脑监控操作1.0   事件:点击鼠标,移动鼠标,按键
    # event 事件
    for event in pygame.event.get():
        # print(event)
        # 判断是否点击退出 type类型 kind
        if event.type == pygame.QUIT:
            # quit 游戏退出
            pygame.quit()
            # exit 退出
            sys.exit()
        elif event.type == pygame.MOUSEMOTION:
            ban_x, _ = event.pos

    # 背景填充颜色 fill
    screen.fill((225, 30, 35))
    ball_y = ball_y + 1
    if ball_y > screen_height:
        ball_y = 0
        ball_x = random.randint(0, screen_width)

    if ban_x<ball_x<ban_x+ban_w and ban_y<ball_y<ban_y+ban_h:
        fen_shu = fen_shu + 1
        ball_y = 0
        ball_x = random.randint(0, screen_width)


    # 小球 1.0: 参数:窗口/屏幕;颜色 rgb;坐标(x,y);
    pygame.draw.circle(screen, (0, 0, 0), (ball_x, ball_y), 10)
    # 画一个板 : 参数:窗口/屏幕;颜色 rgb;位置信息(x,y,宽,高);
    pygame.draw.rect(screen, (255, 255, 255), (ban_x, ban_y, ban_w, ban_h ))
    # 文字2.0 设置文字内容
    content = font.render("分数:%d"%fen_shu,True,(0,0,0))
    # 文字3.0 把文字放到窗口上  blit 传送
    screen.blit(content, (0, 0))

    # 4.刷新
    pygame.display.update()
发布了516 篇原创文章 · 获赞 21 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/houlaos/article/details/105161262
今日推荐