python - Pygame角色没有出现,我如何改变我的背景形状
问题描述
我一直在关注这个 youtube 教程,但自从我更改了角色后,他就没有出现在我的屏幕上。我花了几天时间试图找出原因。我已将我的角色保存在与我的游戏相同的文件夹中,我似乎无法将背景中的第 3 层矩形(浅绿色的)更改为随玩家移动的圆圈。真的很迷茫。谢谢你。
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,0,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,400]],[0.5,[30,40,40,400]],[0.5,[130,90,100,400]],[0.5,[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,80,75),pygame.Rect(0,120,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,(x*16-scroll[0],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)
解决方案
推荐阅读
- java - Android WebView 进度条+滑动刷新
- javascript - 仅使用 javascrip 检查音频文件是否存在
- javascript - Firebase - AngularJS - 通过子值/键/名称获取父级
- c - 在 sudo 中运行程序时将目录的所有权更改为用户
- c# - 应用程序未正确运行 - 启用了自定义 Web 代理
- jenkins - Jenkins 脚本化管道 - switch 语句中的变量更改未反映在管道步骤中
- php - 本地化帖子 ID 导致 Wordpress 错误
- azure-devops - Azure DevOps 多阶段管道错误:找不到具有指定模式的包:/home/vsts/work/1/s/**/*.zip - 如何修复?
- c++ - 关于复制赋值和删除操作符的问题
- html - 还剩 2 个 div 和 1 个个人