python弹球游戏彩蛋

在上一期,我们已经实现了基本弹球的功能,先总结一下上期的代码:

import pygame
pygame.init()

width=800
height=600

screen=pygame.display.set_mode([width,height])
pygame.display.set_caption("python弹球游戏")

keepGoing=True
pic=pygame.image.load("ball.png")
colorkey=pic.get_at((0,0))
pic.set_colorkey(colorkey)

picx = 0
picy = 0
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
timer = pygame.time.Clock()
speedx = 5
speedy = 5
paddlew = 200
paddleh = 25
paddlex = 300
paddley = 550
picw = 100
pich = 40

xend=width-50
yend=height-50#这两行的减50为判定提供时间
ticks=60#帧率

font=pygame.font.SysFont("None",24)
while keepGoing:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            keepGoing=False
    picx+=speedx
    picy+=speedy
    if picx<=0 or picx+pic.get_width()>=xend:
        speedx= -speedx
    if picy<=0:
        speedy= -speedy
    if picy>=yend:
        speedy= -speedy
    if picy + pich >= paddley and picy + pich <= paddley + paddleh and speedy > 0:
        if picx + picw / 2 >= paddlex and picx + picw / 2 <= paddlex + paddlew:
            speedy = -speedy
    screen.fill(BLACK)
    screen.blit(pic, (picx, picy))
    paddlex = pygame.mouse.get_pos()[0]
    paddlex -= paddlew / 2
    pygame.draw.rect(screen, WHITE, (paddlex, paddley, paddlew, paddleh))
    pygame.display.update()
    timer.tick(ticks)

pygame.quit()

至于功能...不用再细说了吧?

咳咳,言归正传,我们来列出今天的任务:

一,添加字幕,其中包括坐标,分数以及游戏结果

二,优化整体算法

三,添加开始界面及选项

我们先来制作第一项:字幕

字幕的添加

说到字幕,它已经属于pygame的标配了。这是因为pygame的字幕具有制作简单、字体多样及颜色多样等特点。它的模板很简单,如下:

首先是关于字体的设定:

font=pygame.font.SysFont("字体",大小)

其次是字体的显示:

draw_string="要显示的内容"
text=font.render(draw_string,True,颜色)
text_rect=text.get_rect()
text_rect.centerx=screen.get_rect().centerx
text_rect.y= y坐标
screen.blit(text,text_rect)

以上代码仅限函数中显示

我们因此可以输出弹球与球拍的坐标,代码如下:

import pygame

pygame.init()
width=800
height=600
screen=pygame.display.set_mode([width,height])
pygame.display.set_caption("python弹球游戏")
keepGoing=True
pic=pygame.image.load("ball.png")
colorkey=pic.get_at((0,0))
pic.set_colorkey(colorkey)
picx = 0
picy = 0
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
timer = pygame.time.Clock()
speedx = 5
speedy = 5
paddlew = 200
paddleh = 25
paddlex = 300
paddley = 550
picw = 100
pich = 40
xend=width-50
yend=height-50#这两行的减50为判定提供时间
ticks=60#帧率
font=pygame.font.SysFont("None",24)
while keepGoing:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            keepGoing=False
    picx+=speedx
    picy+=speedy
    if picx<=0 or picx+pic.get_width()>=xend:
        speedx= -speedx
    if picy<=0:
        speedy= -speedy
    if picy>=yend:
        speedy= -speedy
    screen.fill(BLACK)
    screen.blit(pic, (picx, picy))
    paddlex = pygame.mouse.get_pos()[0]
    paddlex -= paddlew / 2
    pygame.draw.rect(screen, WHITE, (paddlex, paddley, paddlew, paddleh))
    if picy + pich >= paddley and picy + pich <= paddley + paddleh and speedy > 0:
        if picx + picw / 2 >= paddlex and picx + picw / 2 <= paddlex + paddlew:
            speedy = -speedy
    draw_string = "X Speed:" + str(speedx) + '   ' + "Y Speed:" + str(speedy)
    text = font.render(draw_string, True, WHITE)
    text_rect = text.get_rect()
    text_rect.centerx = screen.get_rect().centerx
    text_rect.y = 30
    screen.blit(text, text_rect)
    pygame.display.update()
    timer.tick(ticks)
pygame.quit()

一开始作者不小心删掉了画拍子的程序,结果拍子怎么都显示不出来,最后才看到

显示分数

分数非常简单,前提是你已经理解了上面的代码。代码如下:

import pygame

pygame.init()
width=800
height=600
screen=pygame.display.set_mode([width,height])
pygame.display.set_caption("python弹球游戏")
keepGoing=True
pic=pygame.image.load("ball.png")
colorkey=pic.get_at((0,0))
pic.set_colorkey(colorkey)
picx = 0
picy = 0
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
timer = pygame.time.Clock()
speedx = 5
speedy = 5
paddlew = 200
paddleh = 25
paddlex = 300
paddley = 550
picw = 100
pich = 40
xend=width-50

猜你喜欢

转载自blog.csdn.net/CSP_J/article/details/129030422