Crossin的编程教室
标题:
关于按键 控制打飞机游戏的问题
[打印本页]
作者:
920428684
时间:
2018-5-5 21:57
标题:
关于按键 控制打飞机游戏的问题
我想让w按下后 小人就一直往上走 而不是 每次移动都要按一次 w 想用一个标志量来记录 但是效果不对
求大家帮忙看下 谢谢!
这是程序:
import
pygame
from
sys
import
exit
import
random
class
Hero:
def
__init__
(
self
):
self
.image = pygame.image.load(
'image\huaji.jpg'
).convert_alpha()
self
.x = random.randint(
100
,
800
) -
self
.image.get_width() /
2
self
.y = random.randint(
200
,
500
) -
self
.image.get_height() /
2
self
.speed =
20
def
move(
self
,x,y):
self
.x = x
self
.y = y
class
Bullet:
def
__init__
(
self
):
self
.x =
0
self
.y =
0
pygame.init()
screen = pygame.display.set_mode((
1000
,
500
),
0
,
32
)
pygame.display.set_caption(
"Fly_game"
)
bg = pygame.image.load(
"image
\\
bg.jpg"
).convert()
clock = pygame.time.Clock()
hero = Hero()
w_flag =
0
s_flag =
0
a_flag =
0
d_flag =
0
while True
:
for
event
in
pygame.event.get():
if
event.type == pygame.QUIT:
pygame.quit()
exit()
if
event.type == pygame.KEYDOWN:
if
event.key == pygame.K_w:
w_flag =
1
elif
event.key == pygame.K_s:
s_flag =
1
elif
event.key == pygame.K_a:
a_flag =
1
elif
event.key == pygame.K_d:
d_flag =
1
if
event.type == pygame.KEYUP:
if
event.key == pygame.K_w:
w_flag =
0
elif
event.key == pygame.K_s:
s_flag =
0
elif
event.key == pygame.K_a:
a_flag =
0
elif
event.key == pygame.K_d:
d_flag =
0
if
w_flag:
hero.y -= hero.speed
if
s_flag:
hero.y += hero.speed
if
a_flag:
hero.x -= hero.speed
if
d_flag:
hero.x += hero.speed
if
hero.x <
0
:
hero.x =
0
if
hero.x >
1000
:
hero.x =
1000
if
hero.y <
0
:
hero.y =
0
if
hero.y >
800
:
hero.y =
800
print
(d_flag,hero.x)
screen.blit(bg, (
0
,
0
))
screen.blit(hero.image,(hero.x,hero.y))
pygame.display.update()
clock.tick(
50
)
作者:
crossin先生
时间:
2018-5-7 01:34
设置标志放在 event 里,小人移动不应该放在 event 里
欢迎光临 Crossin的编程教室 (https://bbs.crossincode.com/)
Powered by Discuz! X2.5