小程序:桌面弹球

小程序:桌面弹球

print("************************************欢迎进入 桌面弹球 ***************************************************")
cla = input("请选择难度等级:1:小菜一碟  2:驾轻就熟  3:略感吃力  4:手忙脚乱  5:头晕目眩  6:目瞪口呆:")
dic = {"1":4,"2":7,"3":12,"4":20,"5":50,"6":100}
import random
num1 = random.randint(200,500)
num2 = random.randint(200,500)
import pygame
pygame.init()
window = pygame.display.set_mode((1000,600)) #创建一个窗口

pygame.display.set_caption('小杨的Freya——桌面弹球')
clock = pygame.time.Clock()#创建一个时钟
ball = pygame.Rect(num1,num2,45,45) #球的参数,以及对球的位置进行初始化
ball1 = pygame.Rect(num1,num2,45,45)
player_1 = pygame.Rect(0,230,20,200) #定义玩家1
player_2 = pygame.Rect(980,230,20,200) #定义玩家2

player_speed = 7
p1_w = 0
p1_s = 0
p2_w = 0
p2_s = 0
p1_score = 0
p2_score = 0
ball_speed_y = dic.get(cla)
ball_speed_x = dic.get(cla)
#球2
ball1_speed_y = 4
ball1_speed_x = 4
import os
now_path = os.path.abspath(__file__) #获得当前文件所在路径,为文件的传输做准备
current_path = os.path.dirname(now_path)

font = pygame.font.SysFont(None,40) #设置窗体字体样式
def text_object(text,color):
    '''
    在画面中创建一个边框
    :return: pass
    '''
    textsurface = font.render(text,True,color)
    return textsurface,textsurface.get_rect()
def print_message(msg,color,place):
    '''
    设置边框的信息
    :return: pass
    '''
    tsurf,tsurf_rect = text_object(msg,color)
    tsurf_rect.center = place
    window.blit(tsurf,tsurf_rect)

def pause():
    '''
    暂停功能
    :return:pass
    '''
    flag = True
    while flag:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_y:
                    flag = False
                elif event.key == pygame.K_n:
                    pygame.exit()
                    exit()
            print_message("You are the loser!!!  Coutinue?(N/Y)",(225,225,225),(500,300))
            pygame.display.update()
            clock.tick(50)

#背景音乐
v = "Gak Ada Waktu Beib - Ghea Youbi.mp3"
result = os.path.join(current_path,v) #对文件的路径进行拼接,防止因为文件的移动导致位置偏移
# file=r'C:\Users\lenovo\Desktop\MusicDownload\Gak Ada Waktu Beib - Ghea Youbi.mp3'       # 音乐的路径
pygame.mixer.init()                   # 初始化
track = pygame.mixer.music.load(result)    # 加载音乐文件
pygame.mixer.music.play()           # 开始播放音乐流\
volume = pygame.mixer.music.set_volume(0.2)#音量
v1 = "bejing.jpg"
result1 = os.path.join(current_path,v1)
# background = pygame.image.load(r"C:\Users\lenovo\Desktop\MusicDownload\bejing.jpg")  # 创建背景图片
background = pygame.image.load(result1)
#主循环
while True:
    clock.tick(90) #刷新频率
    for event in pygame.event.get():
        print(event)
        if event.type == pygame.QUIT:
            print("退出游戏")
            pygame.quit()
            exit()

        elif event.type == pygame.VIDEORESIZE: #背景图片与窗体相适应
            size = width, height = event.size[0], event.size[1]  # 获取背景图新的size
            screen = pygame.display.set_mode(size, pygame.RESIZABLE)

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_s:
                p1_s = player_speed
            if event.key == pygame.K_w:
                p1_w = -player_speed
            # if event.key == pygame.K_y:
            #     pause()
            if event.key == pygame.K_DOWN:
                p2_s = player_speed
            if event.key == pygame.K_UP:
                p2_w = -player_speed
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_s:
                p1_s = 0
            if event.key ==pygame.K_DOWN:
                p2_s = 0
            if event.key == pygame.K_w:
                p1_w = 0
            if event.key ==pygame.K_UP:
                p2_w = 0


    #移动球
    ball.x += ball_speed_x
    ball.y += ball_speed_y
    ball1.x += ball1_speed_x
    ball1.y += ball1_speed_y

    if ball.bottom >= 600 or ball.top <= 0:
        ball_speed_y = ball_speed_y * -1.01
    if ball.right >= 1000 or ball.left <= 0:
        pause()
        ball = pygame.Rect(num1, num2, 45, 45)

    #球二
    # if ball1.bottom >= 600 or ball.top <= 0:
    #     ball1_speed_y = ball1_speed_y * -1.01
    # if ball1.right >= 1000 or ball1.left <= 0:
    #     pause()
    #     ball1= pygame.Rect(num1, num2, 45, 45)

    #碰撞:
    v3 = "11479.wav"
    result3 = os.path.join(current_path, v3)
    # soundwav = pygame.mixer.Sound(r"C:\Users\lenovo\Desktop\MusicDownload\11479.wav") #碰撞音效
    soundwav = pygame.mixer.Sound(result3)
    if ball.colliderect(player_1):
        ball_speed_x *= -1.01
        p1_score += 1
        soundwav.play()
    if ball.colliderect(player_2):
        ball_speed_x *= -1.01
        p2_score += 1
        soundwav.play()
        #球2
    if ball1.colliderect(player_1):
        ball1_speed_x *= -1
        p1_score += 1
        soundwav.play()
    if ball1.colliderect(player_2):
        ball1_speed_x *= -1
        p2_score += 1
        soundwav.play()

    # 移动玩家范围
    if player_1.top >= 0:
        player_1.y += p1_w
    if player_1.bottom <= 600:
        player_1.y += p1_s
    if player_2.top >= 0:
        player_2.y += p2_w
    if player_2.bottom <= 600:
        player_2.y += p2_s


    #呈现背景图片
    window.blit(pygame.transform.scale(background, size), (0, 0))  # 背景图片对齐的坐标
    # pygame.display.flip()

    #呈像
    pygame.draw.ellipse(window,(225,22,0),ball)
    pygame.draw.ellipse(window, (0, 22, 225), ball1)
    pygame.draw.rect(window,(160,32,240),player_1)
    pygame.draw.rect(window, (0,255,255),player_2)

    #打印分数
    print_message(f'P1 : {p1_score} - P2 : {p2_score}',(225,225,225),(500,30))
    #刷新画面
    pygame.display.update()
posted @ 2020-09-14 08:59  小杨的冥想课  阅读(208)  评论(0编辑  收藏  举报