【发布时间】:2020-09-04 12:46:33
【问题描述】:
我目前正在尝试使用滚动相机在 python 中制作 2d 平台游戏。最初我只使用 rect 样式的对象制作这个游戏,但与仅使用 sprite 相比,这使得碰撞非常困难。
所以现在我正在为敌人和主要玩家使用精灵重写这个游戏,但我无法弄清楚为什么滚动功能将不再起作用。这似乎是一个扩展问题,但我不知道。
当我关闭滚动时,游戏运行正常。
谢谢你们,如果你看到任何跳出来的东西,请告诉我。 抱歉代码有点乱,我还在习惯整个 pygame 布局。
import pygame, sys
clock = pygame.time.Clock()
from pygame.locals import *
pygame.init() # initiates pygame
pygame.display.set_caption('Pygame Platformer')
WINDOW_SIZE = (1200,800)
screen = pygame.display.set_mode(WINDOW_SIZE,0,32) # initiate the window
display = pygame.Surface((150,100)) # used as the surface for rendering, which is scaled
moving_right = False
moving_left = False
vertical_momentum = 0
air_timer = 0
scroll = [0, 0]
class Player(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((8, 8))
self.image.fill((255, 0, 0))
self.rect = self.image.get_rect()
self.rect.centerx = 100
self.rect.bottom = 50
self.isJump = False
self.jumpCount = 5
def update(self):
self.speedx = 0
self.rect.x += self.speedx
class Mob(pygame.sprite.Sprite):
def __init__(self,x,y):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((10, 10))
self.image.fill((255, 255, 0))
self.rect = self.image.get_rect()
self.rect.centerx = y
self.rect.bottom = x
self.speedx = 0
self.speedy = 0
def update(self):
self.speedx = 0
self.speedy = 0
keystate = pygame.key.get_pressed()
if keystate[pygame.K_LEFT]:
self.speedx = -10
if keystate[pygame.K_RIGHT]:
self.speedx = 10
if keystate[pygame.K_SPACE]:
self.speedy = -30
self.rect.x += 0
self.rect.y += 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
#ame_map = load_map('data/pictures/map')
game_map = [['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'],
['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'],
['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'],
['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'],
['0','0','0','0','0','0','0','2','2','2','2','2','0','0','0','0','0','0','0'],
['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'],
['2','2','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','2','2'],
['1','1','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','1','1'],
['1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1'],
['1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1'],
['1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1'],
['1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1'],
['1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1']]
grass_img = pygame.image.load('data/pictures/Purple_grass.png')
dirt_img = pygame.image.load('data/pictures/purple_tile.png')
player = Player()
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
all_sprites = pygame.sprite.Group()
mobs = []
mob1 = Mob(10,10)
mobs.append(mob1)
all_sprites.add(player,mobs)
while True: # game loop
display.fill((146,244,255)) # clear screen by filling it with blue
#scroll
scroll[0] += (player.rect.x - scroll[0] -100 ) / 10
scroll[1] += (player.rect.y - scroll[1] -52) / 10
all_sprites.update()
tile_rects = []
y = 0
for layer in game_map:
x = 0
for tile in layer:
if tile == '1':
display.blit(dirt_img, (x * 8 - int(scroll[0]), y * 8 - int(scroll[1])))
if tile == '2':
display.blit(grass_img, (x * 8 - int(scroll[0]), y * 8 - int(scroll[1])))
if tile != '0':
tile_rects.append(pygame.Rect(x * 8, y * 8, 8, 8))
x += 1
y += 1
player_movement = [0,0]
if moving_right == True:
player_movement[0] += 1
if moving_left == True:
player_movement[0] -= 1
player_movement[1] += vertical_momentum
vertical_momentum += 0.3
if vertical_momentum > 3:
vertical_momentum = 3
player.rect,collisions = move(player.rect,player_movement,tile_rects)
if collisions['bottom'] == True:
air_timer = 0
player.jumpCount = 0
else:
air_timer += 1
for mob in mobs:
if pygame.sprite.collide_rect(player,mob):
#mob.image.fill((255,0,255))
mobs.pop(mobs.index(mob))
print("collide")
all_sprites.remove(mob)
else:
mob.image.fill((255,255,0))
all_sprites.draw(display)
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_SPACE:
if air_timer < 6:
vertical_momentum = -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)
【问题讨论】:
-
我需要map.txt文件来运行游戏。
-
对不起,我编辑了一个帖子,所以有一个基本的集成地图
-
if tile != '0':- 当你附加瓷砖时,你应该调整瓷砖的矩形滚动:tile_rects.append(pygame.Rect(x * 8 - int(scroll[0]), y * 8 - int(scroll[1]), 8, 8)) -
我刚刚尝试过,我遇到了一些疯狂的碰撞错误,它把我扔到屏幕周围并穿过墙壁。我不确定滚动会如何影响我的碰撞,但我猜它是可能的。
-
如果出现问题,请使用
print()查看不同位置的变量值 - 然后您可以检查它们是否正确。它被称为print debuging