|
keys=pygame.key.get_pressed(),接受游戏中当前按下的所有按键,并把它们保存为一个列表,放在keys变量中
pygame.K_w告诉PyGame你在检查w键,通过修改最后的一个字母,你可以把它修改为任何你想要检查的按键
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| import pygame
pygame.init()
size=[400,300]
screen=pygame.display.set_mode(size)
clock=pygame.time.Clock()
done=False
while not done:
keys=pygame.key.get_pressed()
if keys[pygame.K_w]:
print "hello"
for event in pygame.event.get():
if event.type==pygame.QUIT:
done=True
clock.tick(32)
pygame.quit()
|
控制小球上下左右移动
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
| import pygame
pygame.init()
size=[400,300]
screen=pygame.display.set_mode(size)
clock=pygame.time.Clock()
x=size[0]/2
y=size[1]/2
red=pygame.color.Color('#FF8080')
blue=pygame.color.Color('#8080FF')
white=pygame.color.Color('#FFFFFF')
black=pygame.color.Color('#000000')
done=False
while not done:
screen.fill(black)
keys=pygame.key.get_pressed()
if keys[pygame.K_w]:
y-=1
if keys[pygame.K_s]:
y+=1
if keys[pygame.K_a]:
x-=1
if keys[pygame.K_d]:
x+=1
pygame.draw.circle(screen,red,[x,y],6)
pygame.display.flip()
for event in pygame.event.get():
if event.type==pygame.QUIT:
done=True
clock.tick(72)
pygame.quit()
|
注:防止小球出屏幕,加判断
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
| import pygame
pygame.init()
size=[400,300]
screen=pygame.display.set_mode(size)
clock=pygame.time.Clock()
x=size[0]/2
y=size[1]/2
red=pygame.color.Color('#FF8080')
blue=pygame.color.Color('#8080FF')
white=pygame.color.Color('#FFFFFF')
black=pygame.color.Color('#000000')
def checkOffScreenX(x):
if x>size[0]:
x=0
elif x<0:
x=size[0]
return x
def checkOffScreenY(y):
if y>size[1]:
y=0
elif y<0:
y=size[1]
return y
done=False
while not done:
screen.fill(black)
keys=pygame.key.get_pressed()
if keys[pygame.K_w]:
y-=1
if keys[pygame.K_s]:
y+=1
if keys[pygame.K_a]:
x-=1
if keys[pygame.K_d]:
x+=1
x=checkOffScreenX(x)
y=checkOffScreenY(y)
pygame.draw.circle(screen,red,[x,y],6)
pygame.display.flip()
for event in pygame.event.get():
if event.type==pygame.QUIT:
done=True
clock.tick(72)
pygame.quit()
|
完整游戏代码:
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
| import random
import pygame
pygame.init()
size=[400,300]
screen=pygame.display.set_mode(size)
clock=pygame.time.Clock()
x=size[0]/2
y=size[1]/2
ballX=random.randrange(0,size[0])
ballY=random.randrange(0,size[1])
goalX=size[0]/2-10
goalY=size[1]/2-10
goalW=20
goalH=20
points=0
red=pygame.color.Color('#FF8080')
blue=pygame.color.Color('#8080FF')
white=pygame.color.Color('#FFFFFF')
black=pygame.color.Color('#000000')
def checkOffScreenX(x):
if x>size[0]:
x=0
elif x<0:
x=size[0]
return x
def checkOffScreenY(y):
if y>size[1]:
y=0
elif y<0:
y=size[1]
return y
def checkTouching():
global x
global ballX
global y
global ballY
if -10<y-ballY<10 and -10<x-ballX<10:
pygame.draw.circle(screen,white,[x,y],15)
xDiff=x-ballX
yDiff=y-ballY
if ballX==0:
xDiff-=5
elif ballX==size[0]:
xDiff+=5
if ballY==0:
yDiff-=5
elif ballY==size[1]:
yDiff+=5
x+=xDiff*3
ballX-=xDiff*3
y+=yDiff*3
ballY-=yDiff*3
done=False
while not done:
screen.fill(black)
pygame.draw.rect(screen,white,(goalX,goalY,goalW,goalH))
keys=pygame.key.get_pressed()
if keys[pygame.K_w]:
y-=1
if keys[pygame.K_s]:
y+=1
if keys[pygame.K_a]:
x-=1
if keys[pygame.K_d]:
x+=1
x=checkOffScreenX(x)
y=checkOffScreenY(y)
ballX=checkOffScreenX(ballX)
ballY=checkOffScreenY(ballY)
checkTouching()
for point in range(points):
pointX=0+point*5
pygame.draw.rect(screen,white,(pointX,3,4,10))
pygame.draw.circle(screen,red,[x,y],6)
pygame.draw.circle(screen,blue,[ballX,ballY],6)
if goalX<=ballX<=goalX+goalW and goalY<=ballY<=goalY+goalH:
points+=1
ballX=random.randrange(0,size[0])
ballY=random.randrange(0,size[1])
pygame.display.flip()
for event in pygame.event.get():
if event.type==pygame.QUIT:
done=True
clock.tick(72)
pygame.quit()
print "Total points: "+str(points)
|
|
|