Follow-Up Assignment Series: Python Game Development
11 - Score Multiplier and Bonus Points
Introduction
In this assignment, we'll introduce a score multiplier feature. If a player hits the ball multiple times without missing, their score will increase faster. This concept is similar to a combo multiplier in many games, encouraging players to keep the ball in play to maximize their score.
Task
-
Multiplier Logic:
- Create a multiplier variable starting at 1.
- Every time the ball hits the paddle without the player missing, increase the multiplier by 0.5 (up to a maximum of 3x).
- When the player misses, reset the multiplier to 1.
-
Update Score Function:
- Modify the
update_score()
function to display the current multiplier alongside the score.
- Modify the
Example Code Snippet:
# Initialize the multiplier
multiplier = 1.0
# Update the score function to include the multiplier
def update_score():
pen.clear()
pen.write(f"Score: {score} Levens: {lives} Multiplier: {multiplier}x", align="center", font=("Courier", 24, "normal"))
# Update multiplier in the game loop
if hit_paddle:
multiplier = min(multiplier + 0.5, 3)
score += int(1 * multiplier)
else:
multiplier = 1
Deliverable
A screenshot showing the score, lives, and current multiplier on the screen.
12 - OOP with Turtle
Introduction
To enhance your Python skills, we'll refactor our Pong game using Object-Oriented Programming (OOP). This will help in managing complex codebases and introduce new concepts like classes, objects, methods, and encapsulation.
Task
-
Create a Paddle Class:
- Convert the existing paddle code into a
Paddle
class with attributes for position and size. - Include methods for moving up and down and initializing the paddle.
- Convert the existing paddle code into a
-
Create a Ball Class:
- Convert the ball code into a
Ball
class with attributes for position and velocity. - Include methods to move the ball and check for collisions.
- Convert the ball code into a
Example Code Snippet:
class Paddle:
def __init__(self, x, y):
self.paddle = turtle.Turtle()
self.paddle.shape("square")
self.paddle.color("white")
self.paddle.shapesize(stretch_wid=6, stretch_len=1)
self.paddle.penup()
self.paddle.goto(x, y)
def move_up(self):
y = self.paddle.ycor()
if y < 250:
y += 20
self.paddle.sety(y)
def move_down(self):
y = self.paddle.ycor()
if y > -240:
y -= 20
self.paddle.sety(y)
# Similar refactoring would be done for the Ball class
python