# 导入专门做游戏的工具包
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()
succi - 接小球2.0
最新推荐文章于 2020-07-18 14:53:10 发布