Files
ping_pong/main.py
2026-06-17 14:46:36 +08:00

139 lines
3.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import pgzrun
<<<<<<< HEAD
import random
#随机数生成
random_intX = random.randint(235, 1685)
random_intY = random.randint(235, 845)
=======
>>>>>>> 2d13872febf768c59aec106182ed404dbbf57614
music.play('bgm')
# 窗口参数
WIDTH = 1920
HEIGHT = 1080
TITLE = "Ping_Pang"
# 左右球拍
paddle1 = Actor("1")
paddle1.x = 80
paddle1.y = HEIGHT / 2
paddle_speed = 8
paddle2 = Actor("2")
paddle2.x = WIDTH - 80
paddle2.y = HEIGHT / 2
<<<<<<< HEAD
#box生成
tbox = Actor("t_box")
tbox.x = random_intX
tbox.y = HEIGHT / 2
=======
>>>>>>> 2d13872febf768c59aec106182ed404dbbf57614
# 乒乓球
ball = Actor("ball")
ball.pos = (WIDTH//2, HEIGHT//2)
ball_speed_x = 7
ball_speed_y = 7
# 游戏状态: playing / gameover
game_state = "playing"
lose_text = ""
# 球拍上下限位
half_h = paddle1.height / 2
top = half_h
bottom = HEIGHT - half_h
bg = Actor("bg")
def draw():
screen.clear()
bg.draw()
paddle1.draw()
paddle2.draw()
ball.draw()
<<<<<<< HEAD
tbox.draw()
=======
>>>>>>> 2d13872febf768c59aec106182ed404dbbf57614
# 游戏结束提示
if game_state == "gameover":
screen.draw.text(lose_text, center=(WIDTH//2, HEIGHT//2), fontsize=80, color="red")
screen.draw.text("click space to restar", center=(WIDTH//2, HEIGHT//2+80), fontsize=40, color="white")
def update():
global ball_speed_x, ball_speed_y, game_state, lose_text
if game_state != "playing":
return
# 左拍 W S
if keyboard.w and paddle1.y>top:
paddle1.y -= paddle_speed
if keyboard.s and paddle1.y<bottom:
paddle1.y += paddle_speed
# 右拍 ↑ ↓
if keyboard.up and paddle2.y>top:
paddle2.y -= paddle_speed
if keyboard.down and paddle2.y<bottom:
paddle2.y += paddle_speed
# 小球移动
ball.x += ball_speed_x
ball.y += ball_speed_y
# 上下墙壁反弹
if ball.top <= 0 or ball.bottom >= HEIGHT:
ball_speed_y *= -1
sounds.bump.play()
# 球拍碰撞反弹
if ball.colliderect(paddle1) or ball.colliderect(paddle2):
ball_speed_x *= -1
sounds.bump.play()
<<<<<<< HEAD
#方块实现
if ball.colliderect(tbox):
sounds.bump.play()
# 判断撞方块左侧/右侧翻转X速度
if ball.centerx < tbox.x:
ball_speed_x = -abs(ball_speed_x) - 1
else:
ball_speed_x = abs(ball_speed_x) + 1
# 判断撞方块上侧/下侧翻转Y速度
if ball.centery < tbox.y:
ball_speed_y = -abs(ball_speed_y)
else:
ball_speed_y = abs(ball_speed_y)
# 重新生成方块坐标(现在能修改全局变量)
random_intX = random.randint(235, 1685)
random_intY = random.randint(235, 845)
tbox.x = random_intX
tbox.y = random_intY
=======
>>>>>>> 2d13872febf768c59aec106182ed404dbbf57614
# 出左右边界直接失败
if ball.left <= 0:
game_state = "gameover"
lose_text = "player 1 win!"
sounds.zhanbai.play()
if ball.right >= WIDTH:
game_state = "gameover"
lose_text = "player 2 win!"
sounds.zhanbai.play()
# 空格重新开局
def on_key_down(key):
global game_state, ball_speed_x, ball_speed_y
if key == keys.SPACE and game_state == "gameover":
game_state = "playing"
ball.pos = (WIDTH//2, HEIGHT//2)
ball_speed_x = 7
ball_speed_y = 7
pgzrun.go()