forked from sd18spring/InteractiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasic_Movements.py
More file actions
69 lines (59 loc) · 1.81 KB
/
Basic_Movements.py
File metadata and controls
69 lines (59 loc) · 1.81 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
import pygame
from pygame.locals import *
#Initialize the screen
screen = pygame.display.set_mode((1024, 768))
pygame.display.set_caption("Smash Clone")
clock = pygame.time.Clock()
FPS = 120
#Variable to keep the game running
running = True
#the sample rectangle, danny
danny = pygame.rect.Rect((20, 20, 250, 100))
print(danny)
#booleans for control toggles.
left = False
right = False
up = False
down = False
while running:
#make the background pretty close to black
screen.fill((0, 15, 15))
#For each event at the moment:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
#KEYDOWN events toggle movement on
if event.type == KEYDOWN:
if event.key == pygame.K_LEFT:
left = True
if event.key == pygame.K_RIGHT:
right = True
if event.key == pygame.K_UP:
up = True
if event.key == pygame.K_DOWN:
down = True
#KEYUP events toggles movement off
if event.type == KEYUP:
if event.key == pygame.K_LEFT:
left = False
if event.key == pygame.K_RIGHT:
right = False
if event.key == pygame.K_UP:
up = False
if event.key == pygame.K_DOWN:
down = False
#actually moving the rectangle. move() is a fruitful function
if left:
danny = danny.move(-10, 0)
if right:
danny = danny.move(10, 0)
if up:
danny = danny.move(0, -10)
if down:
danny = danny.move(0, 10)
#drawing the rectangle to the screen, and updating the screen
pygame.draw.rect(screen, [255, 255 ,255], danny)
pygame.display.update()
#clock for consistent for loop timing.
clock.tick(FPS)
pygame.quit()