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

有谁知道我如何在pygame中让我的精灵运动

如何解决有谁知道我如何在pygame中让我的精灵运动

我知道如何左右移动,但是有人知道如何在pygame中上下吗 我正在使用pygame locals,pygame,sys,glob。任何帮助将不胜感激,大多数代码基于:https://pythonprogramming.altervista.org/platform-game-in-detail-part-1/?doing_wp_cron=1603309265.4902870655059814453125

from pygame.locals import *
import pygame
import sys
import glob
map1 = """wwwwwwwwwwwwwwwwwwwwwwwwwwwww
w                           w
w                           w
w                           w
w                           w
w                           
w                           d
w            p               
w                           
w                           w
w                           w
w                           w
w                           w
w                           w
w                           w
wwwwwwwwwwwwwwwwwwwwwwwwwwwww"""
pygame.display.set_icon(pygame.image.load("C:/Users/cuerv/Downloads/flappy-bird-assets-master/flappy-bird-assets-master/favicon.ico"))
pygame.display.set_caption("Knock Knight")

screen = pygame.display.set_mode((480,250))
moving_right = False
moving_left = False
moving_up = False
moving_down = False
player_location = [50,50]#remember its a fucking list

#-----------------------------

door = pygame.image.load("C:/Users/cuerv/Downloads/Door.png")
door_rect = door.get_rect(center=(100,250))

tile = pygame.image.load("C:/Users/cuerv/Downloads/Wall.png")
tile_rect = tile.get_rect(center=(100,256))

player = pygame.image.load("C:/Users/cuerv/Downloads/Player.png").convert()
player_rect = player.get_rect(center=(100,256))



def init_display():
    global screen,tile,door,player


def tiles(map1):
    global tile,player
    for y,line in enumerate(map1):
        #counts lines
        for x,c in enumerate(line):
            #counts caracters
            if c == "w":
                #caracter is w
                screen.blit(tile,(x * 16.18,y * 15))
            if c == "d":
                screen.blit(door,(x * 16.2,y * 15))
            if c == "p":
                screen.blit(player,player_location)


map1 = map1.splitlines()
pygame.init()
init_display()
clock = pygame.time.Clock()
while True:
    tiles(map1)
    if moving_right == True:
        player_location[0] += 4
    if moving_left == True:
        player_location[0] -= 4
    if moving_up == True:
        player_location[0] +=8

    for event in pygame.event.get():
        if event.type == 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





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

解决方法

玩家的位置由 2 个坐标 (x,y) 定义。对于上下运动,您必须更改第二 (y) 坐标(例如 player_location[1] -= 4):

while True:
    tiles(map1)

    if moving_right == True:
        player_location[0] += 4
    if moving_left == True:
        player_location[0] -= 4

    if moving_up == True:
        player_location[1] -= 4
    if moving_down == True:
        player_location[1] += 4

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