-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path4-2-2.pongGame.py
50 lines (42 loc) · 1.19 KB
/
4-2-2.pongGame.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
# 바. pygame.time 모듈
# Pong Game
import pygame, random
from pygame.locals import *
pygame.init()
w, h = 500, 500
screen = pygame.display.set_mode((w, h))
pygame.display.set_caption('Pong Game')
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
ball_x = random.randint(0, 250)
ball_y = random.randint(0, 250)
move_x, move_y = 8, 8
paddle_x, paddle_y = 220, 470
fps = pygame.time.Clock()
gameover = False
while not gameover:
for event in pygame.event.get():
if event.type == QUIT:
gameover = True
elif event.type == MOUSEMOTION:
paddle_x = event.pos[0]
ball_x += move_x
ball_y += move_y
ball_rect = Rect(ball_x, ball_y, 20, 20)
paddle_rect = Rect(paddle_x, paddle_y, 60, 20)
if ball_y+20 >= paddle_y and \
paddle_x <= ball_x <= paddle_x+60:
move_y = -abs(move_y)
if ball_x < 0 or ball_x > w:
move_x = -move_x
if ball_y < 0:
move_y = -move_y
if ball_y > paddle_y+20:
gameover = True
print('You lost! Game over!')
screen.fill(BLACK)
pygame.draw.ellipse(screen, WHITE, ball_rect)
pygame.draw.rect(screen, WHITE, paddle_rect)
pygame.display.update()
fps.tick(40)
pygame.quit()