Qiyuan-接小球游戏3.0

更多内容可以联系少儿编程侯老师,微信data_ecology
本文链接: https://blog.csdn.net/houlaos/article/details/102771324
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('ziti.ttf', 24)  # 设置字体参数
score = 0
lives = 3  # 修改属性
ball1 = pygame.image.load('ball.bmp')
ball2 = pygame.image.load('ball2.png')
game_over = True

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.MOUSEBUTTONUP: # 鼠标抬起
            if game_over:
                game_over=False

        elif event.type == pygame.MOUSEMOTION:  # 监听鼠标动作
            rect_x, _ = event.pos
    screen.fill((240, 240, 240))  # regb
    if game_over:
        Text_start = font1.render('点击游戏', True, (0, 0, 0))
        screen.blit(Text_start, (200, 200))
    else:
        ball_y = ball_y + 1
        if ball_y > 500:  #
            ball_y = 0
            ball_x = random.randint(0, 600)
            lives = lives -1
            if lives == 0:
                game_over = True
                score = 0
                lives = 3
        if score<10:
            ball(ball_x, ball_y)
        elif score < 20:
            screen.blit(ball1,(ball_x, ball_y))
        else:
            screen.blit(ball2, (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('分数:%d' % score, True, (0, 0, 0))  # 设置加入文字的参数
        screen.blit(Text_score, (0, 0)) # 把文字放到屏幕上
        Text_lives = font1.render('生命值:%d' % lives, True, (0, 0, 0))
        screen.blit(Text_lives, (450, 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

猜你喜欢

转载自blog.csdn.net/houlaos/article/details/102771324