@Annvil/

Paddle Ball

Pygame

Sample from class

fork
loading
Files
  • main.py
  • nohup.out
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# adding libraries we're going to use
import pygame
from random import randint

# initializing variables to account for the number of balls caught, and total dropped
score = 0
total = 0

# Making dictionaries with settings for everything.
display = {
    "width": 800,
    "height": 600
}


class paddle:
    width = 0
    height = 20
    x = 300
    y = 580
    velocity = 8

    def __init__(self, size=150):
        if size > 40:
            self.width = size

    def left(self):
      self.x -= self.velocity 

    def right(self):
      self.x += self.velocity


class ball:
    radius = 15
    y = 30
    x = 0
    velocity = float(3)

    def __init__(self, maxvel=0):
        self.x = randint(30, 770)
        if maxvel > 1:
            self.velocity = float(randint(2, int(maxvel)))

    def fall(self):
        self.y += int(self.velocity)


# initialize a set to store the balls that must be deleted
delete_balls = []

# initialize dictionary to store active balls
balls = []

# initliaize instance of the paddle
pad = paddle()

# creating a window, and launching our game
pygame.init()
myfont = pygame.font.SysFont('monospace', 50)
win = pygame.display.set_mode((display["width"], display["height"]))

while (total-score) < 10:
    pygame.time.delay(20)
    win.fill((255, 255, 255))

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

    while len(balls) < (score/6)+1:
        balls.append(ball((score/3)+3))

    keys = pygame.key.get_pressed()

    if keys[pygame.K_LEFT]:
        pad.left()

    if keys[pygame.K_RIGHT]:
        pad.right()

    pygame.draw.rect(win, (255, 0, 0), (pad.x, pad.y, pad.width, pad.height))

    for a_ball in balls:
        a_ball.fall()
        pygame.draw.circle(win, (0, 0, 255), (a_ball.x, a_ball.y), a_ball.radius)

        if (a_ball.y + a_ball.radius) >= pad.y:
            if a_ball.x > pad.x and a_ball.x < pad.x + pad.width:
                score += 1
            total += 1
            delete_balls.append(a_ball)

    for a_ball in delete_balls:
        balls.remove(a_ball)
    delete_balls.clear()

    textsurface = myfont.render("score: {0}/{1}".format(score, total), False, (0, 0, 0))
    win.blit(textsurface, (10, 10))

    pygame.display.update()