Python Forum
[PyGame] How do I add more balls to basic Pong game?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] How do I add more balls to basic Pong game?
#1
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()
 
Reply
#2
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.
Recommended Tutorials:
Reply
#3
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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Exclamation pong paddles wont move skullkat232 5 2,335 Feb-13-2023, 03:53 PM
Last Post: Vadanane
  Problem with my pong code Than999 3 3,451 Oct-22-2021, 08:50 AM
Last Post: Qanima
  Game “Pong” I have problems with the code BenBach18 2 3,489 Jan-10-2021, 05:16 PM
Last Post: michael1789
  [PyGame] Creating Pong in Pygame Russ_CW 2 2,805 Oct-11-2020, 11:56 AM
Last Post: Russ_CW
  Trying to make a simple pong game. kevindadmun 1 3,903 Aug-05-2019, 06:39 AM
Last Post: kevindadmun
  Smiley Pong Help Jasmineleroy 6 4,713 May-22-2019, 11:36 AM
Last Post: metulburr
  [PyGame] Pong game key.event problem erickDarko 2 4,168 Dec-12-2018, 03:17 PM
Last Post: erickDarko
  [PyGame] Basic Snake game (using OOP) PyAlex 1 12,527 Sep-10-2018, 09:02 PM
Last Post: Mekire

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020