在 Pygame 中创建了一个类似乒乓球的游戏,但是存在以下几个问题:
- 问题 1: 挡板可以超出游戏区域的边界。
- 问题 2: 小球可以穿过挡板和墙壁。
- 问题 3: 小球的速度会不断加快。
解决方案
问题 1:
为了解决第一个问题,需要在 position_x 和 position_y 变量中添加边界检查。在每次更新挡板的位置之前,我们需要检查挡板的坐标是否超过了游戏区域的边界。如果超过了边界,我们需要将挡板的位置重置到边界内。
问题 2:
为了解决第二个问题,需要在 player 和 ball 变量之间添加碰撞检测。在每次更新小球的位置之前,我们需要检查小球是否与挡板或墙壁碰撞。如果小球与挡板或墙壁碰撞,我们需要改变小球的速度方向。
问题 3:
为了解决第三个问题,需要在 velocity1 和 velocity2 变量中添加一个速度上限。在每次更新小球的速度之前,我们需要检查小球的速度是否超过了速度上限。如果小球的速度超过了速度上限,我们需要将小球的速度重置到速度上限以内。
代码示例:
import pygame
import time
import sys
import random
from pygame.locals import *
pygame.init()
# 设置屏幕大小
screen = pygame.display.set_mode((600, 500))
# 设置窗口标题
pygame.display.set_caption("Pong Squash")
# 定义游戏变量
game_over = False
lives = 3
points = 0
lives_remaining = 3
game_over1 = False
lives1 = False
points1 = False
lives_remaining1 = False
font1 = pygame.font.Font((munrosmall), 40)
font2 = pygame.font.Font(None, 40)
# 设置颜色
white = (255, 255, 255)
black = (0, 0, 0)
green = (0, 250, 0)
yellow = (255, 255, 0)
# 加载游戏资源
game_over_display = pygame.image.load("game_over1.png").convert()
lives = pygame.image.load("lives.png").convert()
points = pygame.image.load("points.png").convert()
lives_remaining = pygame.image.load("lives_remaining.png").convert()
your_score = pygame.image.load("score_intro.png").convert()
space = pygame.image.load("space.png").convert()
esc = pygame.image.load("esc.png").convert()
munrosmall = "munrosmall.ttf"
beep1 = "beep1.wav"
beep2 = "beep2.wav"
beep3 = "beep3.wav"
beep4 = "beep4.wav"
# 设置挡板和球的初始位置和速度
position_x = 175
position_y = 375
velocity_x1 = 0
velocity_y1 = 0
position1 = 275
position2 = 150
velocity1 = 2
velocity2 = 2
# 游戏主循环
while not game_over:
# 处理事件
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
if event.key == pygame.K_SPACE:
if waiting:
waiting = False
reset_ball()
if event.key == pygame.K_LEFT:
velocity_x1 = (velocity_x1 - 3)
elif event.key == pygame. K_RIGHT:
velocity_x1 = (velocity_x1 + 3)
elif event.type == KEYUP:
if event.key == K_LEFT or event.key == K_RIGHT:
velocity_x1 = 0
# 清除屏幕
screen.fill(black)
# 绘制挡板和球
color = 255
width = 0
position = position_x, position_y, 250, 25
position_x += velocity_x1
position_y += velocity_y1
position1 += velocity1
position2 += velocity2
player = pygame.draw.rect(screen, (color, color, color), position, width)
ball = pygame.draw.rect(screen, (color, color, color), (position1, position2, 15, 15), width)
# 检查挡板和球之间的碰撞
if player.colliderect(ball):
velocity2 = -velocity2
beep_list = [beep1, beep2, beep3, beep4]
beep = random.shuffle(beep_list)
pygame.mixer.music.load((beep_list[1]))
pygame.mixer.music.play()
# 检查小球是否超过了游戏区域的边界
if position_x > 350 or position_x < 0:
velocity_x1 = 0
elif position1 > 575 or position1 < 0:
velocity1 = -velocity1
beep = random.shuffle(beep_list)
pygame.mixer.music.load((beep_list[1]))
pygame.mixer.music.play()
elif position2 < 0:
velocity2 = -velocity2
velocity2 += 0.1
points += 100
beep = random.shuffle(beep_list)
pygame.mixer.music.load((beep_list[1]))
pygame.mixer.music.play()
pygame.display.update()
elif position2 > 365:
new_points = points
newposition_x = position_x
change_level1()
# 绘制游戏信息
screen.blit(lives1, (450, 455))
screen.blit(points1, (0, 459))
screen.blit(lives_count, (560, 453))
points_count = font1.render(str(points), True, white)
screen.blit(points_count, (150, 456))
# 更新屏幕
pygame.display.update()
# 如果游戏结束,则显示游戏结束画面
if game_over:
screen.fill(black)
screen.blit(game_over_display, (200, 100))
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
elif event.key == K_RETURN:
gameplay1()
pygame.display.flip()
time.sleep(1)
screen.blit(your_score1, (100, 175))
pygame.display.flip()
time.sleep(1)
points_count = font1.render(str(new_points), True, white)
screen.blit(points_count, (400, 170))
pygame.display.flip()
time.sleep(1)
screen.blit(space1, (115, 250))
screen.blit(esc1, (175, 300))
pygame.display.flip()
# 开始游戏
gameplay1()