在pygame中,为什么我不能在按住按钮的同时逐渐移动我的精灵?
问题描述:
正如我的标题中明显指出的那样,为什么我的精灵只会每按一次按键,(而不是按住按键时移动)。顺便说一下,真的很抱歉,您将无法当我移除阻止你走进墙壁的所有块以允许它适合字符限制(30000)时,正确运行它。这里是我的代码:在pygame中,为什么我不能在按住按钮的同时逐渐移动我的精灵?
import pygame
from pygame.locals import*
import pygame
#initialise pygame
pygame.init()
WHITE = (255,255,255)
start_in_bed = 0
#counts which sprite you should be on when running
#create screen
screen_width = 160
screen_height = 144
screen_multiplier = 4
screen = pygame.display.set_mode(((screen_width*screen_multiplier),(screen_height*screen_multiplier)))
pygame.display.set_caption('Pokemon Blood Red')
#Sprite stuff
sprite = pygame.image.load('player_south_still.png')
#Reform the sprite
sprite = pygame.transform.scale(sprite, (10*screen_multiplier, 14*screen_multiplier))
sprite.set_colorkey(WHITE)
class Player(pygame.sprite.Sprite):
def __init__(self):
super(Player, self).__init__()
self.player_sprite = pygame.Surface((10*screen_multiplier, 14*screen_multiplier))
self.player_sprite.fill(WHITE)
self.rect = self.player_sprite.get_rect()
self.rect.x = 160/2
self.rect.y = 144/2
class Blocks(pygame.sprite.Sprite):
def __init__(self, width, height):
super(Blocks, self).__init__()
block_width = width
self.image = pygame.Surface([width, height])
self.image.fill(WHITE)
self.rect = self.image.get_rect()
self.block_width = width
self.block_height = height
#######################################################################
#Create all blockades ever
player = Player()
player.rect.x = (126*screen_multiplier)
player.rect.y = (100*screen_multiplier)
nt_trees_left = Blocks(15*screen_multiplier, 144*screen_multiplier)
nt_trees_left.rect.x = 0*screen_multiplier
nt_trees_left.rect.y = 0*screen_multiplier
screen.blit(nt_trees_left.image, (nt_trees_left.rect.x, nt_trees_left.rect.y))
nt_trees_top = Blocks(160*screen_multiplier, 14*screen_multiplier)
nt_trees_top.rect.x = 33*screen_multiplier
nt_trees_top.rect.y = 0*screen_multiplier
screen.blit(nt_trees_top.image, (nt_trees_top.rect.x, nt_trees_top.rect.y))
laboratory = Blocks(50*screen_multiplier, 46*screen_multiplier)
laboratory.rect.x = 98*screen_multiplier
laboratory.rect.y = 0*screen_multiplier
screen.blit(laboratory.image, (laboratory.rect.x, laboratory.rect.y))
nt_trees_right = Blocks(15*screen_multiplier, 72*screen_multiplier)
nt_trees_right.rect.x = 148*screen_multiplier
nt_trees_right.rect.y = 0*screen_multiplier
screen.blit(nt_trees_right.image, (nt_trees_right.rect.x, nt_trees_right.rect.y))
nt_trees_right_bottom = Blocks(15*screen_multiplier, 65*screen_multiplier)
nt_trees_right_bottom.rect.x = 148*screen_multiplier
nt_trees_right_bottom.rect.y = 91*screen_multiplier
screen.blit(nt_trees_right_bottom.image, (nt_trees_right_bottom.rect.x, nt_trees_right_bottom.rect.y))
nt_trees_bottom = Blocks(160*screen_multiplier, 14*screen_multiplier)
nt_trees_bottom.rect.x = 0*screen_multiplier
nt_trees_bottom.rect.y = 128*screen_multiplier
screen.blit(nt_trees_bottom.image, (nt_trees_bottom.rect.x, nt_trees_bottom.rect.y))
nt_player_house = Blocks(20*screen_multiplier, 22*screen_multiplier)
nt_player_house.rect.x = 38*screen_multiplier
nt_player_house.rect.y = 53*screen_multiplier
screen.blit(nt_player_house.image, (nt_player_house.rect.x, nt_player_house.rect.y))
nt_friend_house = Blocks(20*screen_multiplier, 22*screen_multiplier)
nt_friend_house.rect.x = 70*screen_multiplier
nt_friend_house.rect.y = 53*screen_multiplier
screen.blit(nt_friend_house.image, (nt_friend_house.rect.x, nt_friend_house.rect.y))
################################################################################
home_upstairs_back_wall = Blocks(78*screen_multiplier, 35*screen_multiplier)
home_upstairs_back_wall.rect.x = 0
home_upstairs_back_wall.rect.y = 10*screen_multiplier
screen.blit(home_upstairs_back_wall.image, (home_upstairs_back_wall.rect.x, home_upstairs_back_wall.rect.y))
home_upstairs_back_back_wall = Blocks(78*screen_multiplier, 24*screen_multiplier)
home_upstairs_back_back_wall.rect.x = 80*screen_multiplier
home_upstairs_back_back_wall.rect.y = 10*screen_multiplier
screen.blit(home_upstairs_back_back_wall.image, (home_upstairs_back_back_wall.rect.x, home_upstairs_back_back_wall.rect.y))
home_upstairs_tree = Blocks(15*screen_multiplier, 42*screen_multiplier)
home_upstairs_tree.rect.x = 0*screen_multiplier
home_upstairs_tree.rect.y = 96*screen_multiplier
screen.blit(home_upstairs_tree.image, (home_upstairs_tree.rect.x, home_upstairs_tree.rect.y))
home_upstairs_left_wall = Blocks(1*screen_multiplier, 144*screen_multiplier)
home_upstairs_left_wall.rect.x = 0*screen_multiplier
home_upstairs_left_wall.rect.y = 0*screen_multiplier
screen.blit(home_upstairs_left_wall.image, (home_upstairs_left_wall.rect.x, home_upstairs_left_wall.rect.y))
home_upstairs_right_wall = Blocks(1*screen_multiplier, 144*screen_multiplier)
home_upstairs_right_wall.rect.x = 153*screen_multiplier
home_upstairs_right_wall.rect.y = 0*screen_multiplier
screen.blit(home_upstairs_right_wall.image, (home_upstairs_right_wall.rect.x, home_upstairs_right_wall.rect.y))
home_upstairs_bottom_wall = Blocks(160*screen_multiplier, 1*screen_multiplier)
home_upstairs_bottom_wall.rect.x = 0*screen_multiplier
home_upstairs_bottom_wall.rect.y = 130*screen_multiplier
screen.blit(home_upstairs_bottom_wall.image, (home_upstairs_bottom_wall.rect.x, home_upstairs_bottom_wall.rect.y))
home_upstairs_banister = Blocks(22*screen_multiplier, 5*screen_multiplier)
home_upstairs_banister.rect.x = 131*screen_multiplier
home_upstairs_banister.rect.y = 62*screen_multiplier
screen.blit(home_upstairs_banister.image, (home_upstairs_banister.rect.x, home_upstairs_banister.rect.y))
################################################################################
class Doors(pygame.sprite.Sprite):
def __init__(self, width, height):
super(Doors, self).__init__()
self.player_house_door = pygame.Surface((width, height))
self.player_house_door.fill(WHITE)
self.rect = self.player_house_door.get_rect()
self.rect.x = 40*screen_multiplier
self.rect.y = 75*screen_multiplier
player_house_door = Doors(8*screen_multiplier, 2*screen_multiplier)
player_group = pygame.sprite.Group()
player_group.add(player)
inside_house_door = Doors(22*screen_multiplier, 18*screen_multiplier)
inside_house_door.rect.x = 80*screen_multiplier
inside_house_door.rect.y = 13*screen_multiplier
screen.blit(inside_house_door.player_house_door, (inside_house_door.rect.x, inside_house_door.rect.y))
extra_door = Doors(8*screen_multiplier, 2*screen_multiplier)
################################################################################
place = 3
def area_load():
global background
if place == 0:
global start_in_bed
if start_in_bed == 0:
player.rect.x = 126*screen_multiplier
player.rect.y = 105*screen_multiplier
sprite = pygame.image.load('player_south_still.png')
else:
player.rect.x = 110*screen_multiplier
player.rect.y = 50*screen_multiplier
sprite = pygame.image.load('player_west_still.png')
inside_house_door.rect.x = 130*screen_multiplier
inside_house_door.rect.y = 50*screen_multiplier
sprite = pygame.image.load('player_west_still.png')
background = pygame.image.load('player_house_upstairs.png').convert()
background = pygame.transform.scale(background, (160*screen_multiplier, 144*screen_multiplier))
screen.blit(background, (0, 0))
screen.blit(player.player_sprite, (player.rect.x, player.rect.y))
if place == 1:
start_in_bed += 1
inside_house_door.rect.x = 110*screen_multiplier
inside_house_door.rect.y = 24*screen_multiplier
#Player rect.x and rect.y are determined in collisions of doors so you end up at the right place
player_house_door.rect.x = 73*screen_multiplier
player_house_door.rect.y = 130*screen_multiplier
door_enter = pygame.sprite.spritecollide(player_house_door, player_group, False)
if door_enter:
while door_enter:
player.rect.y -= 1*screen_multiplier
door_enter = pygame.sprite.spritecollide(player_house_door, player_group, False)
background = pygame.image.load('player_house_bottom.png').convert()
background = pygame.transform.scale(background, (160*screen_multiplier, 144*screen_multiplier))
screen.blit(background, (0, 0))
if place == 2:
player_house_door.rect.x = 40*screen_multiplier
player_house_door.rect.y = 75*screen_multiplier
background = pygame.image.load('neuory_town.png').convert()
background = pygame.transform.scale(background, (160*screen_multiplier, 144*screen_multiplier))
screen.blit(background, (0, 0))
if place == 3:
player.rect.x = (70*screen_multiplier)
player.rect.y = (112*screen_multiplier)
inside_house_door.rect.x = 70*screen_multiplier
inside_house_door.rect.y = 133*screen_multiplier
extra_door.rect.x = 114*screen_multiplier
extra_door.rect.y = 47*screen_multiplier
sprite = pygame.image.load('player_north_still.png')
background = pygame.image.load('lab.png').convert()
background = pygame.transform.scale(background, (160*screen_multiplier, 144*screen_multiplier))
screen.blit(background, (0, 0))
area_load()
#######################################################################
#Random variables for later use
pygame.init()
amount_caught = 0
catch1 = {'pokemon':'none',
'hp':0,
'attack':0,
'defence':0,
'sp_attack':0,
'sp_defence':0,}
(x) = 160*0.45
(y) = 144*0.45
def caught():
if amount_caught == 0:
pass
#catch1 values are equal to wild one's
#Mainloop
crashed = False
while not crashed:
x_change = 0
y_change = 0
#Different buttons
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
pygame.quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -3*screen_multiplier
player.rect.x += x_change
screen.blit(player.player_sprite, (player.rect.x, player.rect.y))
if place == 3:
hits = pygame.sprite.spritecollide(player, lab_blocks, False)
if hits:
while hits:
print ('Collision!')
x_change = 1*screen_multiplier
player.rect.x += x_change
hits = pygame.sprite.spritecollide(player, lab_blocks, False)
if place == 2:
hits = pygame.sprite.spritecollide(player, nt_blocks, False)
if hits:
while hits:
print ('Collision!')
x_change = 1*screen_multiplier
player.rect.x += x_change
hits = pygame.sprite.spritecollide(player, nt_blocks, False)
if place == 1:
hits = pygame.sprite.spritecollide(player, home_downstairs_blocks, False)
if hits:
while hits:
print ('Collision!')
x_change = 1*screen_multiplier
player.rect.x += x_change
hits = pygame.sprite.spritecollide(player, home_downstairs_blocks, False)
if place == 0:
hits = pygame.sprite.spritecollide(player, home_upstairs_blocks, False)
if hits:
while hits:
print ('Collision!')
x_change = 1*screen_multiplier
player.rect.x += x_change
hits = pygame.sprite.spritecollide(player, home_upstairs_blocks, False)
sprite = pygame.image.load('player_west_still.png')
elif event.key == pygame.K_RIGHT:
x_change = 3*screen_multiplier
player.rect.x += x_change
screen.blit(player.player_sprite, (player.rect.x, player.rect.y))
if place == 3:
hits = pygame.sprite.spritecollide(player, lab_blocks, False)
if hits:
while hits:
print ('Collision!')
x_change = -1*screen_multiplier
player.rect.x += x_change
hits = pygame.sprite.spritecollide(player, lab_blocks, False)
if place == 2:
hits = pygame.sprite.spritecollide(player, nt_blocks, False)
if hits:
while hits:
print ('Collision!')
x_change = -1*screen_multiplier
player.rect.x += x_change
hits = pygame.sprite.spritecollide(player, nt_blocks, False)
if place == 1:
hits = pygame.sprite.spritecollide(player, home_downstairs_blocks, False)
if hits:
while hits:
print ('Collision!')
x_change = -1*screen_multiplier
player.rect.x += x_change
hits = pygame.sprite.spritecollide(player, home_downstairs_blocks, False)
if place == 0:
hits = pygame.sprite.spritecollide(player, home_upstairs_blocks, False)
if hits:
while hits:
print ('Collision!')
x_change = -1*screen_multiplier
player.rect.x += x_change
hits = pygame.sprite.spritecollide(player, home_upstairs_blocks, False)
sprite = pygame.image.load('player_east_still.png')
elif event.key == pygame.K_UP:
y_change = -3*screen_multiplier
player.rect.y += y_change
screen.blit(player.player_sprite, (player.rect.x, player.rect.y))
if place == 3:
hits = pygame.sprite.spritecollide(player, lab_blocks, False)
if hits:
while hits:
print ('Collision!')
y_change = 1*screen_multiplier
player.rect.y += y_change
hits = pygame.sprite.spritecollide(player, lab_blocks, False)
if place == 2:
hits = pygame.sprite.spritecollide(player, nt_blocks, False)
if hits:
while hits:
print ('Collision!')
y_change = 1*screen_multiplier
player.rect.y += y_change
hits = pygame.sprite.spritecollide(player, nt_blocks, False)
if place == 1:
hits = pygame.sprite.spritecollide(player, home_downstairs_blocks, False)
if hits:
while hits:
print ('Collision!')
y_change = 1*screen_multiplier
player.rect.y += y_change
hits = pygame.sprite.spritecollide(player, home_downstairs_blocks, False)
if place == 0:
hits = pygame.sprite.spritecollide(player, home_upstairs_blocks, False)
if hits:
while hits:
print ('Collision!')
y_change = 1*screen_multiplier
player.rect.y += y_change
hits = pygame.sprite.spritecollide(player, home_upstairs_blocks, False)
sprite = pygame.image.load('player_north_still.png')
elif event.key == pygame.K_DOWN:
y_change = 3*screen_multiplier
player.rect.y += y_change
screen.blit(player.player_sprite, (player.rect.x, player.rect.y))
if place == 3:
hits = pygame.sprite.spritecollide(player, lab_blocks, False)
if hits:
while hits:
print ('Collision!')
y_change = -1*screen_multiplier
player.rect.y += y_change
hits = pygame.sprite.spritecollide(player, lab_blocks, False)
if place == 2:
hits = pygame.sprite.spritecollide(player, nt_blocks, False)
if hits:
while hits:
print ('Collision!')
y_change = -1*screen_multiplier
player.rect.y += y_change
hits = pygame.sprite.spritecollide(player, nt_blocks, False)
if place == 1:
hits = pygame.sprite.spritecollide(player, home_downstairs_blocks, False)
if hits:
while hits:
print ('Collision!')
y_change = -1*screen_multiplier
player.rect.y += y_change
hits = pygame.sprite.spritecollide(player, home_downstairs_blocks, False)
if place == 0:
hits = pygame.sprite.spritecollide(player, home_upstairs_blocks, False)
if hits:
while hits:
print ('Collision!')
y_change = -1*screen_multiplier
player.rect.y += y_change
hits = pygame.sprite.spritecollide(player, home_upstairs_blocks, False)
sprite = pygame.image.load('player_south_still.png')
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
x_change = 0
elif event.key == pygame.K_RIGHT:
x_change = 0
elif event.key == pygame.K_UP:
y_change = 0
elif event.key == pygame.K_DOWN:
y_change = 0
x += x_change
y += y_change
player.rect.x += x_change
player.rect.y += y_change
#Check for collisions
hits = pygame.sprite.spritecollide(player, nt_blocks, False)
if place == 3:
door_enter = pygame.sprite.spritecollide(inside_house_door, player_group, False)
if door_enter:
print('n')
player.rect.x = 100*screen_multiplier
player.rect.y = 50*screen_multiplier
place = 2
area_load()
elif place == 2:
door_enter = pygame.sprite.spritecollide(player_house_door, player_group, False)
if door_enter:
print('n')
player.rect.x = 70*screen_multiplier
player.rect.y = 110*screen_multiplier
place = 1
area_load()
door_enter = pygame.sprite.spritecollide(extra_door, player_group, False)
if door_enter:
print('n')
player.rect.x = 70*screen_multiplier
player.rect.y = 110*screen_multiplier
place = 3
area_load()
elif place == 1:
door_enter = pygame.sprite.spritecollide(inside_house_door, player_group, False)
if door_enter:
print('n')
player.rect.x = (38*screen_multiplier)
player.rect.y = (80*screen_multiplier)
place = 0
sprite = pygame.image.load('player_east_still.png')
if start_in_bed != 0:
player.rect.x = 130*screen_multiplier
player.rect.y = 30*screen_multiplier
area_load()
door_enter = pygame.sprite.spritecollide(player_house_door, player_group, False)
if door_enter:
print('outside door')
player.rect.x = (38*screen_multiplier)
player.rect.y = (80*screen_multiplier)
place = 2
sprite = pygame.image.load('player_north_still.png')
area_load()
elif place == 0:
door_enter = pygame.sprite.spritecollide(inside_house_door, player_group, False)
if door_enter:
print('n')
place = 1
player.rect.y = 25*screen_multiplier
player.rect.x = 132*screen_multiplier
area_load()
if place == 0:
sprite = pygame.transform.scale(sprite, (10*screen_multiplier*2, 14*screen_multiplier*2))
elif place == 1:
sprite = pygame.transform.scale(sprite, (10*screen_multiplier*2, 14*screen_multiplier*2))
elif place == 3:
sprite = pygame.transform.scale(sprite, (10*screen_multiplier*2, 14*screen_multiplier*2))
else:
sprite = pygame.transform.scale(sprite, (10*screen_multiplier, 14*screen_multiplier))
#Draw everything
screen.blit(player.player_sprite, (player.rect.x, player.rect.y))
screen.blit(background, (0, 0))
#screen.blit(lab_bottom_left_fridges.image, (lab_bottom_left_fridges.rect.x, lab_bottom_left_fridges.rect.y))
sprite.set_colorkey(WHITE)
screen.blit(sprite,(player.rect.x,player.rect.y))
pygame.display.flip()
pygame.quit()
感谢您的帮助!
答
不幸的是,在pygame的keydown事件中,它只显示在按下按键的框架上,并且在释放按键的框架上触发了按键事件。你必须实现自己的逻辑才能使这项工作对你有益。我建议一个简单的布尔开关。 例子:
if event.type == KEYDOWN:
if event.key == K_LEFT:
moveLeft = true
elif event.type == KEYUP:
if event.key == K_LEFT:
moveLeft = false
if moveLeft:
player.rect.x = player.rect.x - 1
使用这种方法,moveLeft将只要左键不放,将不再是真实的,只要它被释放是真的。你可以在任何需要的地方应用相同的逻辑。
答
您还可以选择使用pygame.key.get_pressed()。例如:
key_pressed = pygame.key.get_pressed()
if key_pressed[pygame.K_UP]:
# move something up
if key_pressed[pygame.K_LEFT]:
# move something left
if key_pressed[pygame.K_DOWN]:
# move something down
if key_pressed[pygame.K_RIGHT]:
# move something right
Per the documentation,调用pygame.key.get_pressed()
“则返回表示键盘上的每个键的状态的布尔值的序列”
换句话说,调用这个函数不依赖pygame事件发生(如key_down或key_up)。数组始终知道密钥是关闭还是关闭。
这是太多的代码为*。在将它发布到此处之前,您应该将代码减少到绝对[最小值](http://*.com/help/mcve)。我还建议在http://codereview.stackexchange.com上发布代码,因为有很大的改进潜力。与*相比,您应该将所有代码发布到那里,并且还必须确保其正常工作。看起来你需要一个[有限状态机](https://www.reddit.com/r/pygame/comments/3kghhj/simple_state_machine_example/)在不同的场景之间切换。 – skrx