-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpygameEvent3.py
More file actions
52 lines (45 loc) · 1.02 KB
/
pygameEvent3.py
File metadata and controls
52 lines (45 loc) · 1.02 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
#!/usr/bin/python
# pygameEvent3.py
# Chapter 16 Game Programming
# Author: William C. Gunnells
# Rapid Python Programming
# libs
import pygame
white=(255,255,255) # RGB values
black=(0,0,0)
pygame.init()
display=pygame.display.set_mode((800,600))
x=300; y=300
xChange=0; yChange=0
clock = pygame.time.Clock()
while 1:
for event in pygame.event.get(): # event handling
if event.type==pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
xChange = - 10
yChange = 0
elif event.key == pygame.K_RIGHT:
xChange = 10
yChange = 0
elif event.key == pygame.K_UP:
yChange = - 10
xChange = 0
elif event.key == pygame.K_DOWN:
yChange = 10
xChange = 0
# if x >= 800 or x <= 0 or y >= 600 or y <= 0:
# break
if x == 700:
xChange=-10
yChange=0
elif x==10:
xChange=0
yChange=0
x += xChange
y += yChange
display.fill(white) # not rendered without update
pygame.draw.rect(display,black,[x,y,10,10]) # coords, w,h
pygame.display.update()
clock.tick(10)
pygame.quit()
quit()