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


Messages In This Thread
How do I add more balls to basic Pong game? - by JUtah - Apr-18-2019, 03:15 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Exclamation pong paddles wont move skullkat232 5 2,573 Feb-13-2023, 03:53 PM
Last Post: Vadanane
  Problem with my pong code Than999 3 3,632 Oct-22-2021, 08:50 AM
Last Post: Qanima
  Game “Pong” I have problems with the code BenBach18 2 3,640 Jan-10-2021, 05:16 PM
Last Post: michael1789
  [PyGame] Creating Pong in Pygame Russ_CW 2 2,926 Oct-11-2020, 11:56 AM
Last Post: Russ_CW
  Trying to make a simple pong game. kevindadmun 1 4,046 Aug-05-2019, 06:39 AM
Last Post: kevindadmun
  Smiley Pong Help Jasmineleroy 6 4,935 May-22-2019, 11:36 AM
Last Post: metulburr
  [PyGame] Pong game key.event problem erickDarko 2 4,318 Dec-12-2018, 03:17 PM
Last Post: erickDarko
  [PyGame] Basic Snake game (using OOP) PyAlex 1 12,719 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