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
import pygame
from random import randint

pygame.init()

score = 0
total = 0

# myfont = pygame.font.SysFont('comicsansms', 30)


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

paddle = {
  "width": 200,
  "height": 20,
  "x": 300,
  "y": 580,
  "velocity": 5
}

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

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

run = True

while run:
    pygame.time.delay(100)
    win.fill((0, 0, 0))

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

    keys = pygame.key.get_pressed()

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

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

    if keys[pygame.K_UP]:
        paddle["y"] -= paddle["velocity"]

    if keys[pygame.K_DOWN]:
        paddle["y"] += paddle["velocity"]

    if ball["y"] + ball["radius"] >= paddle["y"]:
        if ball["x"] > paddle["x"] and ball[
                "x"] < paddle["x"] + paddle["width"]:
            score += 1

        total += 1
        ball["y"] = ball["radius"]
        ball["x"] = randint(0, display["width"])
        print(score, total)

    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"]))
    # textsurface = myfont.render("{0}/{1}".format(score, total), False, (255, 255, 255))
    # win.blit(textsurface, (0, 0))
    pygame.display.update()

pygame.quit()