python:写坤打球

半腔热情 提交于 2019-12-24 18:06:02
import pygame,sys
#窗体设置
pygame.init()
screen_width=1000
screen_height=560
screen=pygame.display.set_mode((screen_width ,screen_height))
pygame.display.set_caption("打球")

#添加音乐
pygame.mixer.init()#初始化混响器
pygame.mixer.music.load("bg.mp3")#加载文件
pygame.mixer.music.set_volume(0.4)#音量大小
pygame.mixer.music.play(-1,0)#循环播放


sound=pygame.mixer.Sound("素材/加分.wav")


def start():

    #基本素材
    bg=pygame.image.load("素材/background.png")#背景
    # ikun_l=pygame.image.load("素材/l1.png")
    # ikun_r = pygame.image.load("素材/r1.png")
    ikun_l=[]
    ikun_r=[]
    for i in range(1,4):#循环添加图片到列表
        ikun_l.append(pygame.image.load("素材/l"+str(i)+".png"))
        ikun_r.append(pygame.image.load("素材/r"+str(i) + ".png"))
    basketball=pygame.image.load("素材/ball.png")
    #球的位置
    ball_x=240
    ball_y=0
    ball_x_speed=15
    ball_y_speed =15
    which_img=ikun_r[0]#初始图片,就是坤刚开始时的图片
    img_num=0
    #碰撞时坤的图片变化导入
    ikun_hit_r=pygame.image.load("素材/r4.png")
    ikun_hit_l =pygame.image.load("素材/l4.png")

    #坤的坐标
    ikun_x = 500 - which_img.get_size()[0] // 2
    ikun_y = 560 - which_img.get_size()[1]
#字典处理坤移动
    ikun_move={pygame.K_LEFT:0,pygame.K_RIGHT:0}
    speed=20

    while True:
        pygame.time.delay(100)#延时100毫秒
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                sys.exit()
                # 按下键时设置键值为1,放下设置成2
            if event.type == pygame.KEYDOWN:  # 判断按键
                if event.key in ikun_move:  # 如果按下的键再字典中
                    ikun_move[event.key] = 1
            elif event.type == pygame.KEYUP:  # 按键谈起时
                if event.key in ikun_move:
                    ikun_move[event.key] = 0

        #切换图片
        keys=pygame.key.get_pressed()#当按下时
        if keys[pygame.K_LEFT]:
            if img_num<2:
                img_num += 1
            else:
                img_num=0
            which_img = ikun_l[img_num]  # 向左的第几张图片
        if keys[pygame.K_RIGHT]:
            if img_num<2:
                img_num += 1
            else:
                img_num=0
            which_img = ikun_r[img_num]  # 向左的第几张图片
        #绘制背景
        screen.blit(bg,(0,0))
        screen.blit(basketball,(ball_x,ball_y))#球
        ball_x+=ball_x_speed
        ball_y+=ball_y_speed
        #判断边界调节
        if ball_x>1000-basketball.get_width():#如果大于边框减去篮球的宽度
            ball_x_speed=-ball_x_speed
            ball_x =1000 - basketball.get_width()
        elif ball_x<0:
            ball_x_speed = -ball_x_speed
            ball_x=0
        if ball_y>560-basketball.get_height():#如果大于边框减去篮球的高度
            ball_y_speed=-ball_y_speed
        elif ball_y<0:
            ball_y_speed = -ball_y_speed
            ball_y=0


        #边界检测
        if ikun_x>1000-which_img.get_width():#屏幕宽度减去坤的宽度,不减坤会出屏幕才停止
            ikun_x=1000-which_img.get_width()
        elif ikun_x<0:
            ikun_x=0


        #定位坐标
        ikun_x-=ikun_move[pygame.K_LEFT] * speed #移动
        ikun_x+= ikun_move[pygame.K_RIGHT] * speed
        #screen.blit(which_img,(ikun_x,ikun_y))#调用坤

        if (ball_x>ikun_x and ball_x<ikun_x+which_img.get_width())and(ball_y>ikun_y-basketball.get_height()and ball_y<ikun_y ):#如果碰撞
            ball_x_speed=-ball_x_speed
            ball_y_speed=-ball_y_speed
            sound.play()
            if ikun_move[pygame.K_LEFT]:
                which_img=ikun_hit_l
            else:
                which_img=ikun_hit_r
        screen.blit(which_img, (ikun_x, ikun_y))  # 调用坤
        if ball_y>560-50:
            return""#返回空值表示游戏结束   return返回一个值到定义的函数中,就是我也说不清楚,就是函数会返回值,本身函数是不会返回的,加上这个就会了
        pygame.display.update()#刷新
def game_over():
    start_btn=pygame.image.load("素材/start.png")#加载按钮图片
    bg=pygame.image.load("素材/background.png")
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type==pygame.MOUSEBUTTONDOWN:#单击位置?
                if (370<=event.pos[0]<=370+start_btn.get_width()) and (250<=event.pos[1]<250+start_btn.get_height()):#鼠标x位置在370到图片右边的中间时 and 同理
                    return True#返回True
        screen.blit(bg,(0,0))#绘制背景
        screen.blit(start_btn,(370,250))#绘制按钮
        pygame.display.update()

if __name__ == '__main__':
    flag=True
    ###start()#开局调用函数 就他妈因为你,老子找了半天重新两次的原因
    while flag:
        game_result=start()#调用函数
        if game_result=="":#如果返的是空值
            flag=game_over()

素材打包

百度云资源:https://pan.baidu.com/s/11zNx4jhBy7bOqO7icUmCnw

在这里插入图片描述

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!