forked from DiegoAgher/learnPongPython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpong2players.py
81 lines (67 loc) · 2.08 KB
/
pong2players.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
import pygame
import numpy as np
from pygame.locals import *
from game_objects.TrainingPlayer import TrainPlayer
from game_objects.ball import Ball
from game_objects.block import Block
SCR_WID, SCR_HEI = 400, 400
screen = pygame.display.set_mode((SCR_WID, SCR_HEI))
pygame.display.set_caption("Pong")
pygame.font.init()
clock = pygame.time.Clock()
FPS = 30
pygame.init()
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GOLD = (201, 168, 168)
Call = pygame.sprite.Group(())
got = pygame.sprite.Group(())
backg = pygame.Surface((SCR_WID, SCR_HEI))
def main():
backgscale = pygame.transform.scale(backg, (SCR_WID, SCR_HEI))
global player
player = TrainPlayer("player1", screen, SCR_HEI, SCR_WID)
global enemy
enemy = TrainPlayer("player2", screen, SCR_HEI, SCR_WID)
block = Block()
got.add(block)
ball = Ball(screen, SCR_HEI, SCR_WID, player, enemy)
Call.add(ball)
screen_data = []
i = 0
SCREEN_REDUCE = 16
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
print("Game exited by user")
exit()
keys = pygame.key.get_pressed()
# if keys[pygame.K_w]:
# pressed = 'up'
# elif keys[pygame.K_s]:
# pressed = 'down'
screen.blit(backg, (0, 0))
got.update()
Call.update()
backgscale = pygame.transform.scale(backg, (SCR_WID, SCR_HEI))
got.draw(screen)
Call.draw(screen)
#print "ball x, y: ", ball.rect.x, ball.rect.y
player.draw()
enemy.draw()
if ball.rect.x <= 200:
screen_np = (pygame.surfarray.array2d(screen)
[SCREEN_REDUCE*2:SCR_WID - SCREEN_REDUCE, :])
screen_np = np.append(screen_np.flatten(), player.y)
screen_data.append(screen_np)
player.movement()
enemy.movement()
ball.movement()
player.scoring(screen_data)
enemy.scoring(screen_data)
pygame.display.flip()
clock.tick(FPS)
i += 1
if __name__ == "__main__":
main()