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坐标
ball1_y = 300
ball2_y = 300
ball3_y = 300
bag_x = 500
bag_y = 550
# 设置文字字体和大小
font = pygame.font.Font(None,35)
while 1:
# 我们对电脑的每一次操作都是一个事件,按键或者移动鼠标
# event 事件
for event in pygame.event.get():
print(event)
# 判断事件类型是不是退出
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 - 5
elif event.key == pygame.K_d:
bag_x = bag_x + 5
# 填充颜色 fill
screen.fill((187, 255, 255))
# 小球的y坐标+1
ball1_y = ball1_y + 1
# 判断小球是否出界
if ball1_y > 600:
ball1_y = 0
ball2_y = ball2_y + 5
if ball2_y > 600:
ball2_y = 0
ball3_y = ball3_y + 10
if ball3_y > 600:
ball3_y = 0
# 判断竹篮是否出界
if bag_x < 0:
bag_x = 0
# 画画(屏幕,颜色, 坐标(x,y),半径)
pygame.draw.circle(screen, (25, 25, 112), (750, ball1_y), 25)
# pygame.draw.circle(screen, (255, 255, 0), (500, ball2_y), 25)
# pygame.draw.circle(screen, (238, 44, 44), (250, ball3_y), 25)
# 画长方形
pygame.draw.rect(screen,(205,133,0),(bag_x,bag_y,75,50))
# 设置文字内容
text = font.render("shanzhi",True,(25,25,122))
# 把文字放在窗口上
screen.blit(text,(0,0))
# 刷新画面
pygame.display.update()
# time.sleep(0.01)
来源:CSDN
作者:少儿编程侯老师
链接:https://blog.csdn.net/houlaos/article/details/104887346