本博客使用了Pygame库来创建游戏窗口和处理游戏逻辑。
一、代码详细解释:
首先,通过调用pygame.init()
来初始化Pygame库,然后创建一个窗口并设置标题:
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Ball")
这里的WIDTH
和HEIGHT
是窗口的宽度和高度。
接下来,通过调用init_item
函数初始化球、杆子和分数的图像及其位置:
ball, ball_rect = init_item('bar.gif', random.randint(50, WIDTH - 50), 0)
bar, bar_rect = init_item('ball.jpg', 200, 400)
txt_score, txt_score_rect = show_text("Score:0", 40, 20, 20)
init_item
函数用于加载图像文件并返回图像对象和矩形对象,矩形对象用于控制图像的位置。
然后设置小球的初始速度、播放背景音乐、创建计时器对象和记分变量:
ball_speed = [5, 5]
pygame.mixer.music.load('背景音乐.mp3')
pygame.mixer.music.play(loops=-1)
clock = pygame.time.Clock()
score = 0
ball_speed
表示小球在水平和垂直方向上的速度。
接下来是游戏的主循环,通过调用pygame.event.get()
来获取用户输入事件并进行处理。例如,通过检查按键事件来控制杆子的移动:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
if event.type == pygame.KEYDOWN and event.key == pygame.K_LEFT:
# 左移杆子
if event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT:
# 右移杆子
if event.type == pygame.MOUSEBUTTONDOWN:
# 处理鼠标点击事件
如果用户点击了窗口的关闭按钮,则调用exit()
函数退出游戏。
然后更新小球的位置,处理小球与边界的碰撞以及小球与杆子的碰撞:
ball_rect = ball_rect.move(ball_speed)
# 处理小球与边界的碰撞
if ball_rect.top < 0:
ball_speed[1] = -ball_speed[1]
if ball_rect.right > WIDTH or ball_rect.left < 0:
ball_speed[0] = -ball_speed[0]
# 处理小球与杆子的碰撞
if ball_rect.bottom > bar_rect.top and ball_rect.left > bar_rect.left and ball_rect.right < bar_rect.right:
# 碰到杆子,反弹并增加分数
ball_speed[1] = -ball_speed[1]
score += 1
如果小球碰到上下边界,将垂直速度取反使其反弹;如果小球碰到左右边界,将水平速度取反。如果小球与杆子发生碰撞,将垂直速度取反使其反弹,并增加分数。
然后根据游戏状态更新窗口的内容:
screen.fill((0, 0, 0)) # 清空窗口
if ball_rect.bottom > HEIGHT:
# 游戏结束,显示游戏结束的界面
else:
# 游戏未结束,显示小球、杆子和分数
如果小球超过窗口的底部,则游戏结束,显示游戏结束的界面。否则,显示小球、杆子和分数。
二、完整代码展示:
import pygame
import random
WIDTH = 640
HEIGHT = 480
def mainGame():
# 显示窗口
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Ball")
##球:
ball, ball_rect = init_item('bar.gif', random.randint(50, WIDTH - 50), 0)
##杆子
bar, bar_rect = init_item('ball.jpg', 200, 400)
##显示分数
txt_score, txt_score_rect = show_text("Score:0", 40, 20, 20)
ball_speed = [5, 5] ##小球的运行速度
pygame.mixer.music.load('背景音乐.mp3') # 导入音乐
pygame.mixer.music.play(loops=-1) # 循环播放
clock = pygame.time.Clock() ##用来设定窗口的刷新频率
##记分的变量
score = 0
while True:
clock.tick(60) ##每秒执行60次
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
if event.type == pygame.KEYDOWN and event.key == pygame.K_LEFT:
if bar_rect.left > 3:
bar_rect = bar_rect.move([-80, 0])
else:
bar_rect = bar_rect.move([0, 0])
if event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT:
if bar_rect.right < WIDTH - 3:
bar_rect = bar_rect.move([80, 0])
else:
bar_rect = bar_rect.move([0, 0])
if event.type == pygame.MOUSEBUTTONDOWN:
if 250 <= event.pos[0] < 350 \
and 280 < event.pos[1] < 320:
mainGame()
ball_rect = ball_rect.move(ball_speed) ##球运动
if ball_rect.top < 0: ##遇到上下边界反弹
ball_speed[1] = -ball_speed[1]
if ball_rect.right > WIDTH or ball_rect.left < 0: ##遇到左右边界反弹
ball_speed[0] = -ball_speed[0]
##球和杆子的位置关系
if ball_rect.bottom > bar_rect.top \
and ball_rect.left > bar_rect.left \
and ball_rect.right < bar_rect.right: ##碰到杆子
ball_speed[1] = -ball_speed[1]
score += 1
# 游戏增加难度
if score % 5 == 0:
ball_speed[1] *= 1.2
ball_speed[0] *= 1.2
txt_score, txt_score_rect = show_text("Score:" + str(score), 40, 20, 20)
screen.fill((0, 0, 0)) ##把球停留过的位置颜色涂成黑色
if ball_rect.bottom > HEIGHT: ##本次游戏结束的条件
ball_speed = [0, 0]
over, over_rect = init_item('game over.jpg', 0, 0)
txt_score_over, txt_score_rect_over = show_text("Score:" + str(score), screen.get_rect().centerx,
screen.get_rect().centery, 48)
txt_restart, txt_restart_rect = show_text("Restart", 300, 300, 32)
screen.blit(over, over_rect) ##显示gameover图片
screen.blit(txt_score_over, txt_score_rect_over) ##结束的最终分数
screen.blit(txt_restart, txt_restart_rect) ##将分数显示在窗口左上脚
else:
screen.blit(ball, ball_rect) ##把球显示在窗口中
screen.blit(bar, bar_rect) ##把杆子显示在窗口中
screen.blit(txt_score, txt_score_rect) ##将分数显示在窗口左上脚
pygame.display.flip()
def show_text(txt, pos_x, pos_y, fontsize=12):
##设定显示文本信息
font = pygame.font.Font(None, fontsize)
text = font.render(txt, True, (255, 0, 0))
text_rect = text.get_rect()
text_rect.centerx = pos_x
text_rect.centery = pos_y
return text, text_rect
def init_item(img_path, pos_x, pos_y):
##显示图片
item = pygame.image.load(img_path)
item_rect = pygame.Rect((pos_x, pos_y, 0, 0))
item_rect.size = item.get_size()
return item, item_rect
if __name__ == "__main__":
mainGame()