问题描述
我一直在关注这个 youtube 教程,但自从我更改了角色后,他就没有出现在我的屏幕上。我花了几天时间试图找出原因。我已将我的角色保存在与我的游戏相同的文件夹中,但我似乎无法将背景中的第三层矩形(浅绿色矩形)更改为随玩家移动的圆圈。真的很迷茫。谢谢。
import pygame,sys
clock = pygame.time.Clock()
from pygame.locals import *
pygame.init()
pygame.display.set_caption('ATTACK ON THE BOOKERS')
WINDOW_SIZE = (600,400)
screen = pygame.display.set_mode(WINDOW_SIZE,32)
display = pygame.Surface((300,200))
moving_right = False
moving_left = False
gravity = 0
timer = 0
true_scroll = [0,0]
def load_map(path):
f = open(path + '.txt','r')
data = f.read()
f.close()
data = data.split('\n')
game_map = []
for row in data:
game_map.append(list(row))
return game_map
game_map = load_map('map')
grass_img = pygame.image.load('grass.png')
dirt_img = pygame.image.load('dirt.png')
cloud_img = pygame.image.load('cloud.png')
Mr_Booker = pygame.image.load('Mr Booker.png').convert()
Mr_Booker.set_colorkey((255,255,255))
player_rect = pygame.Rect(100,100,5,13)
background_objects = [[0.25,[120,10,70,400]],[0.25,[280,30,40,[0.5,[30,[130,90,[300,80,120,400]]]
def collision_test(rect,tiles):
hit_list = []
for tile in tiles:
if rect.colliderect(tile):
hit_list.append(tile)
return hit_list
def move(rect,movement,tiles):
collision_types = {'top':False,'bottom':False,'right':False,'left':False}
rect.x += movement[0]
hit_list = collision_test(rect,tiles)
for tile in hit_list:
if movement[0] > 0:
rect.right = tile.left
collision_types['right'] = True
elif movement[0] < 0:
rect.left = tile.right
collision_types['left'] = True
rect.y += movement[1]
hit_list = collision_test(rect,tiles)
for tile in hit_list:
if movement[1] > 0:
rect.bottom = tile.top
collision_types['bottom'] = True
elif movement[1] < 0:
rect.top = tile.bottom
collision_types['top'] = True
return rect,collision_types
while True: # game loop
display.fill((146,244,255))
true_scroll[0] += (player_rect.x-true_scroll[0]-152)/20
true_scroll[1] += (player_rect.y-true_scroll[1]-106)/20
scroll = true_scroll.copy()
scroll[0] = int(scroll[0])
scroll[1] = int(scroll[1])
pygame.draw.rect(display,(7,75),pygame.Rect(0,300,80))
for background_object in background_objects:
obj_rect = pygame.Rect(background_object[1][0]-scroll[0]*background_object[0],background_object[1][1]-scroll[1]*background_object[0],background_object[1][2],background_object[1][3])
if background_object[0] == 0.5:
pygame.draw.rect(display,(14,222,150),obj_rect)
else:
pygame.draw.rect(display,(9,91,85),obj_rect)
tile_rects = []
y = 0
for layer in game_map:
x = 0
for tile in layer:
if tile == '1':
display.blit(dirt_img,(x*16-scroll[0],y*16-scroll[1]))
if tile == '2':
display.blit(grass_img,y*16-scroll[1]))
if tile != '0':
tile_rects.append(pygame.Rect(x*16,y*16,16,16))
x += 1
y += 1
player_movement = [0,0]
if moving_right == True:
player_movement[0] += 2
if moving_left == True:
player_movement[0] -= 2
player_movement[1] += gravity
gravity += 0.2
if gravity > 3:
gravity = 3
player_rect,collisions = move(player_rect,player_movement,tile_rects)
if collisions['bottom'] == True:
timer = 0
gravity = 0
else:
timer += 1
display.blit(Mr_Booker,(player_rect.x - scroll[0],player_rect.y - scroll[1]))
for event in pygame.event.get(): # event loop
if event.type == 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:
if timer < 6:
gravity = -5
if event.type == KEYUP:
if event.key == K_RIGHT:
moving_right = False
if event.key == K_LEFT:
moving_left = False
screen.blit(pygame.transform.scale(display,WINDOW_SIZE),(0,0))
pygame.display.update()
clock.tick(60)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)