-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
176 lines (137 loc) · 5.04 KB
/
test.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
import pygame
from pygame.locals import *
from random import randint
yes = True
maybe = True
while True:
name1 = input("Nafn uppí 5 characters: ")
name2 = input("Nafn uppí 5 characters: ")
if len(name1) > 5 or len(name2) > 5:
print("5 eða minni stafir")
else:
break
pygame.init()
pygame.font.init()
window_size = window_width, window_height = 960, 770
window = pygame.display.set_mode(window_size)
pygame.display.set_caption('Pong')
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
litur = [255, 255, 255]
litur2 = [255, 255, 255]
rainbow = (randint(0,255), randint(0,255), randint(0,255))
myfont = pygame.font.SysFont('Comic Sans MS', 30)
ballXList = [-2, 2]
ballYList = [-2, -1, 1, 2]
player1_x_position = 60
player1_y_position = 330
player2_x_position = 890
player2_y_position = 330
player1_y_velocity = 0
player2_y_velocity = 0
ball_xpos = 480
ball_ypos = 360
ball_xvel = ballXList[randint(0,1)]
ball_yvel = ballYList[randint(0,3)]
score1 = 0
score2 = 0
hradi = 1
window.fill(BLACK)
clock = pygame.time.Clock()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
player2_y_velocity = -7
elif event.key == pygame.K_DOWN:
player2_y_velocity = 7
if event.key == pygame.K_w:
player1_y_velocity = -7
elif event.key == pygame.K_s:
player1_y_velocity = 7
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP:
player2_y_velocity = 0
elif event.key == pygame.K_DOWN:
player2_y_velocity = 0
if event.key == pygame.K_w:
player1_y_velocity = 0
elif event.key == pygame.K_s:
player1_y_velocity = 0
rainbow = (randint(0, 255), randint(0, 255), randint(0, 255))
ball_xpos += ball_xvel
ball_ypos += ball_yvel
player1_y_position += player1_y_velocity
player2_y_position += player2_y_velocity
window.fill(BLACK)
# Objects
ball = pygame.draw.circle(window, WHITE, (ball_xpos, ball_ypos), 10)
rect1 = pygame.draw.rect(window, litur, pygame.Rect(player1_x_position, player1_y_position, 15, 60))
rect2 = pygame.draw.rect(window, litur2, pygame.Rect(player2_x_position, player2_y_position, 15, 60))
# Lines
pygame.draw.line(window, WHITE, (0, 70), (1000, 70))
pygame.draw.line(window, RED, (60+15, player1_y_position + 60), (60+15, player1_y_position))
pygame.draw.line(window, RED, (890, player2_y_position + 60), (890, player2_y_position))
# Text
name1Score = myfont.render(str(name1), True, (WHITE))
window.blit(name1Score, (10, -5))
player1Score = myfont.render(str(score1), True, (WHITE))
window.blit(player1Score, (25, 25))
name1Score = myfont.render(str(name2), True, (WHITE))
window.blit(name1Score, (880, -5))
player2Score = myfont.render(str(score2), True, (WHITE))
window.blit(player2Score, (900, 25))
hraditexti = myfont.render("Hraði: "+ str(hradi), True, (WHITE))
window.blit(hraditexti, (435, -5))
# Gáir hvort að boltinn hittir hægri spilari
if player1_x_position < ball_xpos < player1_x_position + 25 and player1_y_position < ball_ypos < player1_y_position + 60:
print("hit1")
litur = [randint(0, 255), randint(0, 255), randint(0, 255)]
ball_xvel *= -1
ball_xvel += 2
hradi += 1
# Gáir hvort að bolti hittir vinsti spilari
if player2_x_position - 5 < ball_xpos < player2_x_position + 15 and player2_y_position < ball_ypos < player2_y_position + 60:
print("hit2")
litur2 = [randint(0, 255), randint(0, 255), randint(0, 255)]
ball_xvel *= -1
ball_xvel -= 2
hradi += 1
# Ef y position er
if ball_ypos > 760 or ball_ypos < 80:
ball_yvel *= -1
if ball_xpos > 960:
ball_xpos = 480
ball_ypos = 360
ball_xvel = ballXList[randint(0,1)]
ball_yvel = randint(-20, 20)
score1 +=1
hradi = 1
if ball_xpos < 0:
ball_xpos = 480
ball_ypos = 360
ball_xvel = ballXList[randint(0,1)]
ball_yvel = ballYList[randint(0,3)]
score2 += 1
hradi = 1
if player1_y_position > 708 or player1_y_position < 75:
if player1_y_position > 708:
player1_y_position = 710
if player1_y_position < 75:
player1_y_position = 70
player1_y_velocity = 0
if player2_y_position > 708 or player2_y_position < 75:
if player2_y_position > 708:
player2_y_position = 710
if player2_y_position < 75:
player2_y_position = 70
player2_y_velocity = 0
pygame.display.update()
clock.tick(60)
pygame.quit()