Pygame

Another game I made in middle school

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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import pygame
import random
import time

WIDTH = 360
HEIGHT = 480
FPS = 30

# define colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

# initialize pygame and create window
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Pong")
clock = pygame.time.Clock()
background = pygame.Surface((WIDTH, HEIGHT))
background.fill(BLACK)
background_rect = background.get_rect()
font_name = pygame.font.match_font("arial")


def draw_text(surf, text, size, x, y):
    font = pygame.font.Font(font_name, size)
    text_surface = font.render(text, True, WHITE)
    text_rect = text_surface.get_rect()
    text_rect.midtop = (x, y)
    surf.blit(text_surface, text_rect)

def show_start():
    screen.blit(background, background_rect)
    draw_text(screen, "PONG!", 64, WIDTH / 2, HEIGHT / 4)
    draw_text(screen, "P1: Arrow keys to move, P2: WASD to move", 22, WIDTH / 2, HEIGHT / 2)
    draw_text(screen, "Press a key to begin", 18, WIDTH / 2, HEIGHT * 3 / 4)
    pygame.display.flip()
    wait_for_key()

def wait_for_key():
    waiting = True
    while waiting:
        clock.tick(FPS)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
            if event.type == pygame.KEYUP:
                waiting = False

class Ball(pygame.sprite.Sprite):
    def __init__(self, side):
        pygame.sprite.Sprite.__init__(self)
        self.size = 10
        self.side = side
        self.image = pygame.Surface((self.size, self.size))
        self.image.fill(WHITE)
        self.rect = self.image.get_rect()
        self.rect.x = WIDTH / 2
        self.rect.y = HEIGHT / 2
        self.movespeedy = 0
        if self.side == 'right':
            self.movespeedx = 5
        else:
            self.movespeedx = -5

    def update(self):
        self.rect.x += self.movespeedx
        self.rect.y += self.movespeedy


class Player(pygame.sprite.Sprite):
    def __init__(self, pos, type1):
        pygame.sprite.Sprite.__init__(self)
        self.size = 10
        self.loc = []
        self.image = pygame.Surface((self.size, HEIGHT/4))
        self.image.fill(WHITE)
        self.rect = self.image.get_rect()
        self.rect.x = pos[0]
        self.rect.y = pos[1]
        self.type = type1

    def update(self):
        if self.type == 'Keys':
            self.key = pygame.key.get_pressed()

            if self.key[pygame.K_UP]:
                self.loc.append((self.rect.y, pygame.time.get_ticks()))
                self.rect.y -= 10

            elif self.key[pygame.K_DOWN]:
                self.loc.append((self.rect.y, pygame.time.get_ticks()))
                self.rect.y += 10
        else:
            self.key = pygame.key.get_pressed()

            if self.key[pygame.K_w]:
                self.loc.append((self.rect.y, pygame.time.get_ticks()))
                self.rect.y -= 10

            elif self.key[pygame.K_s]:
                self.loc.append((self.rect.y, pygame.time.get_ticks()))
                self.rect.y += 10

        if self.rect.bottom >= HEIGHT:
            self.rect.bottom = HEIGHT

        elif self.rect.y <= 0:
            self.rect.y = 0

side = 'right'
all_sprites = pygame.sprite.Group()
Paddles = pygame.sprite.Group()
Balls = pygame.sprite.Group()
pong = Ball(side)
Balls.add(pong)
all_sprites.add(pong)
player1 = Player((0, HEIGHT/2), 'no')
player2 = Player((WIDTH-10, HEIGHT/2), 'Keys')
Paddles.add(player1, player2)
all_sprites.add(player1, player2)

# Game loop
running = True
show_start()
RIGHT = 0
LEFT = 0
while running:
    if RIGHT == 5 or LEFT == 5:
        running = False
    # keep loop running at the right speed
    clock.tick(FPS)
    # Process input (events)
    for event in pygame.event.get():
        # check for closing window
        if event.type == pygame.QUIT:
            running = False

    # Update
    all_sprites.update()
    # Win!

    # If ball goes out of screen
    if pong.rect.x > WIDTH:
        draw_text(screen, 'Goal!!!', 18, WIDTH * 3 / 4, HEIGHT / 4)
        draw_text(screen, "Press a key to continue", 18, WIDTH / 2, HEIGHT * 3 / 4)
        side = 'right'
        RIGHT += 1
        pygame.display.flip()
        wait_for_key()
        pong.kill()
        pong = Ball(side)
        Balls.add(pong)
        all_sprites.add(pong)
    elif pong.rect.x < -10:
        draw_text(screen, 'Goal!!!', 18, WIDTH / 4, HEIGHT / 4)
        draw_text(screen, "Press a key to continue", 18, WIDTH / 2, HEIGHT * 3 / 4)
        side = 'left'
        LEFT += 1
        pygame.display.flip()
        wait_for_key()
        pong.kill()
        pong = Ball(side)
        Balls.add(pong)
        all_sprites.add(pong)
    # Ball bounce back Logic
    hit = pygame.sprite.spritecollide(pong, Paddles, False)
    if hit:
        pong.movespeedx = -pong.movespeedx
        pong.movespeedy = (pong.rect.centery - hit[0].rect.centery) / 5
    if pong.rect.y >= HEIGHT - pong.size or pong.rect.y <= 0:
        pong.movespeedy = -pong.movespeedy

    # Draw / render
    screen.fill(BLACK)
    draw_text(screen, str(RIGHT) + '           ' + str(LEFT), 30, WIDTH / 2, HEIGHT / 4)
    all_sprites.draw(screen)
    # *after* drawing everything, flip the display
    pygame.display.flip()

screen.fill(BLACK)
draw_text(screen, "WIN!", 64, WIDTH / 2, HEIGHT / 4)
pygame.display.flip()
wait_for_key()
pygame.quit()