随着Pong游戏的进行增加球的速度



我正在编写一款《Pong》克隆游戏,我想让游戏随着游戏的进展变得越来越难。每得一分(或几分),增加球的速度。

我的代码如下:
import pygame
import sys
import math
class Ball(object):
    def __init__(self, x, y, width, height, vx, vy, colour):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.vx = vx
        self.vy = vy
        self.colour = colour

    def render(self, screen):
        pygame.draw.ellipse(screen, self.colour, self.rect)
    def update(self):
        self.x += self.vx
        self.y += self.vy
    @property
    def rect(self):
        return pygame.Rect(self.x, self.y, self.width, self.height)

class Paddle(object):
    def __init__(self, x, y, width, height, speed, colour):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.vx = 0
        self.speed = speed
        self.colour = colour
    def render(self, screen):
        pygame.draw.rect(screen, self.colour, self.rect)
    def update(self):
        self.x += self.vx
    def key_handler(self, event):
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                self.vx = -self.speed
            elif event.key == pygame.K_RIGHT:
                self.vx = self.speed
        elif event.key in (pygame.K_LEFT, pygame.K_RIGHT):
                self.vx = 0
    @property
    def rect(self):
        return pygame.Rect(self.x, self.y, self.width, self.height)   
        
class Pong(object):
    COLOURS = {"BLACK": (  0,   0,   0),
               "WHITE": (255, 255, 255),
               "RED"  : (255,   0,   0)}
    def __init__(self):
        pygame.init()
        (WIDTH, HEIGHT) = (640, 480)
        self.screen = pygame.display.set_mode((WIDTH, HEIGHT))
        pygame.display.set_caption("smach ball hit")
        self.ball = Ball(5, 5, 50, 50, 5, 5, Pong.COLOURS["BLACK"])
        self.paddle = Paddle(WIDTH / 2, HEIGHT - 50, 100,
                             10, 3, Pong.COLOURS["BLACK"])
        self.score = 0
 
    def play(self):
        clock = pygame.time.Clock()
        while True:
            clock.tick(50)
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()
                if event.type in (pygame.KEYDOWN, pygame.KEYUP):
                    self.paddle.key_handler(event)
            self.collision_handler()
            self.draw()
    def collision_handler(self):
        if self.ball.rect.colliderect(self.paddle.rect):
            self.ball.vy = -self.ball.vy
            self.score += 1
           
  
        if self.ball.x + self.ball.width >= self.screen.get_width():
            self.ball.vx = -(math.fabs(self.ball.vx))
        elif self.ball.x <= 0:
            self.ball.vx = math.fabs(self.ball.vx)
        if self.ball.y + self.ball.height >= self.screen.get_height():
            pygame.quit()
            sys.exit()
        elif self.ball.y <= 0:
            self.ball.vy = math.fabs(self.ball.vy)
        if self.paddle.x + self.paddle.width >= self.screen.get_width():
            self.paddle.x = self.screen.get_width() - self.paddle.width
        elif self.paddle.x <= 0:
            self.paddle.x = 0
    def draw(self):
        self.screen.fill(Pong.COLOURS["WHITE"])
        font = pygame.font.Font(None, 48)
        score_text = font.render("Score: " + str(self.score), True,
                                 Pong.COLOURS["RED"])
        self.screen.blit(score_text, (0, 0))
        self.ball.update()
        self.ball.render(self.screen)
        self.paddle.update()
        self.paddle.render(self.screen)
        pygame.display.update()
if __name__ == "__main__":
    Pong().play()
我对编程很陌生,我不知道它是如何工作的。对于已经存在的代码,我有一个更有经验的朋友来帮助我。

我建议您添加一个方法来根据分数更新球的速度,并且您可以在碰撞检测之后调用它(因为这是您增加分数的地方)。您可以像这样在play方法中包含对该方法的调用:

        # (...) all your current code
        self.collision_handler()
        self.speed_up()
        self.draw()

并且,在方法实现中,您可以将您的分数除以10,并将其作为额外的速度。调整这个值,这样它将更适合你的游戏。

def speed_up(self):
    delta = self.score // 10
    if self.ball.vx > 0:
        self.ball.vx += delta
    else:
        self.ball.vx -= delta
    if self.ball.vy > 0:
        self.ball.vy += delta
    else:
        self.ball.vy -= delta

最新更新