-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMiniAssignment.py
111 lines (80 loc) · 3.01 KB
/
MiniAssignment.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
import pygame, random, sys
from pygame.locals import *
pygame.init()
window_height=500
window_width=500
black = (0,0,0)
green = (0, 255, 0)
fps = 20
speed = 10
font = pygame.font.SysFont(None, 28)
mainClock = pygame.time.Clock()
Canvas = pygame.display.set_mode((window_width,window_height))
pygame.display.set_caption('MINI PROJECT')
pygame.display.update()
ballimage = pygame.image.load('ball.png')
ballrect = ballimage.get_rect()
while True:
moveup = movedown = moveright = moveleft = False
ballrect.centerx = ballrect.centery = 250
while True:
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_UP:
moveup = True
if event.key == K_DOWN:
movedown = True
if event.key == K_LEFT:
moveleft = True
if event.key == K_RIGHT:
moveright = True
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
if event.type == KEYUP:
if event.key == K_UP:
moveup = False
if event.key == K_DOWN:
movedown = False
if event.key == K_LEFT:
moveleft = False
if event.key == K_RIGHT:
moveright = False
if (moveup and (ballrect.top > 0)):
ballrect.top-= speed
if (movedown and (ballrect.bottom < 500)):
ballrect.bottom += speed
if (moveleft and (ballrect.left > 0)):
ballrect.left -= speed
if (moveright and (ballrect.right < 500)):
ballrect.right += speed
x = str(ballrect.centerx)
y = str(ballrect.centery)
t = ','
m = '('
n = ')'
pos_x = font.render(x, 1, green)
pos_y = font.render(y, 1, green)
pos_c = font.render(t, 1, green)
pos_m = font.render(m, 1, green)
pos_n = font.render(n, 1, green)
pos_xrect = pos_x.get_rect()
pos_yrect = pos_y.get_rect()
pos_crect = pos_c.get_rect()
pos_mrect = pos_m.get_rect()
pos_nrect = pos_n.get_rect()
pos_xrect.topleft = (10, 10)
pos_crect.topleft = (45, 10)
pos_yrect.topleft = (50, 10)
pos_mrect.topleft = (5, 10)
pos_nrect.topleft = (85, 10)
Canvas.fill(black)
Canvas.blit(pos_x, pos_xrect)
Canvas.blit(pos_c, pos_crect)
Canvas.blit(pos_y, pos_yrect)
Canvas.blit(pos_m, pos_mrect)
Canvas.blit(pos_n, pos_nrect)
Canvas.blit(ballimage, ballrect)
pygame.display.update()
mainClock.tick(fps)
pygame.display.update()