微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么即使计算机识别出我的播放器精灵也没有移动?

如何解决为什么即使计算机识别出我的播放器精灵也没有移动?

由于某种原因,即使我将变量设置为全局变量,即使没有出现错误,player1 精灵也没有移动。我已经检查了几次,但我看不到错误在哪里,所以如果有人帮助我,我会非常满意。我正在使用 pygame、sys 和 os 库。抱歉代码混乱。

代码

from GGD import *

screen = pygame.display.set_mode((226,318))
player_pos=[100,50]

pygame.display.set_icon(pygame.image.load('Sprites/Icon.png'))
pygame.display.set_caption("Knock Knight")
player1 = pygame.image.load('Sprites/Player.png')
player_rect = pygame.Rect(player_pos[0],player_pos[1],player1.get_width(),player1.get_height())

spriteGlobals()
clock = pygame.time.Clock()
while True:
    screen.fill((0,0))

    screen.blit(player1,player_pos)

    spriteMovement(player1,player_pos)

    pygame.display.update()
    clock.tick(60)

pygame.quit()

GGD:

from pygame.locals import *
import pygame
import sys
import os

print('Loading[*]')

pygame.init()

moving_right = False
moving_left = False
moving_up = False
moving_down = False

def spriteGlobals():
    global moving_up,moving_down,moving_left,moving_right

def spriteMovement(player1,player_pos=[0,0]):
    global moving_up,moving_right

    if moving_right == True:
        player_pos[0] += 4
    if moving_left == True:
        player_pos[0] -= 4
    if moving_up == True:
        player_pos[1] -=4
    if moving_down == True:
        player_pos[1] +=4

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

        if event.type == KEYDOWN:
        if event.key == K_RIGHT:
            moving_right = True
        if event.key == K_LEFT:
            moving_left = True
        if event.key == K_UP:
            moving_up = True
        if event.key == K_DOWN:
            moving_down = True
    if event.type == KEYUP:
        if event.key == K_RIGHT:
            moving_right = False
        if event.key == K_LEFT:
            moving_left = False
        if event.key == K_UP:
            moving_up = False
        if event.key == K_DOWN:
            moving_down = False

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。