111
This commit is contained in:
52
main.py
52
main.py
@@ -1,10 +1,18 @@
|
|||||||
import pgzrun
|
import pgzrun
|
||||||
import random
|
import random
|
||||||
|
|
||||||
|
#分数
|
||||||
|
scorep1 = 0
|
||||||
|
scorep2 = 0
|
||||||
|
# 防重复加分标记
|
||||||
|
hit_paddle1 = False
|
||||||
|
hit_paddle2 = False
|
||||||
|
|
||||||
#随机数生成
|
#随机数生成
|
||||||
random_intX = random.randint(235, 1685)
|
random_intX = random.randint(235, 1685)
|
||||||
random_intY = random.randint(235, 845)
|
random_intY = random.randint(235, 845)
|
||||||
|
|
||||||
|
|
||||||
music.play('bgm')
|
music.play('bgm')
|
||||||
# 窗口参数
|
# 窗口参数
|
||||||
WIDTH = 1920
|
WIDTH = 1920
|
||||||
@@ -45,14 +53,18 @@ bottom = HEIGHT - half_h
|
|||||||
bg = Actor("bg")
|
bg = Actor("bg")
|
||||||
|
|
||||||
def draw():
|
def draw():
|
||||||
|
global scorep1, scorep2
|
||||||
screen.clear()
|
screen.clear()
|
||||||
bg.draw()
|
bg.draw()
|
||||||
paddle1.draw()
|
paddle1.draw()
|
||||||
paddle2.draw()
|
paddle2.draw()
|
||||||
ball.draw()
|
ball.draw()
|
||||||
|
|
||||||
tbox.draw()
|
tbox.draw()
|
||||||
|
|
||||||
|
#分数
|
||||||
|
screen.draw.text('Score_p1: ' + str(scorep1), (15,10), color=(255,255,255), fontsize=30)
|
||||||
|
screen.draw.text('Score_p2: ' + str(scorep2), (1750,10), color=(255,255,255), fontsize=30)
|
||||||
|
|
||||||
# 游戏结束提示
|
# 游戏结束提示
|
||||||
if game_state == "gameover":
|
if game_state == "gameover":
|
||||||
screen.draw.text(lose_text, center=(WIDTH//2, HEIGHT//2), fontsize=80, color="red")
|
screen.draw.text(lose_text, center=(WIDTH//2, HEIGHT//2), fontsize=80, color="red")
|
||||||
@@ -60,6 +72,13 @@ def draw():
|
|||||||
|
|
||||||
def update():
|
def update():
|
||||||
global ball_speed_x, ball_speed_y, game_state, lose_text
|
global ball_speed_x, ball_speed_y, game_state, lose_text
|
||||||
|
global scorep1, scorep2, hit_paddle1, hit_paddle2
|
||||||
|
global random_intX, random_intY
|
||||||
|
|
||||||
|
# 每一帧重置碰撞标记,保证一次碰撞只加一次分
|
||||||
|
hit_paddle1 = False
|
||||||
|
hit_paddle2 = False
|
||||||
|
|
||||||
if game_state != "playing":
|
if game_state != "playing":
|
||||||
return
|
return
|
||||||
|
|
||||||
@@ -83,13 +102,23 @@ def update():
|
|||||||
ball_speed_y *= -1
|
ball_speed_y *= -1
|
||||||
sounds.bump.play()
|
sounds.bump.play()
|
||||||
|
|
||||||
# 球拍碰撞反弹
|
# 左球拍接住加分
|
||||||
if ball.colliderect(paddle1) or ball.colliderect(paddle2):
|
if ball.colliderect(paddle1) and not hit_paddle1:
|
||||||
ball_speed_x *= -1
|
ball_speed_x *= -1
|
||||||
sounds.bump.play()
|
sounds.bump.play()
|
||||||
|
scorep1 += 1
|
||||||
|
hit_paddle1 = True
|
||||||
|
|
||||||
|
# 右球拍接住加分
|
||||||
|
if ball.colliderect(paddle2) and not hit_paddle2:
|
||||||
|
ball_speed_x *= -1
|
||||||
|
sounds.bump.play()
|
||||||
|
scorep2 += 1
|
||||||
|
hit_paddle2 = True
|
||||||
|
|
||||||
#方块实现
|
#方块实现
|
||||||
if ball.colliderect(tbox):
|
if ball.colliderect(tbox):
|
||||||
sounds.bump.play()
|
sounds.box_co.play()
|
||||||
# 判断撞方块左侧/右侧:翻转X速度
|
# 判断撞方块左侧/右侧:翻转X速度
|
||||||
if ball.centerx < tbox.x:
|
if ball.centerx < tbox.x:
|
||||||
ball_speed_x = -abs(ball_speed_x) - 1
|
ball_speed_x = -abs(ball_speed_x) - 1
|
||||||
@@ -100,30 +129,33 @@ def update():
|
|||||||
ball_speed_y = -abs(ball_speed_y)
|
ball_speed_y = -abs(ball_speed_y)
|
||||||
else:
|
else:
|
||||||
ball_speed_y = abs(ball_speed_y)
|
ball_speed_y = abs(ball_speed_y)
|
||||||
# 重新生成方块坐标(现在能修改全局变量)
|
# 重新生成方块坐标
|
||||||
random_intX = random.randint(235, 1685)
|
random_intX = random.randint(235, 1685)
|
||||||
random_intY = random.randint(235, 845)
|
random_intY = random.randint(235, 845)
|
||||||
tbox.x = random_intX
|
tbox.x = random_intX
|
||||||
tbox.y = random_intY
|
tbox.y = random_intY
|
||||||
|
|
||||||
|
# 出左右边界游戏结束,这里不再加分
|
||||||
# 出左右边界直接失败
|
|
||||||
if ball.left <= 0:
|
if ball.left <= 0:
|
||||||
game_state = "gameover"
|
game_state = "gameover"
|
||||||
lose_text = "player 1 win!"
|
lose_text = "player 2 win!"
|
||||||
sounds.zhanbai.play()
|
sounds.zhanbai.play()
|
||||||
if ball.right >= WIDTH:
|
if ball.right >= WIDTH:
|
||||||
game_state = "gameover"
|
game_state = "gameover"
|
||||||
lose_text = "player 2 win!"
|
lose_text = "player 1 win!"
|
||||||
sounds.zhanbai.play()
|
sounds.zhanbai.play()
|
||||||
|
|
||||||
# 空格重新开局
|
# 空格重新开局
|
||||||
def on_key_down(key):
|
def on_key_down(key):
|
||||||
global game_state, ball_speed_x, ball_speed_y
|
global game_state, ball_speed_x, ball_speed_y, scorep1, scorep2
|
||||||
if key == keys.SPACE and game_state == "gameover":
|
if key == keys.SPACE and game_state == "gameover":
|
||||||
game_state = "playing"
|
game_state = "playing"
|
||||||
ball.pos = (WIDTH//2, HEIGHT//2)
|
ball.pos = (WIDTH//2, HEIGHT//2)
|
||||||
ball_speed_x = 7
|
ball_speed_x = 7
|
||||||
ball_speed_y = 7
|
ball_speed_y = 7
|
||||||
|
# 重启清空分数,不需要就注释掉下面两行
|
||||||
|
scorep1 = 0
|
||||||
|
scorep2 = 0
|
||||||
|
|
||||||
pgzrun.go()
|
pgzrun.go()
|
||||||
|
|
||||||
|
|||||||
BIN
sounds/box_co.wav
Normal file
BIN
sounds/box_co.wav
Normal file
Binary file not shown.
Reference in New Issue
Block a user