如何在 pygame 中提供一组预定义指令
How to provide set of predefined instructions in pygame
我是 pygame 的新手,不是普通的编码员。我正在尝试编写一个 pygame 代码,该代码接受来自文本框的指令集,然后相应地移动图像。例如:当您执行代码时,pygame window 将打开一个文本框和主图像。首先,用户将提供一组方向 LEFT、RIGHT、UP、DOWN。在主图像之后应该向左 > 右 > 向上 > 向下移动。
下面是我的尝试,但这里的图像直接到达最后一步,而不是一个接一个地移动。
我希望图像移动 A - B - C - D 而不是直接移动 A - D。任何帮助将不胜感激。
import pygame
pygame.init()
# game screen dimensions
screen_width = 1200
screen_height = 800
# Define colors for using it in code
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
dark_red = (138,0,0)
green = (0,128,0)
dark_green = (0,200,0)
silver = (192,192,192)
display_mygame = pygame.display.set_mode((screen_width,screen_height))
# Primary Image
my_img = pygame.image.load('kid.png')
def game_loop():
x = (screen_width * 0.25)
y = (screen_height * 0.8)
font = pygame.font.Font(None, 32)
clock = pygame.time.Clock()
input_box = pygame.Rect(100, 100, 240, 62)
color_inactive = pygame.Color('lightskyblue3')
color_active = pygame.Color('dodgerblue2')
color = color_inactive
active = False
text = ''
list = []
count = 0
start_width = 50
start_height = 50
sy = 0
crashed = False
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
display_mygame.fill(white)
if event.type == pygame.MOUSEBUTTONDOWN:
if input_box.collidepoint(event.pos):
active = not active
else:
active = False
if event.type == pygame.KEYDOWN:
if active:
if event.key == pygame.K_RETURN:
# Capturing the 4 instructions provided
list.append(str(text))
text = ""
count += 1
# Want the primary image to act accordingly to the instructions
if int(count) == 4:
for text in list:
if text == 'left':
x += -300
elif text == 'right':
x += 450
elif text == 'up':
y += -300
elif text == 'down':
y += 300
elif event.key == pygame.K_BACKSPACE:
text = text[:-1]
else:
text += event.unicode
display_mygame.blit(my_img, (x,y))
txt_surface = font.render(text, True, color)
width = max(200, txt_surface.get_width()+10)
input_box.w = width
display_mygame.blit(txt_surface, (input_box.x+5, input_box.y+5))
pygame.draw.rect(display_mygame, color, input_box, 2)
pygame.display.update()
clock.tick(60)
game_loop()
pygame.quit()
quit()
请不要命名变量 list
。例如使用名称 move_list
而不是 list
.
添加 2 个新状态,walking
和 move
:
walking = False
move = [0, 0]
如果walking == True
,则不接受任何输入。输入数为4时设置walking = True
:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if not walking:
if event.type == pygame.MOUSEBUTTONDOWN:
if input_box.collidepoint(event.pos):
active = not active
else:
active = False
if event.type == pygame.KEYDOWN:
if active:
if event.key == pygame.K_RETURN:
# Capturing the 4 instructions provided
move_list.append(str(text))
text = ""
count += 1
walking = count == 4
elif event.key == pygame.K_BACKSPACE:
text = text[:-1]
else:
text += event.unicode
如果 walking == True
,则在主应用程序循环中移动播放器。从 move_list
中删除第一个元素并相应地设置移动方向 move
。如果设置了move
,则每帧将玩家移动1并适当减少move
:
if walking:
if move[0] != 0 or move[1] != 0:
if move[0] < 0:
x -= 1
move[0] += 1
if move[0] > 0:
x += 1
move[0] -= 1
if move[1] < 0:
y -= 1
move[1] += 1
if move[1] > 0:
y += 1
move[1] -= 1
elif len(move_list) > 0:
if move_list[0] == 'left':
move = [-300, 0]
elif move_list[0] == 'right':
move = [300, 0]
elif move_list[0] == 'up':
move = [0, -300]
elif move_list[0] == 'down':
move = [0, 300]
del move_list[0]
else:
walking = False
count = 0
看例子:
import pygame
pygame.init()
# game screen dimensions
screen_width = 1200
screen_height = 800
# Define colors for using it in code
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
dark_red = (138,0,0)
green = (0,128,0)
dark_green = (0,200,0)
silver = (192,192,192)
display_mygame = pygame.display.set_mode((screen_width,screen_height))
# Primary Image
#my_img = pygame.image.load('kid.png')
my_img = pygame.Surface((20, 20))
my_img.fill((0, 255, 0))
def game_loop():
x = (screen_width * 0.25)
y = (screen_height * 0.8)
font = pygame.font.Font(None, 32)
clock = pygame.time.Clock()
input_box = pygame.Rect(100, 100, 240, 62)
color_inactive = pygame.Color('lightskyblue3')
color_active = pygame.Color('dodgerblue2')
color = color_inactive
active = False
text = ''
move_list = []
count = 0
start_width = 50
start_height = 50
sy = 0
crashed = False
walking = False
move = [0, 0]
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if not walking:
if event.type == pygame.MOUSEBUTTONDOWN:
if input_box.collidepoint(event.pos):
active = not active
else:
active = False
if event.type == pygame.KEYDOWN:
if active:
if event.key == pygame.K_RETURN:
# Capturing the 4 instructions provided
move_list.append(str(text))
text = ""
count += 1
walking = count == 4
elif event.key == pygame.K_BACKSPACE:
text = text[:-1]
else:
text += event.unicode
if walking:
if move[0] != 0 or move[1] != 0:
if move[0] < 0:
x -= 1
move[0] += 1
if move[0] > 0:
x += 1
move[0] -= 1
if move[1] < 0:
y -= 1
move[1] += 1
if move[1] > 0:
y += 1
move[1] -= 1
elif len(move_list) > 0:
if move_list[0] == 'left':
move = [-300, 0]
elif move_list[0] == 'right':
move = [300, 0]
elif move_list[0] == 'up':
move = [0, -300]
elif move_list[0] == 'down':
move = [0, 300]
del move_list[0]
else:
walking = False
count = 0
display_mygame.fill(white)
display_mygame.blit(my_img, (x,y))
txt_surface = font.render(text, True, color)
width = max(200, txt_surface.get_width()+10)
input_box.w = width
display_mygame.blit(txt_surface, (input_box.x+5, input_box.y+5))
pygame.draw.rect(display_mygame, color, input_box, 2)
pygame.display.update()
clock.tick(60)
game_loop()
pygame.quit()
quit()
我是 pygame 的新手,不是普通的编码员。我正在尝试编写一个 pygame 代码,该代码接受来自文本框的指令集,然后相应地移动图像。例如:当您执行代码时,pygame window 将打开一个文本框和主图像。首先,用户将提供一组方向 LEFT、RIGHT、UP、DOWN。在主图像之后应该向左 > 右 > 向上 > 向下移动。
下面是我的尝试,但这里的图像直接到达最后一步,而不是一个接一个地移动。
我希望图像移动 A - B - C - D 而不是直接移动 A - D。任何帮助将不胜感激。
import pygame
pygame.init()
# game screen dimensions
screen_width = 1200
screen_height = 800
# Define colors for using it in code
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
dark_red = (138,0,0)
green = (0,128,0)
dark_green = (0,200,0)
silver = (192,192,192)
display_mygame = pygame.display.set_mode((screen_width,screen_height))
# Primary Image
my_img = pygame.image.load('kid.png')
def game_loop():
x = (screen_width * 0.25)
y = (screen_height * 0.8)
font = pygame.font.Font(None, 32)
clock = pygame.time.Clock()
input_box = pygame.Rect(100, 100, 240, 62)
color_inactive = pygame.Color('lightskyblue3')
color_active = pygame.Color('dodgerblue2')
color = color_inactive
active = False
text = ''
list = []
count = 0
start_width = 50
start_height = 50
sy = 0
crashed = False
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
display_mygame.fill(white)
if event.type == pygame.MOUSEBUTTONDOWN:
if input_box.collidepoint(event.pos):
active = not active
else:
active = False
if event.type == pygame.KEYDOWN:
if active:
if event.key == pygame.K_RETURN:
# Capturing the 4 instructions provided
list.append(str(text))
text = ""
count += 1
# Want the primary image to act accordingly to the instructions
if int(count) == 4:
for text in list:
if text == 'left':
x += -300
elif text == 'right':
x += 450
elif text == 'up':
y += -300
elif text == 'down':
y += 300
elif event.key == pygame.K_BACKSPACE:
text = text[:-1]
else:
text += event.unicode
display_mygame.blit(my_img, (x,y))
txt_surface = font.render(text, True, color)
width = max(200, txt_surface.get_width()+10)
input_box.w = width
display_mygame.blit(txt_surface, (input_box.x+5, input_box.y+5))
pygame.draw.rect(display_mygame, color, input_box, 2)
pygame.display.update()
clock.tick(60)
game_loop()
pygame.quit()
quit()
请不要命名变量 list
。例如使用名称 move_list
而不是 list
.
添加 2 个新状态,walking
和 move
:
walking = False
move = [0, 0]
如果walking == True
,则不接受任何输入。输入数为4时设置walking = True
:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if not walking:
if event.type == pygame.MOUSEBUTTONDOWN:
if input_box.collidepoint(event.pos):
active = not active
else:
active = False
if event.type == pygame.KEYDOWN:
if active:
if event.key == pygame.K_RETURN:
# Capturing the 4 instructions provided
move_list.append(str(text))
text = ""
count += 1
walking = count == 4
elif event.key == pygame.K_BACKSPACE:
text = text[:-1]
else:
text += event.unicode
如果 walking == True
,则在主应用程序循环中移动播放器。从 move_list
中删除第一个元素并相应地设置移动方向 move
。如果设置了move
,则每帧将玩家移动1并适当减少move
:
if walking:
if move[0] != 0 or move[1] != 0:
if move[0] < 0:
x -= 1
move[0] += 1
if move[0] > 0:
x += 1
move[0] -= 1
if move[1] < 0:
y -= 1
move[1] += 1
if move[1] > 0:
y += 1
move[1] -= 1
elif len(move_list) > 0:
if move_list[0] == 'left':
move = [-300, 0]
elif move_list[0] == 'right':
move = [300, 0]
elif move_list[0] == 'up':
move = [0, -300]
elif move_list[0] == 'down':
move = [0, 300]
del move_list[0]
else:
walking = False
count = 0
看例子:
import pygame
pygame.init()
# game screen dimensions
screen_width = 1200
screen_height = 800
# Define colors for using it in code
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
dark_red = (138,0,0)
green = (0,128,0)
dark_green = (0,200,0)
silver = (192,192,192)
display_mygame = pygame.display.set_mode((screen_width,screen_height))
# Primary Image
#my_img = pygame.image.load('kid.png')
my_img = pygame.Surface((20, 20))
my_img.fill((0, 255, 0))
def game_loop():
x = (screen_width * 0.25)
y = (screen_height * 0.8)
font = pygame.font.Font(None, 32)
clock = pygame.time.Clock()
input_box = pygame.Rect(100, 100, 240, 62)
color_inactive = pygame.Color('lightskyblue3')
color_active = pygame.Color('dodgerblue2')
color = color_inactive
active = False
text = ''
move_list = []
count = 0
start_width = 50
start_height = 50
sy = 0
crashed = False
walking = False
move = [0, 0]
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if not walking:
if event.type == pygame.MOUSEBUTTONDOWN:
if input_box.collidepoint(event.pos):
active = not active
else:
active = False
if event.type == pygame.KEYDOWN:
if active:
if event.key == pygame.K_RETURN:
# Capturing the 4 instructions provided
move_list.append(str(text))
text = ""
count += 1
walking = count == 4
elif event.key == pygame.K_BACKSPACE:
text = text[:-1]
else:
text += event.unicode
if walking:
if move[0] != 0 or move[1] != 0:
if move[0] < 0:
x -= 1
move[0] += 1
if move[0] > 0:
x += 1
move[0] -= 1
if move[1] < 0:
y -= 1
move[1] += 1
if move[1] > 0:
y += 1
move[1] -= 1
elif len(move_list) > 0:
if move_list[0] == 'left':
move = [-300, 0]
elif move_list[0] == 'right':
move = [300, 0]
elif move_list[0] == 'up':
move = [0, -300]
elif move_list[0] == 'down':
move = [0, 300]
del move_list[0]
else:
walking = False
count = 0
display_mygame.fill(white)
display_mygame.blit(my_img, (x,y))
txt_surface = font.render(text, True, color)
width = max(200, txt_surface.get_width()+10)
input_box.w = width
display_mygame.blit(txt_surface, (input_box.x+5, input_box.y+5))
pygame.draw.rect(display_mygame, color, input_box, 2)
pygame.display.update()
clock.tick(60)
game_loop()
pygame.quit()
quit()