- 帖子
- 17
- 精华
- 0
- 积分
- 103
- 阅读权限
- 20
- 注册时间
- 2017-2-19
- 最后登录
- 2018-8-22
|
我想让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)
|
|