Reputation: 21
I am writing a Pong clone and I want to make the game get harder as the game progress. For every point (or few points), increase the speed of the ball.
My code is as follows:
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()
I am pretty new to programming and I don't know much of how it works. For the code that is already there, I had a friend who is more experienced to help me.
Upvotes: 2
Views: 550
Reputation: 8556
I suggest you to add one method that updates the ball speed depending on the score, and you can call it just after the collision detection (because there is where you increase the score). You can include a call to that method like this, inside your play
method:
# (...) all your current code
self.collision_handler()
self.speed_up()
self.draw()
And, in the method implementation, you can divide your score, for example by 10, and add it as extra speed. Tweak this value to more or less, so it will fit your game better.
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
Upvotes: 1