forked from thecount12/rapidpythonprogramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollision1.py
More file actions
80 lines (68 loc) · 1.71 KB
/
collision1.py
File metadata and controls
80 lines (68 loc) · 1.71 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
#!/usr/bin/python
# collision.py
# Chapter 16 Game Programming
# Author: William C. Gunnells
# Rapid Python Programming
# libs
import pygame
import random
white=(255,255,255) # RGB values
black=(0,0,0)
green=(0,255,0)
red=(255,0,0)
yellow=(255,255,0)
blue=(0,0,255)
pygame.init()
display=pygame.display.set_mode((800,600))
clock = pygame.time.Clock()
font=pygame.font.SysFont(None,25)
bg=pygame.image.load("gimpbackground.png")
star=pygame.image.load('assortstar1.png')
def message(msg,color,posx,posy):
mytext=font.render(msg,True,color)
display.blit(mytext,[posx,posy])
def game():
exit=False
x=300; y=300
xChange=0; yChange=0
boxX=round(random.randrange(0,800-10) /10.0) *10.0
boxY=round(random.randrange(0,600-10) /10.0) *10.0
while not exit: # exit loop
for event in pygame.event.get(): # event handling
if event.type==pygame.QUIT:
exit=True
k=pygame.key.get_pressed()
if k[pygame.K_LEFT]:
xChange-=10
elif k[pygame.K_RIGHT]:
xChange+=10
elif k[pygame.K_UP]:
yChange-=10
elif k[pygame.K_DOWN]:
yChange+=10
else:
xChange=0
yChange=0
if x==700: # borders
xChange=-10; yChange=0
if x==10:
xChange=10; yChange=0
if y==500:
yChange=-10; xChange=0
if y==10:
exit=True
x += xChange # continue from previous location
y += yChange
display.blit(bg,(0,0))
message("Move square in any direction...", yellow, 300,50)
pygame.draw.rect(display,yellow,[boxX,boxY,40,40])
display.blit(star,[x,y])
pygame.display.update()
if x== boxX and y==boxY: #collision
print("Boom!!!")
boxX=round(random.randrange(0,800-10) /10.0) *10.0
boxY=round(random.randrange(0,600-10) /10.0) *10.0
clock.tick(10)
pygame.quit()
quit()
game()