我的代码有问题。当我开始运行它不会给出任何错误但它只打开黑屏。我是编码BTW的新手。最后,关于地图,我没有在互联网上制作或找到任何地图,所以我把模板'.png'文件看看我的代码是否正常工作。 以下是文件:https://drive.google.com/drive/folders/1t6EAG7cWSTDItHJnqNeHeJrnbT-2E_8B?usp=sharing
import pygame
import random
#import time
pygame.init()
display_width = 800
display_height = 600
gameDisplay = pygame.display.set_mode((display_width,
display_height))
walkDown = [pygame.image.load('wdown1.png'),
pygame.image.load('wdown2.png'),
pygame.image.load('wdown3.png'),
pygame.image.load('wdown4.png')]
walkLeft = [pygame.image.load('wleft1.png'),
pygame.image.load('wleft2.png'),
pygame.image.load('wleft3.png'),
pygame.image.load('wleft4.png')]
walkRight = [pygame.image.load('wright1.png'),
pygame.image.load('wright2.png'),
pygame.image.load('wright3.png'),
pygame.image.load('wright4.png')]
walkUp = [pygame.image.load('wup1.png'),
pygame.image.load('wup2.png'),
pygame.image.load('wup3.png'),
pygame.image.load('wup4.png')]
lookDown = pygame.image.load('ldown.png')
lookLeft = pygame.image.load('lleft.png')
lookRight = pygame.image.load('lright.png')
lookUp = pygame.image.load('lup.png')
map1 = pygame.image.load("map1.png")
map2 = pygame.image.load("map2.png")
map3 = pygame.image.load("map3.png")
map4 = pygame.image.load("map4.png")
map5 = pygame.image.load("map5.png")
white = (255, 255, 255)
grey = (54, 54, 54)
black = (0, 0, 0)
bright_red = (255, 0, 0)
bright_green = (0, 255, 0)
bright_blue = (0, 0, 255)
red = (200, 0, 0)
green = (0, 200, 0)
blue = (0, 0, 200)
dark_red = (150, 0, 0)
dark_green = (0, 150, 0)
dark_blue = (0, 0, 150)
pygame.display.set_caption("Free Walker")
#gameIcon = pygame.image.load("gameIcon.png")
#pygame.display.set_caption(gameIcon)
clock = pygame.time.Clock()
pause = False
walkCount = 0
down = False
left = False
right = False
up = False
def map():
mapList = ("map1", "map2", "map3", "map4", "map5")
gamemap = random.choice(mapList)
def character(a, b, x, y):
global walkCount
gameDisplay.blit(game_map, (a - cameraX, b - cameraY))
if walkCount + 1 >= 12:
walkCount = 0
if down:
gameDisplay.blit(walkDown[walkCount // 3], (x -
cameraX, y - cameraY))
walkCount += 1
if left:
gameDisplay.blit(walkLeft[walkCount // 3], (x -
cameraX, y - cameraY))
walkCount += 1
if right:
gameDisplay.blit(walkRight[walkCount // 3], (x -
cameraX, y - cameraY))
walkCount += 1
if up:
gameDisplay.blit(walkUp[walkCount // 3], (x -
cameraX, y - cameraY))
walkCount += 1
else:
gameDisplay.blit(char, (x - cameraX, y - cameraY))
def quitgame():
pygame.quit()
quit()
def game_loop():
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
quitgame()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
changeY = -10
down = False
left = False
right = False
up = True
if event.key == pygame.K_a:
changeX = -10
down = False
left = True
right = False
up = False
if event.key == pygame.K_s:
changeY = 10
down = True
left = False
right = False
up = False
if event.key == pygame.K_d:
changeX = 10
down = False
left = False
right = True
up = False
if event.type == pygame.KEYUP:
if event.key == pygame.K_w:
changeY = 0
char = lookUp
down = False
left = False
right = False
up = False
if event.key == pygame.K_a:
changeX = 0
char = lookLeft
down = False
left = False
right = False
up = False
if event.key == pygame.K_s:
changeY = 0
char = lookDown
down = False
left = False
right = False
up = False
if event.key == pygame.K_d:
changeX = 0
char = lookRight
down = False
left = False
right = False
up = False
map()
character(0, 0, 400, 300)
pygame.display.update()
clock.tick(12)
game_loop()
pygame.quit()
quit()
答案 0 :(得分:0)
你的缩进是错误的,绘制代码需要在循环中。
def game_loop():
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
quitgame()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
changeY = -10
down = False
left = False
right = False
up = True
if event.key == pygame.K_a:
changeX = -10
down = False
left = True
right = False
up = False
if event.key == pygame.K_s:
changeY = 10
down = True
left = False
right = False
up = False
if event.key == pygame.K_d:
changeX = 10
down = False
left = False
right = True
up = False
if event.type == pygame.KEYUP:
if event.key == pygame.K_w:
changeY = 0
char = lookUp
down = False
left = False
right = False
up = False
if event.key == pygame.K_a:
changeX = 0
char = lookLeft
down = False
left = False
right = False
up = False
if event.key == pygame.K_s:
changeY = 0
char = lookDown
down = False
left = False
right = False
up = False
if event.key == pygame.K_d:
changeX = 0
char = lookRight
down = False
left = False
right = False
up = False
map()
character(0, 0, 400, 300)
pygame.display.flip()
clock.tick(12)
另外,请使用display.flip()