-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmoving.py
More file actions
80 lines (71 loc) · 1.96 KB
/
moving.py
File metadata and controls
80 lines (71 loc) · 1.96 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
import pygame
from pygame.locals import *
from random import randint
pygame.init()
pygame.mouse.set_visible(False)
BGCOLOR = (150,255,160)
COLOR = (120,182,80)
HSPEED = 20
VSPEED = 15
FONTSIZE = 100
screen = pygame.display.set_mode((1600,900),FULLSCREEN)
clock = pygame.time.Clock()
font = pygame.font.SysFont(None, FONTSIZE)
isRunning = True
move = [False, False]
rect = pygame.Rect((700,785,200,30))
ball = pygame.Rect((785,0,30,30))
ballV = VSPEED
points = 0
def getInput():
isRunning = True
for event in pygame.event.get():
if event.type == QUIT:
isRunning = False
elif event.type == KEYDOWN:
if event.key == K_ESCAPE or event.key == K_q:
isRunning = False
elif event.key == K_LEFT:
move[0] = True
elif event.key == K_RIGHT:
move[1] = True
elif event.type == KEYUP:
if event.key == K_LEFT:
move[0] = False
if event.key == K_RIGHT:
move[1] = False
return isRunning
def update():
global ballV,points
if move[0] and not rect.left <=0:
rect.move_ip(-HSPEED,0)
if move[1] and not rect.right >= 1600:
rect.move_ip(HSPEED,0)
ball.move_ip(0,ballV)
if ball.top >= 900:
#ballV = -VSPEED
newBall()
if ball.bottom < 0:
newBall()
if ball.colliderect(rect) and ballV > 0 and not ball.top > rect.top:
ballV=-VSPEED
points += 1
def draw():
screen.fill(BGCOLOR)
screen.fill(COLOR,rect)
screen.fill(COLOR,ball)
text = font.render("%02d"%points,True,COLOR,BGCOLOR)
fps = font.render("%.2f" % clock.get_fps(),True,COLOR,BGCOLOR)
screen.blit(text,(50,50))
screen.blit(fps, (1400,50))
pygame.display.flip()
def newBall():
global ballV
ball.topleft = (randint(0,1580),0)
ballV=VSPEED
while isRunning:
isRunning = getInput()
update()
draw()
clock.tick(30)
pygame.quit()