我是一名使用python的学生(初学者,只需要作业方面的帮助)。我目前正在编写平台游戏,并且具有以下代码,
def key_pressed(self, key):
if key[pygame.K_LEFT]:
self.set_image(self.player_left, 30, 32)
self.x -= 4
if not self.collides_at(self, 0, 4, 'Block'):
self.gravity = 1
elif not self.collides_at(self, 0, 4, 'Block2'):
self.gravity = 1
elif key[pygame.K_RIGHT]:
self.set_image(self.player_right, 30, 32)
self.x += 4
if not self.collides_at(self, 0, 4, 'Block'):
self.gravity = 1
elif not self.collides_at(self, 0, 4, 'Block2'):
self.gravity = 1
if key[pygame.K_SPACE]:
if self.collides_at(self, 0, 1, 'Block'):
self.y_speed = -15
self.gravity = 1
elif self.collides_at(self, 0, 1, 'Block2'):
self.y_speed = -15
self.gravity = 1
但是,它不适用于两种类型的块(即仅适用于“块”),因此当我的精灵跳到block2平台上时,移动键不再起作用。我可以使用什么代替elif self.collides_at(self,0,1,'Block2'):使精灵能够移动。这是播放器和平台的所有代码。
导入pygame 从GameFrame导入RoomObject,Globals
class Player(RoomObject): def init (自我,房间,x,y): RoomObject。初始化(自我,房间,x,y)
player = self.load_image('monkey.png')
self.player_left = self.load_image('left_monkey.png')
self.player_right = self.load_image('right_monkey.png')
self.set_image(player, 30, 32)
self.depth = 5
self.image_count = 0
self.curr_img = 1
self.gravity = 0
self.handle_key_events = True
# -- Register the objects with which -- #
# -- this object handles collisions -- #
self.register_collision_object('Block')
self.register_collision_object('Goal')
self.register_collision_object('Monster')
self.register_collision_object('Monster2')
self.register_collision_object('MovingBlock')
def step(self):
# - Keep object in the room - #
if self.rect.left <= 0:
self.x = 0
elif self.rect.right >= Globals.SCREEN_WIDTH:
self.x = Globals.SCREEN_WIDTH - self.width
if self.rect.top <= 0:
self.y = 0
elif self.rect.bottom >= Globals.SCREEN_HEIGHT - 60:
self.y = Globals.SCREEN_HEIGHT - 60 - self.height
def handle_collision(self, other):
other_type = type(other).__name__
if other_type == 'Block':
self.blocked()
if self.rect.centery < other.rect.top:
self.y_speed = 0
self.gravity = 0
self.y = other.rect.top - self.height
elif other_type == 'MovingBlock':
self.blocked()
if self.rect.centery < other.rect.top:
self.y_speed = 0
self.gravity = 0
self.y = other.rect.top - self.height
elif other_type == 'Goal':
self.room.running = False
elif other_type == 'Monster' or other_type == 'Monster2':
self.x = 64
self.y = 15*32
self.room.update_lives(-1)
def key_pressed(self, key):
if key[pygame.K_LEFT]:
self.set_image(self.player_left, 30, 32)
self.x -= 4
if not self.collides_at(self, 0, 4, 'Block'):
self.gravity = 1
elif key[pygame.K_RIGHT]:
self.set_image(self.player_right, 30, 32)
self.x += 4
if not self.collides_at(self, 0, 4, 'Block'):
self.gravity = 1
if key[pygame.K_SPACE]:
if self.collides_at(self, 0, 1, 'Block'):
self.y_speed = -15
self.gravity = 1
从GameFrame导入级别,TextObject,全局变量 从对象导入目标,块,玩家,横幅,怪物,Monster2,不可见,移动块
类平台(级别):
def __init__(self, screen):
Level.__init__(self, screen)
# - Set Background image - #
self.set_background_image("wood_background.jpg")
# - Preload images from disk - #
img = self.load_image('invisible_block.png')
img_grnd_flat = self.load_image('centre_log1.png')
img_grnd_left = self.load_image('block.png')
img_grnd_right = self.load_image('right_log1.png')
img_grnd_under = self.load_image('left_log1.png')
# - Set up maze, objects 32x32 25x17 - #
room_objects = [
'_______________________________',
'_______________________________',
'mmmmmmmr_______________________',
'_______________________________',
'____________ummr_______________',
'_____________________umr_______',
'mmmr___________________________',
'_______________________________',
'_____________ummmr__________umm',
'_______________________________',
'i__________________Gumr________',
'_______ummmr___________________',
'_______________________________',
'___umr_________________ummr____',
'__________umr__umr_____________',
'_p____g________________________',
'lllllllllllllllllllllllllllllll'
]
for i, row in enumerate(room_objects):
for j, obj in enumerate(row):
if obj == 'm':
self.add_room_object(Block(self, j*32, i*32, img_grnd_flat))
elif obj == 'j':
self.add_room_object(MovingBlock(self, j*32, i*32, img_grnd_flat))
elif obj == 'l':
self.add_room_object(Block(self, j * 32, i * 32, img_grnd_left))
elif obj == 'r':
self.add_room_object(Block(self, j * 32, i * 32, img_grnd_right))
elif obj == 'u':
self.add_room_object(Block(self, j * 32, i * 32, img_grnd_under))
elif obj == 'i':
self.add_room_object(Invisible(self, j * 32, i * 32, img))
elif obj == 'p':
self.add_room_object(Player(self, j*32, i*32))
elif obj == 'g':
self.add_room_object(Goal(self, j*32, i*32))
elif obj == 'G':
self.add_room_object(Monster2(self, j*32, i*32))
elif obj == 'M':
self.add_room_object(Monster(self, j * 32, i * 32))
# - Add Banner for game info (lives) 800x56 - #
self.add_room_object(Banner(self, 0, 544))
# - Add Text - #
self.score_text = TextObject(self, 20, 560, 'Lives: %i' % Globals.LIVES)
self.score_text.depth = 1000
self.score_text.colour = (255, 255, 255)
self.score_text.update_text()
self.add_room_object(self.score_text)
def update_lives(self, value):
Globals.LIVES += value
if Globals.LIVES == 0:
self.running = False
self.quitting = True
self.score_text.text = 'Lives: %i' % Globals.LIVES
self.score_text.update_text()