-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpythonPong.py
More file actions
130 lines (108 loc) · 3.67 KB
/
pythonPong.py
File metadata and controls
130 lines (108 loc) · 3.67 KB
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
import numpy
import pygame
from pygame.locals import *
from sys import exit
import random
import pygame.surfarray as surfarray
pygame.init()
pygame.font.init()
screen = pygame.display.set_mode((640,480),0,32)
#Variaveis do jogo // Game variables
jogo = pygame.Surface((640,480))
fundo = jogo.convert()
fundo.fill((0,0,0))
bar = pygame.Surface((10,50))
barra1 = bar.convert()
barra1.fill((255,255,255))
barra2 = bar.convert()
barra2.fill((255,255,255))
boladraw = pygame.Surface((15,15))
bolamd = pygame.draw.circle(boladraw,(255,255,255),(int(15/2),int(15/2)),int(15/2))
bola = boladraw.convert()
bola.set_colorkey((0,0,0))
# Variaveis de definicoes tipo velocidade // Variables for some definitions like speed and stuff
barra1_x, barra2_x = 10. , 620.
barra1_y, barra2_y = 215. , 215.
bola_x, bola_y = 307.5, 232.5
mov_barra1, mov_barra2 = 0. , 0.
vel_x, vel_y, vel_bola = 250., 250., 250.
jog1 = "Player 1"
jog2 = "Player 2"
placar_barra1, placar_barra2 = 0,0
relogio = pygame.time.Clock()
font = pygame.font.SysFont("calibri",40)
pronto = False
while pronto==False:
for evento in pygame.event.get():
if evento.type == pygame.QUIT:
pronto = True
if evento.type == KEYDOWN:
if evento.key == K_UP:
mov_barra1 = -vel_ai
elif evento.key == K_DOWN:
mov_barra1 = vel_ai
elif evento.type == KEYUP:
if evento.key == K_UP:
mov_barra1 = 0.
elif evento.key == K_DOWN:
mov_barra1 = 0.
# Score render // renderizacao do placar
placar1 = font.render(str(placar_barra1), True,(255,255,255))
placar2 = font.render(str(placar_barra2), True,(255,255,255))
jogador1 = font.render(jog1, True, (255,255,255))
jogador2 = font.render(jog2, True, (255,255,255))
# Renderizacao dos elementos na tela
screen.blit(fundo,(0,0))
frame = pygame.draw.rect(screen,(255,255,255),Rect((5,5),(630,470)),2)
middle_line = pygame.draw.aaline(screen,(255,255,255),(330,5),(330,475))
screen.blit(barra1,(barra1_x,barra1_y))
screen.blit(barra2,(barra2_x,barra2_y))
screen.blit(bola,(bola_x,bola_y))
screen.blit(placar1,(250.,210.))
screen.blit(placar2,(380.,210.))
screen.blit(jogador1,(100.,20.))
screen.blit(jogador2,(450.,20.))
barra1_y += mov_barra1
# movimento da bola // ball movement
tempo = relogio.tick(30)
tempo_seg = tempo / 1000.0
bola_x += vel_x * tempo_seg
bola_y += vel_y * tempo_seg
vel_ai = vel_bola * tempo_seg
# AI do jogador 2 // Player 2 AI
if bola_x >= 305.:
if not barra2_y == bola_y + 7.5:
if barra2_y < bola_y + 7.5:
barra2_y += vel_ai
if barra2_y > bola_y - 42.5:
barra2_y -= vel_ai
else:
barra2_y == bola_y + 7.5
if barra1_y >= 420.: barra1_y = 420.
elif barra1_y <= 10. : barra1_y = 10.
if barra2_y >= 420.: barra2_y = 420.
elif barra2_y <= 10.: barra2_y = 10.
if bola_x <= barra1_x + 10.:
if bola_y >= barra1_y - 7.5 and bola_y <= barra1_y + 42.5:
bola_x = 20.
vel_x = -vel_x
if bola_x >= barra2_x - 15.:
if bola_y >= barra2_y - 7.5 and bola_y <= barra2_y + 42.5:
bola_x = 605.
vel_x = -vel_x
if bola_x < 5.:
placar_barra2 += 1
bola_x, bola_y = 320., 232.5
barra1_y,bar_2_y = 215., 215.
elif bola_x > 620.:
placar_barra1 += 1
bola_x, bola_y = 307.5, 232.5
barra1_y, barra2_y = 215., 215.
if bola_y <= 10.:
vel_y = -vel_y
bola_y = 10.
elif bola_y >= 457.5:
vel_y = -vel_y
bola_y = 457.5
pygame.display.update()
pygame.quit()