pygame小项目 ~ 2 :Python完成简易乒乓球游戏

效果图

将俩个球拍的图像对象绘制在screen上

球拍击中乒乓球只需要进行坐标的接触判断,可以将x, y的接触范围调小, 避免接触时的突兀

下面上代码

import pygame
import time
clock = pygame.time.Clock()
step = 0.8

#背景音乐
pygame.mixer.init()# 初始化
pygame.mixer.music.set_volume(0.3) #设置音量大小
pygame.mixer.music.get_volume() #获取音量
pygame.mixer.music.load('./sound/Intro Theme.mp3')# 插入并加载音乐文件
pygame.mixer.music.play(-1)# 开始播放音乐流 单曲循环

#球拍击中音效
pygame.mixer.init()
# hit_sound = pygame.mixer.Sound('./sound/hit_voice.wav')


# 1. 初始化界面
pygame.init()
screen = pygame.display.set_mode((1000, 800))  #创建窗口宽高
pygame.display.set_caption('打乒乓') #游戏标题
bgImg = pygame.image.load('./sprites/bg.jpg') #插入背景图片


# 3. 玩家1 右边
player1Img = pygame.image.load('./sprites/player1.png')
player1X = 600
player1Y = 300
player1StepX = 0
player1StepY = 0

# 玩家2  左边
player2Img = pygame.image.load('./sprites/player2 1.png')
player2X = 100
player2Y = 300
player2StepX = 0
player2StepY = 0

# 创建球
ballImg = pygame.image.load('./sprites/ball1.png')
ballX = 450
ballY = 300
ballStep = 0.8 #球移动的速度
def show_ball():
    global ballX, ballStep, ballY


# 游戏结束
is_over = False
over_font = pygame.font.Font('freesansbold.ttf', 64)

def check_is_over():
    if is_over:
        text = 'Game over'
        score_render = over_font.render(text, True, (255, 0, 0))
        screen.blit(score_render, (310, 200))


#添加分数并显示
player1score = 0
player2score = 0
font = pygame.font.Font('freesansbold.ttf', 32)
def show_score():
    global is_over, ballX, ballY
    text = f'player1: {player1score}  player2: {player2score}'
    score_render = font.render(text, True, (255, 255, 255))
    screen.blit(score_render, (50, 50))
    if (player1score == 3 or player2score == 3):
        is_over = True
        print('游戏结束')
        pygame.mixer.music.stop() #背景音乐暂停
        ballX = 500
        ballY = 400


def f():
    global ballStep
    if ballStep < 0: ballStep -= 0.1
    else : ballStep += 0.1

# 2. 游戏主循环
running = True
f1 = d1 = False
f2 = d2 = True
while running:
    screen.blit(bgImg, (0, 0))
    show_score() #显示分数
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    #操作键盘控制玩家移动
    keys = pygame.key.get_pressed()
    if keys[pygame.K_RIGHT]:
        print("R")
        player1X += 1
    elif keys[pygame.K_LEFT]:
        print("L")
        player1X -= 1
    elif keys[pygame.K_UP]:
        player1Y -= 1
    elif keys[pygame.K_DOWN]:
        player1Y += 1
    keys = pygame.key.get_pressed()
    if keys[pygame.K_a]:
        player2X -= 1
    elif keys[pygame.K_d]:
        player2X += 1
    elif keys[pygame.K_w]:
        player2Y -= 1
    elif keys[pygame.K_s]:
        player2Y += 1

    #防止球拍1出界
    if player1X > 800:
        player1X = 800
    if player1X < 460:
        player1X = 460
    if player1Y > 600:
        player1Y = 600
    if player1Y < 0:
        player1Y = 0

    #防止球拍2出界
    if player2X < 0:
        player2X = 0
    if player2X > 350:
        player2X = 350
    if player2Y > 600:
        player2Y = 600
    if player2Y < 0:
        player2Y = 0



    #球拍击球
    if ballX >= player1X + 10 and ballY > player1Y + 10 and ballY < player1Y + 100 and not f1:
        ballStep *= -1
        ballX = player1X
        f1 = True
        f2 = False
        # hit_sound.play()

    if ballX <= player2X + 100 and ballY > player2Y + 20 and ballY < player2Y + 100 and not f2:
        ballStep *= -1
        ballX = player2X + 102
        f2 = True
        f1 = False
        # hit_sound.play()

    #绘制更新
    screen.blit(player1Img, (player1X, player1Y))
    player1X += player1StepX
    player1Y += player1StepY
    screen.blit(player2Img, (player2X, player2Y))
    player2X += player2StepX
    player2Y += player2StepY

    screen.blit(ballImg, (ballX, ballY))
    ballX += ballStep
    ballY += ballStep

    #计算得分
    if (ballX >= 1000):
        time.sleep(0.2)  # 睡眠0.1s
        ballX = 500
        ballY = 300
        f()
        player1score += 1
        print(player1score)
        print("player1得分!")

    if (ballX <= 10):
        time.sleep(0.2)  # 睡眠0.1s
        ballX = 500
        ballY = 300
        f()
        player2score += 1
        print(player2score)
        print("player2得分!")

    clock.tick(144)
    check_is_over() #显示游戏结束字段
    pygame.display.update()


    print('f1 ', f1)
    print('f2 ', f2)



添加音乐和击打音效增加游戏品质

采用5局3胜制

  • 0
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值