我试图在pygame中创建一个下降效果,但我遇到了一个特定的问题。也就是说,当玩家跌倒并与平台发生碰撞时,我的玩家级别开始上下振荡。我确定这与我的更新循环有关,但我不确定它究竟是什么。我已经尝试了几种方法,比如重新安排层次结构无济于事。我最终在网上搜索但没有回答。所以如果有人能帮忙解决这个问题,我真的很感激。
谢谢!
import pygame as pg
import os
#vector
VEC = pg.math.Vector2
def paint(parent, color=None):
""" fills the background for surfaces.
color: for none sprite surfaces optional parameter if given
"""
#set background color based on surface type
if color:
#class based surfaces or sprites
background = pg.Color(color)
else:
#none sprite surfaces usuallly not class
background = pg.Color(parent.background)
#check for image attribure
#if found fill the image's backround
if hasattr(parent, "image"):
parent.image.fill(background)
#if "image" not found fill the surface itself
else:
parent.fill(background)
class Asset(pg.sprite.Sprite):
""" asset class functions as base class for various assets in this case
either a player asset or a block asset group """
#if the given family name(see bellow in constructor) is block,
#all instances of block will be added to this group else its ignored
GROUP = pg.sprite.Group()
def __init__(self, parent=None, family=None, pos=None):
"""
parent:surface asset is being rendered to
family:type of asset(type is not used due to it being a buil-in)
pos: position of asset
"""
super().__init__()
self.parent = parent
self.family = family
self.pos = VEC(pos)
self.size = [20, 20]
#background will be based on family
self.background = {"block":"#000000","player":"#ff0000"}[self.family]
self.image, self.rect = self.set_image()
#see class documention for explanation
if self.family == "block":
Asset.GROUP.add(self)
#if family is player add essential fields for physics
else:
#velocity
self.vel = VEC(3, 3)
#acceleration(x:friction, y:gravity)
self.ac = VEC(.3, .3)
#jump height
self.height = 5
def update(self):
if self.family == "player":
#fall code
self.vel.y += self.ac.y
self.pos.y += self.vel.y
#prevents player from falling of the edge and adds teleportation
if self.pos.x + self.size[0] <= 0:
self.pos.x = 399
elif self.pos.x >= 400:
self.pos.x = 1 - self.size[0]
#updates asset rect postion
self.rect.topleft = self.pos
def render(self):
""" renders image to parent surface """
self.parent.blit(self.image, self.rect)
def set_image(self):
"""creates initial image and rect for sprite"""
self.image = pg.Surface(self.size)
paint(self)
self.rect = self.image.get_rect()
self.rect.topleft = self.pos
return self.image, self.rect
def move(self, key):
"""handles player movmet"""
for i in range(2):
#checks for right or left movment
if key[[pg.K_LEFT, pg.K_RIGHT][i]]:
self.pos.x += self.vel.x*[-1, 1][i]
def jump(self):
""" handles jumping """
self.vel.y = -self.height
def block_collision(player, blocks):
"""collision detection between blocks and player"""
hit = pg.sprite.spritecollide(player, blocks, False)
if hit:
if player.rect.bottom >= hit[0].rect.top:
player.pos.y = hit[0].rect.top - hit[0].rect.height
player.vel.y = 0
def main():
POS = [0, 0]
SIZE = [400, 400]
TITLE = "Test"
BACKGROUND = "#ffffff"
CLOCK = pg.time.Clock()
FPS = 60
RUN = True
os.environ["SDL_VIDEO_CENTERED"] = "1"
win = pg.display.set_mode(SIZE)
pg.display.set_caption(TITLE)
# create blocks group
#NOTE:blocks do not need a variable instance because they are
#automatically added to the class group on construction
for x in range(20):
Asset(family="block", pos=[x*20, 380])
#set player filed
player = Asset(win, family="player", pos=[20, 20])
while RUN:
for event in pg.event.get():
if event.type == pg.QUIT:
RUN = False
elif event.type == pg.KEYDOWN:
if event.key == pg.K_UP:
#player jump
player.jump()
#player movement
player.move(pg.key.get_pressed())
#fill window background
paint(win, BACKGROUND)
#check for collision
block_collision(player, Asset.GROUP)
#update player
player.update()
#update block group
Asset.GROUP.update()
#render player
player.render()
#render block group
Asset.GROUP.draw(win)
pg.display.update()
CLOCK.tick(FPS)
if __name__ == '__main__':
main()
答案 0 :(得分:2)
有两个错误共同产生问题
首先:
player.pos.y = hit[0].rect.top - hit[0].rect.height
没有意义。 top - height
与bottom
相同,因此您有
player.pos.y = hit[0].rect.bottom
但您需要使用hit[0].rect.top
player.rect.bottom
player.rect.bottom = hit[0].rect.top
player.pos.y = player.rect.y
(这里我不知道为什么你使用player.pos
如果它总是与player.rect
具有相同的值,rect
有许多有用的字段,如rect.bottom
, rect.center
等,并在您更改rect.x
,rect.y
等时自动重新计算rect.bottom
,rect.center
。
所以第一次修正是
if hit:
if player.rect.bottom >= hit[0].rect.top:
#player.pos.y = hit[0].rect.top - hit[0].rect.height
player.rect.bottom = hit[0].rect.top
player.pos.y = player.rect.y
player.vel.y = 0
第二
在检查碰撞之前你应该做所有动作
# player movement
player.move(pg.key.get_pressed())
# update player
player.update()
#update block group
Asset.GROUP.update()
# check for collision - after all moves
block_collision(player, Asset.GROUP)
完整代码
import pygame as pg
import os
#vector
VEC = pg.math.Vector2
def paint(parent, color=None):
""" fills the background for surfaces.
color: for none sprite surfaces optional parameter if given
"""
#set background color based on surface type
if color:
#class based surfaces or sprites
background = pg.Color(color)
else:
#none sprite surfaces usuallly not class
background = pg.Color(parent.background)
#check for image attribure
#if found fill the image's backround
if hasattr(parent, "image"):
parent.image.fill(background)
#if "image" not found fill the surface itself
else:
parent.fill(background)
class Asset(pg.sprite.Sprite):
""" asset class functions as base class for various assets in this case
either a player asset or a block asset group """
#if the given family name(see bellow in constructor) is block,
#all instances of block will be added to this group else its ignored
GROUP = pg.sprite.Group()
def __init__(self, parent=None, family=None, pos=None):
"""
parent:surface asset is being rendered to
family:type of asset(type is not used due to it being a buil-in)
pos: position of asset
"""
super().__init__()
self.parent = parent
self.family = family
self.pos = VEC(pos)
self.size = [20, 20]
#background will be based on family
self.background = {"block":"#000000","player":"#ff0000"}[self.family]
self.image, self.rect = self.set_image()
#see class documention for explanation
if self.family == "block":
Asset.GROUP.add(self)
#if family is player add essential fields for physics
else:
#velocity
self.vel = VEC(3, 3)
#acceleration(x:friction, y:gravity)
self.ac = VEC(.3, .3)
#jump height
self.height = 5
def update(self):
if self.family == "player":
#fall code
self.vel.y += self.ac.y
self.pos.y += self.vel.y
#prevents player from falling of the edge and adds teleportation
if self.pos.x + self.size[0] <= 0:
self.pos.x = 399
elif self.pos.x >= 400:
self.pos.x = 1 - self.size[0]
#updates asset rect postion
self.rect.topleft = self.pos
def render(self):
""" renders image to parent surface """
self.parent.blit(self.image, self.rect)
def set_image(self):
"""creates initial image and rect for sprite"""
self.image = pg.Surface(self.size)
paint(self)
self.rect = self.image.get_rect()
self.rect.topleft = self.pos
return self.image, self.rect
def move(self, key):
"""handles player movmet"""
for i in range(2):
#checks for right or left movment
if key[[pg.K_LEFT, pg.K_RIGHT][i]]:
self.pos.x += self.vel.x*[-1, 1][i]
def jump(self):
""" handles jumping """
self.vel.y = -self.height
def block_collision(player, blocks):
"""collision detection between blocks and player"""
hit = pg.sprite.spritecollide(player, blocks, False)
if hit:
if player.rect.bottom >= hit[0].rect.top:
#player.pos.y = hit[0].rect.top - hit[0].rect.height
player.rect.bottom = hit[0].rect.top
player.pos.y = player.rect.y
player.vel.y = 0
def main():
POS = [0, 0]
SIZE = [400, 400]
TITLE = "Test"
BACKGROUND = "#ffffff"
CLOCK = pg.time.Clock()
FPS = 60
RUN = True
os.environ["SDL_VIDEO_CENTERED"] = "1"
win = pg.display.set_mode(SIZE)
pg.display.set_caption(TITLE)
# create blocks group
#NOTE:blocks do not need a variable instance because they are
#automatically added to the class group on construction
for x in range(20):
Asset(family="block", pos=[x*20, 380])
#set player filed
player = Asset(win, family="player", pos=[20, 20])
while RUN:
# --- events ---
for event in pg.event.get():
if event.type == pg.QUIT:
RUN = False
elif event.type == pg.KEYDOWN:
if event.key == pg.K_UP:
#player jump
player.jump()
#player movement
player.move(pg.key.get_pressed())
# --- updates --
#update player
player.update()
#update block group
Asset.GROUP.update()
#check for collision
block_collision(player, Asset.GROUP)
# --- draws ---
#fill window background
paint(win, BACKGROUND)
#render player
player.render()
#render block group
Asset.GROUP.draw(win)
pg.display.update()
CLOCK.tick(FPS)
# ---- end ---
pg.quit()
if __name__ == '__main__':
main()