Skip to content

Commit 23b0ff3

Browse files
authored
Create testPyGame.py
Pygame Tutorial #1 - Basic Movement and Key Presses
1 parent bf32241 commit 23b0ff3

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

testPyGame.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#-------------------------------------------------------------------------------------------------------------
2+
# Pygame Tutorial #1 - Basic Movement and Key Presses
3+
# https://www.youtube.com/watch?v=i6xMBig-pP4
4+
# Tech With Tim
5+
#-------------------------------------------------------------------------------------------------------------
6+
import pygame
7+
pygame.init()
8+
9+
win = pygame.display.set_mode((500, 500))
10+
11+
pygame.display.set_caption("First Game")
12+
13+
x = 50
14+
y = 50
15+
width = 40
16+
height = 60
17+
velocity = 5
18+
19+
run = True
20+
while run:
21+
pygame.time.delay(100)
22+
23+
for event in pygame.event.get():
24+
if event.type == pygame.QUIT: # if user clicks on red button
25+
run = False # then run == False
26+
27+
keys = pygame.key.get_pressed()
28+
29+
if keys[pygame.K_LEFT]:
30+
x -= velocity # to move left you subtract from the x coordinate
31+
if keys[pygame.K_RIGHT]:
32+
x += velocity # to move right you add to the x coordinate
33+
if keys[pygame.K_UP]:
34+
y -= velocity # to move up you subtract from the y coordinate
35+
if keys[pygame.K_DOWN]:
36+
y += velocity # to move down you add to the y coordinate
37+
38+
# this code fill's the screen in black before you draw the red rectangle...
39+
win.fill((0,0,0)) # this makes the program fill the screen black thus erasing the previously drawn red rectangles giving
40+
# the appearance of the red rectangle moving and not leaving traces as it did previously without this line of code...
41+
# window, rgb color for the shape, dimensions of the shape
42+
pygame.draw.rect(win, (255, 0, 0), (x, y, width, height)) # parameters...Everythong in pygame is a surface
43+
pygame.display.update() # this refreshes the display thus showing the current code's result which is making a rectangular figure
44+
45+
46+
47+
pygame.quit() # and quit game...

0 commit comments

Comments
 (0)