Qiyuan-

More children's programming can contact Hou, micro-channel data_ecology
This link: https://blog.csdn.net/houlaos/article/details/102753286
import pygame
import time
import random

# loading 加载 初始化
pygame.init()  # 1.初始化游戏
screen = pygame.display.set_mode((600, 500))  # 设置窗口大小
pygame.display.set_caption("蔡旭坤大招乔碧落")  # 设置标题
# 事件
ball_x = 300  # 小球x坐标
ball_y = 250  # 小球y左边
rect_x, rect_y, rect_w, rect_h = 300, 460, 120, 40  # 接板的x,y坐标,宽度,高度
font1 = pygame.font.Font(None, 24)  # 设置字体参数
score = 0
lives = 3

def ball(ball_x, ball_y):
    pygame.draw.circle(screen, (255., 25, 52), (ball_x, ball_y), 20, 0)


while True:  # 不断循环
    for event in pygame.event.get():
        print(event)
        if event.type == pygame.QUIT:  # 判断是否点击退出
            pygame.quit()
        elif event.type == pygame.MOUSEMOTION:  # 监听鼠标动作
            rect_x, _ = event.pos
    screen.fill((34, 177, 135))  # regb
    ball_y = ball_y + 1
    if ball_y > 500:  #
        ball_y = 0
        ball_x = random.randint(0, 600)
        lives = lives -1
    ball(ball_x, ball_y)

    # 判断小球坐标在挡板的左边之间
    if rect_x < ball_x < rect_x + rect_w and rect_y < ball_y < rect_y + rect_h:
        score = score + 1
        ball_y = 0
        ball_x = random.randint(0, 600)
    Text_score = font1.render('score:%d' % score, True, (0, 0, 0))  # 设置加入文字的参数
    screen.blit(Text_score, (0, 0)) # 把文字放到屏幕上
    Text_lives = font1.render('lives:%d' % lives, True, (0, 0, 0))
    screen.blit(Text_lives, (530, 0))

    pygame.draw.rect(screen, (100, 200, 30), (rect_x, rect_y, rect_w, rect_h), 0)
    pygame.display.update()  # 刷新画面
    # time.sleep(0.1)

pygame.quit()  # 2。退出游戏

# 游戏
# 操控游戏,判断输赢
# 添加图片
# bgm

Guess you like

Origin blog.csdn.net/houlaos/article/details/102753286