import random
import pygame
import time
# 1。初始化 loading 加载中
pygame.init()
# 2.设置一个窗口 dis分开 play 展览馆
screen = pygame.display.set_mode((1000, 600))
# 3.设置游戏名字
pygame.display.set_caption('接小球1.0')
# 设置小球1的y坐标
ball_y = 300
ball_x = 750
bag_x = 500
bag_y = 550
# 文字1。0:设置文字字体和大小
font = pygame.font.Font("ziti.ttf", 35)
# score 分数
score = 0
# 时间变量
t1 = time.time()
# 图片1.0 导入图片
head = pygame.image.load("ball3.png")
bg = pygame.image.load("bg.jpeg")
while True:
# 我们对电脑的每一次操作都是一个事件,按键或者移动鼠标
# event 事件
for event in pygame.event.get():
# 判断事件类型是不是退出
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == pygame.MOUSEMOTION:
bag_x, a = event.pos
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
bag_x = bag_x - 100
elif event.key == pygame.K_d:
bag_x = bag_x + 100
# 填充颜色 fill
screen.fill((187, 255, 255))
screen.blit(bg,(0,0))
# 小球的y坐标+1
ball_y = ball_y + 1
# 判断小球是否出界
if ball_y > 600:
ball_y = 0
# 判断竹篮是否出界
if bag_x < 0:
bag_x = 0
# 判断如何接到小球
# 打中一个范围,怎么确定范围呢?
# 判断接中小球
# 如果板的x坐标 小于 球的x坐标 小于 板的x坐标加板宽
if bag_x < ball_x < bag_x + 150 and bag_y < ball_y < bag_y + 50:
score = score + 3
ball_y = 0
ball_x = random.randint(0, 1000)
# 画画(屏幕,颜色, 坐标(x,y),半径)
# pygame.draw.circle(screen, (25, 25, 112), (ball_x, ball_y), 25)
# 图片2.0 将小球放到窗口上
screen.blit(head, (ball_x, ball_y))
# 画长方形
pygame.draw.rect(screen, (205, 133, 0), (bag_x, bag_y, 150, 50))
pygame.draw.rect(screen, (0, 0, 0), (0, 0, 200, 50))
# 文字2.0:设置文字内容
text = font.render("分数:", True, (255, 2, 250))
text2 = font.render("%d" % score, True, (255, 255, 255))
text3 = font.render("剩余时间:%d" %(180 - (time.time()-t1)),True,(0,0,0))
# 文字3.0:把文字放在窗口上
screen.blit(text, (0, 0))
screen.blit(text2, (100, 0))
screen.blit(text3, (700, 0))
# 刷新画面
pygame.display.update()
shanzhi -接小球游戏4.0
猜你喜欢
转载自blog.csdn.net/houlaos/article/details/105031860
今日推荐
周排行