按下向左或向右箭头键时,第四个MOLINE或角色不会移动。我在代码中看不到任何错误。所以我的问题是,我的问题是什么?
import pygame, sys
from pygame.locals import *
pygame.init()
BLACK = ( 0, 0, 0)
ABINO = ( 34, 45, 102)
PINDLER = (255, 123, 90)
MEXON = (200, 200, 200)
size = [700, 500]
D = pygame.display.set_mode(size)
pygame.display.set_caption("Tester of Mester")
def MOLINE1(D, x, y):
pygame.draw.ellipse(D, BLACK, [1 + x, y, 10, 10], 0)
pygame.draw.line(D, BLACK, [5 + x, 17 + y], [10 + x, 27 + y], 2)
pygame.draw.line(D, BLACK, [5 + x, 17 + y], [x, 27 + y], 2)
pygame.draw.line(D, PINDLER, [5 + x, 17 + y], [5 + x, 7 + y], 2)
pygame.draw.line(D, PINDLER, [5 + x, 7 + y], [9 + x, 17 + y], 2)
pygame.draw.line(D, PINDLER, [5 + x, 7 + y], [1 + x, 17 + y], 2)
def MOLINE2(D, x, y):
pygame.draw.ellipse(D, BLACK, [1 + x, y, 10, 10], 0)
pygame.draw.line(D, BLACK, [5 + x, 17 + y], [10 + x, 27 + y], 2)
pygame.draw.line(D, BLACK, [5 + x, 17 + y], [x, 27 + y], 2)
pygame.draw.line(D, PINDLER, [5 + x, 17 + y], [5 + x, 7 + y], 2)
pygame.draw.line(D, PINDLER, [5 + x, 7 + y], [9 + x, 17 + y], 2)
pygame.draw.line(D, PINDLER, [5 + x, 7 + y], [1 + x, 17 + y], 2)
def MOLINE3(D, x, y):
pygame.draw.ellipse(D, BLACK, [1 + x, y, 10, 10], 0)
pygame.draw.line(D, BLACK, [5 + x, 17 + y], [10 + x, 27 + y], 2)
pygame.draw.line(D, BLACK, [5 + x, 17 + y], [x, 27 + y], 2)
pygame.draw.line(D, PINDLER, [5 + x, 17 + y], [5 + x, 7 + y], 2)
pygame.draw.line(D, PINDLER, [5 + x, 7 + y], [9 + x, 17 + y], 2)
pygame.draw.line(D, PINDLER, [5 + x, 7 + y], [1 + x, 17 + y], 2)
def MOLINE4(D, x, y):
pygame.draw.ellipse(D, BLACK, [1 + x, y, 10, 10], 0)
pygame.draw.line(D, BLACK, [5 + x, 17 + y], [10 + x, 27 + y], 2)
pygame.draw.line(D, BLACK, [5 + x, 17 + y], [x, 27 + y], 2)
pygame.draw.line(D, PINDLER, [5 + x, 17 + y], [5 + x, 7 + y], 2)
pygame.draw.line(D, PINDLER, [5 + x, 7 + y], [9 + x, 17 + y], 2)
pygame.draw.line(D, PINDLER, [5 + x, 7 + y], [1 + x, 17 + y], 2)
x_speed = 0
y_speed = 0
x2_speed = 0
y2_speed = 0
x3_speed = 0
y3_speed = 0
x4_speed = 0
y4_speed = 0
x_coord = 300
y_coord = 300
x2_coord = 300
y2_coord = 300
x3_coord = 300
y3_coord = 300
x4_coord = 300
y4_coord = 300
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_speed =- 3
x2_speed = 3
y3_speed =- 3
y4_speed = 3
elif event.key == pygame.K_RIGHT:
x_speed = 3
x2_speed =- 3
y3_speed = 3
y4_speed =- 3
elif event.key == pygame.K_UP:
y_speed =- 3
y2_speed = 3
x3_speed =- 3
x4_speed = 3
elif event.key == pygame.K_DOWN:
y_speed = 3
y2_speed =- 3
x3_speed = 3
x4_speed =- 3
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
x_speed=0
x2_speed=0
y3_speed=0
y4_speed=0
elif event.key == pygame.K_RIGHT:
x_speed=0
x2_speed=0
y3_speed=0
y4_speed=0
elif event.key == pygame.K_UP:
y_speed=0
y2_speed=0
x3_speed=0
x4_speed=0
elif event.key == pygame.K_DOWN:
y_speed=0
y2_speed=0
x3_speed=0
x4_speed=0
x_coord = x_coord + x_speed
y_coord = y_coord + y_speed
x2_coord = x2_coord + x2_speed
y2_coord = y2_coord + y2_speed
x3_coord = x3_coord + x3_speed
y3_coord = y3_coord + y3_speed
x4_coord = x4_coord + x4_speed
y4_coord = x4_coord + x4_speed
D.fill(MEXON)
MOLINE1(D, x_coord, y_coord)
MOLINE2(D, x2_coord, y2_coord)
MOLINE3(D, x3_coord, y3_coord)
MOLINE4(D, x4_coord, y4_coord)
pygame.display.flip()
clock.tick(60)
pygame.quit()
正如您可能看到的那样,它应该可以正常工作,因为代码看起来就像前三个MOLINE字符一样,但我们在这里。我很难过。提前谢谢。
答案 0 :(得分:0)
您指定玩家4的新位置错误。
x4_coord = x4_coord + x4_speed
y4_coord = x4_coord + x4_speed
这应该是
x4_coord = x4_coord + x4_speed
y4_coord = y4_coord + y4_speed