Python Forum
[PyGame] How do I add more balls to basic Pong game? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Game Development (https://python-forum.io/forum-11.html)
+--- Thread: [PyGame] How do I add more balls to basic Pong game? (/thread-17634.html)



How do I add more balls to basic Pong game? - JUtah - Apr-18-2019

I am currently struggling through a class and have an EXTREMELY LIMITED understanding of what I am doing thus for so any detailed help (layman terms please) will be greatly appreciated Big Grin I need to implement the following and have been unsuccessful thus far: "after each 5 balls that you catch, you should increase the number of balls that are falling at a time. Each new ball that gets created should have a velocity set to a random integer (randint) between the initial velocity (20 in the original tutorial) and the current max velocity. Hint: You can create an array of dictionaries and iterate over that list to update/change all objects in a dictionary."

 
# Initializing pygame + some important variables 

import pygame
from random import randint

pygame.init()

score = 0
total = 0

myfont = pygame.font.SysFont('monospace', 50)

# Making dictionaries with settings for everything.

display = {
    "width": 800,
    "height": 600
}

#Increased Paddle Velocity
paddle = {
    "width": 200,
    "height": 20,
    "x": 300,
    "y": 580,
    "velocity": 40
}

ball = {
    "radius": 15,
    "y": 30,
    "x": randint(0, display["width"]),
    "velocity": 20
}

# Launching the window, setting it to the dimensions of the `display` dictionary.

win = pygame.display.set_mode((display["width"], display["height"]))

# The Main game loop

while True:
    pygame.time.delay(100)
    win.fill((255, 255, 255))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            break

    keys = pygame.key.get_pressed()

    if keys[pygame.K_LEFT]:
        paddle["x"] -= paddle["velocity"]

    if keys[pygame.K_RIGHT]:
        paddle["x"] += paddle["velocity"]

    if ball["y"] + ball["radius"] >= paddle["y"]:
        if ball["x"] > paddle["x"] and ball["x"] < paddle["x"] + paddle["width"]:
#Increase ball speed
          ball['velocity'] += 3
          score += 1
        total += 1
        ball["y"] = ball["radius"]
        ball["x"] = randint(0, display["width"])


    ball["y"] += ball["velocity"]

    pygame.draw.circle(win, (0, 0, 255), (ball["x"], ball["y"]), ball["radius"])
    pygame.draw.rect(win, (255, 0, 0), (paddle["x"], paddle["y"], paddle["width"], paddle["height"]))

#LOSE GAME
    if total - score >= 10:
      break
    
    textsurface = myfont.render("score: {0}/{1}".format(score, total), False, (0, 0, 0))
    win.blit(textsurface, (10, 10))
    
    pygame.display.update()

pygame.quit()
 



RE: How do I add more balls to basic Pong game? - metulburr - Apr-18-2019

Is this homework? Are you restricted in what you can use?

I would use a class to handle the ball. Then created numerous objects from that class based on your factors. Here is an example. And here is an example of using that class to create a ball and add it to the list of balls.


RE: How do I add more balls to basic Pong game? - JUtah - Apr-18-2019

Our instructor is directing us to iterate over a dictionary, but I am trying to work through and understand your code and where to make it fit. Thanks so much for your help!