Python 2.7 让精灵留在屏幕上?
如何让我的雪碧留在屏幕上和绿色平台上? 精灵一直从屏幕上掉下来,有人能帮忙吗Python 2.7 让精灵留在屏幕上?,python-2.7,pygame,Python 2.7,Pygame,如何让我的雪碧留在屏幕上和绿色平台上? 精灵一直从屏幕上掉下来,有人能帮忙吗 player_image = pygame.image.load("bcquestchar.png") player_image.set_colorkey(WHITE) def draw_background(screen, x, y): pygame.draw.line(screen, ground_GREEN, [0, 400], [700, 400], 200) pygame.draw.line(
player_image = pygame.image.load("bcquestchar.png")
player_image.set_colorkey(WHITE)
def draw_background(screen, x, y):
pygame.draw.line(screen, ground_GREEN, [0, 400], [700, 400], 200)
pygame.draw.line(screen, sky_BLUE, [0,0], [700,0], 400)
pygame.draw.line(screen, sky_WHITE, [0, 270], [700, 270], 150)
#jumping player definition
class Player(pygame.sprite.Sprite):
def __init__(self):
self.playing = False
self.color = BLUE
self.x = 50
self.y = 210
self.goalY= 450
self.gameover = False
def jump(self):
self.goalY -= 45
def draw(self, screen):
screen.blit(player_image, [self.x, self.y])
#create player
player = Player()
只需做另一个函数,以确保当您的精灵底部触摸屏幕底部时,它会停止在那里,等待任何其他命令。例如:
if self.x >= 1000:
self.x = 1000
上述代码意味着如果x
位置大于或等于1000,则将x
设置为1000,使其不会再降低。数字也可以根据您的需要进行更改。您还需要调用update
函数,因为我们需要它来工作:
def update(self): #Put the function into your Player class
if self.x >= 1000:
self.x = 1000
#Put the following the while loop or just call it
player = Player()
player.update() #Call update()
只需做另一个函数,以确保当您的精灵底部触摸屏幕底部时,它会停止在那里,等待任何其他命令。例如:
if self.x >= 1000:
self.x = 1000
上述代码意味着如果x
位置大于或等于1000,则将x
设置为1000,使其不会再降低。数字也可以根据您的需要进行更改。您还需要调用update
函数,因为我们需要它来工作:
def update(self): #Put the function into your Player class
if self.x >= 1000:
self.x = 1000
#Put the following the while loop or just call it
player = Player()
player.update() #Call update()
移动播放器后,必须检查其位置并进行校正 您应该使用
pygame.Rect
,因为它很有用
它不是完整的代码,也不使用rect
方法来检查冲突,但它显示了如何启动冲突
# jumping player definition
class Player(pygame.sprite.Sprite):
def __init__(self):
self.image = pygame.image.load("bcquestchar.png")
self.image.set_colorkey(WHITE)
self.playing = False
self.color = BLUE
# use rect to keep size and position
self.rect = self.image.get_rect()
self.rect.x = 50
self.rect.y = 210
self.goalY= 450
self.gameover = False
#self.speed_x = 0
#self.speed_y = 0
#self.gravity = 1
def jump(self):
self.goalY -= 45
def draw(self, screen):
screen.blit(self.image, self.rect)
#def event_handler(self, event):
# # if pressed key then change speed
def update(self):
# move player
#self.rect.x += self.speed_x
#self.rect.y += self.speed_y
#self.speed_y += self.gravity
# check collision with grass
if self.rect.bottom > grass_rect.top:
# player below grass then move it up
self.rect.bottom = grass_rect.top:
# ---
# draw other objects
def draw_background(screen, x, y):
pygame.draw.line(screen, ground_GREEN, grass_rect, 200)
# ---
# create player
player = Player()
# create object rect to draw it and check collision with player
grass_rect = pygame.Rect(0, 400, 700, 400)
# move player and check collision with objects
player.update()
你有playerrect
和grassrect
-你可以比较player底部和grasstop
请参阅“源代码中的更多信息移动播放机后,您必须检查其位置并更正它 您应该使用
pygame.Rect
,因为它很有用
它不是完整的代码,也不使用rect
方法来检查冲突,但它显示了如何启动冲突
# jumping player definition
class Player(pygame.sprite.Sprite):
def __init__(self):
self.image = pygame.image.load("bcquestchar.png")
self.image.set_colorkey(WHITE)
self.playing = False
self.color = BLUE
# use rect to keep size and position
self.rect = self.image.get_rect()
self.rect.x = 50
self.rect.y = 210
self.goalY= 450
self.gameover = False
#self.speed_x = 0
#self.speed_y = 0
#self.gravity = 1
def jump(self):
self.goalY -= 45
def draw(self, screen):
screen.blit(self.image, self.rect)
#def event_handler(self, event):
# # if pressed key then change speed
def update(self):
# move player
#self.rect.x += self.speed_x
#self.rect.y += self.speed_y
#self.speed_y += self.gravity
# check collision with grass
if self.rect.bottom > grass_rect.top:
# player below grass then move it up
self.rect.bottom = grass_rect.top:
# ---
# draw other objects
def draw_background(screen, x, y):
pygame.draw.line(screen, ground_GREEN, grass_rect, 200)
# ---
# create player
player = Player()
# create object rect to draw it and check collision with player
grass_rect = pygame.Rect(0, 400, 700, 400)
# move player and check collision with objects
player.update()
你有playerrect
和grassrect
-你可以比较player底部和grasstop
请参阅“的源代码中的更多信息请参阅“Platformer示例”,第一次使用每个元素以保持其位置和大小,然后您可以使用pygame中内置的“碰撞检测”功能。Rect请参阅“Platformer示例”,第一次使用每个元素以保持其位置和大小,然后您可以使用“碰撞检测”函数内置
pygame.Rect
是否将函数添加到精灵中?如果是这样的话,我试过了,但不幸的是,它似乎不起作用:-(屏幕的大小是700x400,所以我做了一个功能,如果y位置超过400,它被设置为400,不能再低了,但它似乎没有work@MichaelNguyen移动玩家后,你必须检查玩家的位置。在你的问题中,移动玩家的位置没有代码-因此答案再精确不过了。我是否要将该函数添加到雪碧?如果是的话,我试过了,但不幸的是,它似乎不起作用:-(屏幕的大小是700x400,所以我做了一个功能,如果y位置超过400,它被设置为400,不能再低了,但它似乎没有work@MichaelNguyen移动球员后,你必须检查球员的位置。在你的问题中,并没有移动球员的代码,所以答案再精确不过了。