我想使程序正常工作,以便当精灵到达迷宫的出口(-15和+780)时,屏幕切换到数组房间中的下一个迷宫[]。
我试图使用预先存在的self.pos集成位置检查器,尽管这似乎不起作用。我还将4个迷宫添加到名为Rooms的数组中
import pygame
pos = (0,0)
class Player(pygame.sprite.Sprite):
def __init__(self, image, pos, background):
super().__init__()
self.image = image
self.pos = pygame.Vector2(pos)
self.rect = self.image.get_rect(center=self.pos)
self.background = background
def update(self, events, dt):
pressed = pygame.key.get_pressed()
move = pygame.Vector2((0, 0))
if pressed[pygame.K_w]: move += (0, -1)
if pressed[pygame.K_a]: move += (-1, 0)
if pressed[pygame.K_s]: move += (0, 1)
if pressed[pygame.K_d]: move += (1, 0)
#if move.length() > 0: move.normalise_ip()
new_pos = self.pos + move*(dt/5)
new_rect = self.rect.copy()
new_rect.center = new_pos
new_rect.clamp_ip(self.background.get_rect())
new_pos = new_rect.center
hit_box = self.background.subsurface(new_rect)
for x in range(new_rect.width):
for y in range(new_rect.height):
if sum(hit_box.get_at((x, y))) < 500:
return
self.pos = new_pos
self.rect.center = self.pos
global pos
pos = self.pos
# Scenes
def room1():
background = (pygame.image.load('maze1.jpg'))
background = pygame.transform.rotate(background, -90)
background = pygame.transform.scale(background, (800,600))
def room2():
background = (pygame.image.load('maze2.jpg'))
background = pygame.transform.rotate(background, -90)
background = pygame.transform.scale(background, (800,600))
def room3():
background = (pygame.image.load('maze3.jpg'))
background = pygame.transform.rotate(background, -90)
background = pygame.transform.scale(background, (800,600))
def room4():
background = (pygame.image.load('maze4.jpg'))
background = pygame.transform.rotate(background, -90)
background = pygame.transform.scale(background, (800,600))
def main():
pygame.init()
screen = pygame.display.set_mode((800, 600))
screen_rect = screen.get_rect()
clock = pygame.time.Clock()
sprites = pygame.sprite.Group()
rooms = []
room = room1()
rooms.append(room)
room = room2()
rooms.append(room)
room = room3()
rooms.append(room)
room = room4()
rooms.append(room)
current_room_no = 0
current_room = rooms[current_room_no]
dt = 0
while True:
events = pygame.event.get()
for e in events:
if e.type == pygame.QUIT:
return
if pos < -15:
if current_room_no == 0:
current_room_no = 2
current_room = rooms[current_room_no]
pos = 790
elif current_room_no == 2:
current_room_no = 1
current_room = rooms[current_room_no]
pos = 790
else:
current_room_no = 0
current_room = rooms[current_room_no]
pos = 790
if pos > 801:
if current_room_no == 0:
current_room_no = 1
current_room = rooms[current_room_no]
pos = 0
elif current_room_no == 1:
current_room_no = 2
current_room = rooms[current_room_no]
pos = 0
else:
current_room_no = 0
current_room = rooms[current_room_no]
pos = 0
pimg = pygame.Surface((10, 10))
pimg.fill((200, 20, 20))
sprites.add(Player(pimg, (50, 50), background))
sprites.update(events, dt)
screen.fill(pygame.Color('grey'))
screen.blit(background, (0, 0))
sprites.draw(screen)
pygame.display.flip()
dt = clock.tick(60)
if __name__ == '__main__':
main()
我希望有人可以帮助我集成屏幕更改功能,因为当前代码输出有关pos不是变量和/或在需要后调用pos的错误,但是如您所见,我清楚地将pos变量称为代码的开头作为全局变量。
答案 0 :(得分:1)
要使用pos
,需要在global pos
的开头指定main
。通常,使用全局变量不是一个好习惯,请尝试将代码重构为在函数内部包含pos
。我担心您正在尝试将tuple
与int
进行比较。这也会产生错误。
我很随意地更改了代码。
player.pos
来跟踪玩家精灵的位置player
,因为在游戏中发生事件时会多次催生玩家。room
之类的操作时删除了rooms.append(room())
函数,基本上是在附加room
函数的结果,该结果基本上是None
。我不太明白你为什么要这么做完整代码:
import pygame
class Player(pygame.sprite.Sprite):
def __init__(self, image, pos, background):
super().__init__()
self.image = image
self.pos = pygame.Vector2(pos)
self.rect = self.image.get_rect(center=self.pos)
self.background = background
def update(self, events, dt):
pressed = pygame.key.get_pressed()
move = pygame.Vector2((0, 0))
if pressed[pygame.K_w]: move += (0, -1)
if pressed[pygame.K_a]: move += (-1, 0)
if pressed[pygame.K_s]: move += (0, 1)
if pressed[pygame.K_d]: move += (1, 0)
#if move.length() > 0: move.normalise_ip()
new_pos = self.pos + move*(dt/5)
new_rect = self.rect.copy()
new_rect.center = new_pos
new_rect.clamp_ip(self.background.get_rect())
new_pos = new_rect.center
hit_box = self.background.subsurface(new_rect)
for x in range(new_rect.width):
for y in range(new_rect.height):
if sum(hit_box.get_at((x, y))) < 500:
return
self.pos = new_pos
self.rect.center = self.pos
pos = self.pos
# Scenes
def load_background(filename):
background = (pygame.image.load(filename))
background = pygame.transform.rotate(background, -90)
background = pygame.transform.scale(background, (800,600))
return background
def load_player(background):
pimg = pygame.Surface((10, 10))
pimg.fill((200, 20, 20))
return Player(pimg, (25, 325), background)
def main():
pygame.init()
screen = pygame.display.set_mode((800, 600))
screen_rect = screen.get_rect()
clock = pygame.time.Clock()
sprites = pygame.sprite.Group()
mazes = [
"maze1.jpg",
"maze2.jpg",
"maze3.jpg",
"maze4.jpg",
]
mazes_len = len(mazes)
player = None
initialized = False
current_maze = None
dt = 0
while True:
events = pygame.event.get()
for e in events:
if e.type == pygame.QUIT:
return
if not initialized:
current_maze = 0
background = load_background(mazes[current_maze])
player = load_player(background)
sprites.add(player)
initialized = True
player_x = player.pos[0]
player_y = player.pos[1]
if player_x >= 780 and 275 < player_y < 375:
current_maze += 1
# reset to first maze if all mazes were done
if current_maze >= mazes_len:
current_maze = 0
background = load_background(mazes[current_maze])
sprites.empty()
player = load_player(background)
sprites.add(player)
sprites.update(events, dt)
screen.fill(pygame.Color('grey'))
screen.blit(background, (0, 0))
sprites.draw(screen)
pygame.display.flip()
dt = clock.tick(60)
if __name__ == '__main__':
main()