python pinball game easter egg

In the last issue, we have implemented the basic pinball function. Let’s summarize the code from the previous issue:

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()

As for the function... no need to go into details, right?

Ahem, let’s get back to business, let’s list today’s tasks:

1. Add subtitles, including coordinates, scores and game results

2. Optimize the overall algorithm

3. Add the start interface and options

Let’s make the first item first: subtitles

Adding subtitles

Speaking of subtitles, they are already standard in pygame. This is because pygame's subtitles have the characteristics of simple production, various fonts and various colors. Its template is very simple, as follows:

The first is about the font settings:

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

Next is the display of fonts:

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)

The above code can only be displayed in functions

We can therefore output the coordinates of the pinball and racket. The code is as follows:

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()

At first, the author accidentally deleted the program for drawing beats. As a result, the beats could not be displayed at all. Finally, I saw

Show score

Fractions are very simple, assuming you understand the code above. code show as below:

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

Guess you like

Origin blog.csdn.net/CSP_J/article/details/129030422