我是pygame的新手,我正在努力获取基本知识。我想创建障碍物并检测玩家矩形的哪一侧(表示精灵的碰撞盒)与障碍物矩形碰撞。有了这个,我可以在游戏中创建基本物理。
这就是我所做的:
import pygame
class Player (pygame.sprite.Sprite):
def __init__(self, x=0, y=0, s=100):
super(Player,self).__init__()
self.image = pygame.transform.scale(pygame.image.load("player.png"), (s, s))
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
class Block (pygame.sprite.Sprite):
def __init__(self, x=0, y=500, s=100):
super(Block, self).__init__()
self.image = pygame.transform.scale(pygame.image.load("wall.png"),(s, s))
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
pygame.init()
display = pygame.display.set_mode((800,600))
player = Player()
block = Block()
sprites = pygame.sprite.Group()
living = pygame.sprite.Group()
noliving = pygame.sprite.Group()
sprites.add(player)
sprites.add(block)
living.add(player)
noliving.add(block)
gravity = 1
xCh = 0
yCh = 0
speed = 1
while True:
display.fill((159, 159, 159))
for liv in living:
for noliv in noliving:
if(not pygame.sprite.collide_rect(liv, noliv)):
player.rect.y += gravity
for event in pygame.event.get():
if(event.type == pygame.KEYDOWN):
if(event.key == pygame.K_ESCAPE):
quit()
elif(event.key == pygame.K_a or event.key == pygame.K_LEFT):
xCh = -speed
elif(event.key == pygame.K_d or event.key == pygame.K_RIGHT):
xCh = speed
elif(event.type == pygame.KEYUP):
xCh = 0
yCh = 0
elif(event.type == pygame.QUIT):
quit()
player.rect.x += xCh
sprites.draw(display)
pygame.display.update()
当玩家接触到阻挡时停止下降,但是如果我向左或向右然后进入阻挡,则玩家直接进入阻挡并停止从那里掉落。我希望玩家无法通过该区块。怎么做?
答案 0 :(得分:1)
以下是您的代码,其中包含建议的修复程序以及其他一些改进建议。
首先要做的事情是:错误的是你从未检查水平移动是否会使块碰撞。你只有一张支票,实际上只有在之后发生了任何碰撞。在这种情况下,它会阻止“引力”做任何事情。由于您是逐个像素地更新播放器,因此碰撞已经发生的事实大部分都未被注意到。修复方法是创建一个检查,以验证移动块是否可以移动到某个位置,然后才允许它。并为所有运动做到这一点,不仅仅是重力运动。
所以,在这里,我将解释可以让这个示例代码演变成完整游戏的改进。
消除模块级别的“浮动代码”并将所有内容放入函数中。这对于任何游戏来说都是必不可少的,这些游戏将来甚至可以像起始屏幕一样简单(可能还有“再次播放”屏幕)。我做了两个功能,一个创建设置 您的游戏环境和创建您的对象,以及主要的游戏功能。您应该在将来进一步分离“设置”阶段, 能够创造更多游戏对象或不同的游戏关卡。
重复使用您的代码。您的__init__
方法中的行大多数在您的两个类之间重复,但是对于初始位置和图像名称常量。所以你实际上只需要一个类,并为这些属性传递不同的值。由于你暗示你将通过使用组等两种不同的游戏类型的对象,我保留了这两个类,但是考虑了公共代码。
帧延迟:您的代码将“尽可能快地”移动 - 这将在不同的计算机中创建不同的游戏速度,并使用100%的CPU - 导致可能的机器速度降低和过多的电源使用。我包括一个30ms的硬编码暂停 - 这将允许你的游戏以接近30FPS的速度移动,保持不同设备的速度稳定。需要进行的另一项更改是,您的重力和速度的硬编码“1”值会使玩家一次移动1px,但必须更改为更大的值。
最后但并非最不重要的是,“我可以移动到那里”逻辑:它实际上看起来很广泛 - 但是认为对“这个对象移动到那个位置”的检查的简单英语描述是冗长的。该方法基本上是:“存储当前位置”。 “用给定的x和y位移假定位置”。 “检查我是否与某些东西相撞”。 “如果没有,恢复以前的位置,并说允许移动”。 “否则,恢复y位置,检查X方向是否有碰撞,如果是,则限制水平移动”。 “恢复x位置,将y位置移动所需的量,检查是否发生碰撞。如果是,则限制y方向的移动”。 “如果运动不受x或y的限制,则只是导致碰撞的组合运动:限制两个方向的运动是公平的”。 “恢复原始位置,允许返回x和y位置,以及我们碰撞的对象”。所有这些逻辑都放在“活”对象类本身中,因此它可以随时使用,主游戏逻辑中只有一行代码。
代码:
import pygame
class GameObject(pygame.sprite.Sprite):
def __init__(self, x=0, y=0, s=100, image=""):
super(GameObject, self).__init__()
if not image:
image = self.image
self.image = pygame.transform.scale(pygame.image.load(image), (s, s))
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
class Player(GameObject):
image = "player.png"
def can_move(self, xCh, yCh, group):
old_pos = self.rect.x, self.rect.y
self.rect.x += xCh
self.rect.y += yCh
collided_with = pygame.sprite.spritecollideany(self, group)
if not collided_with:
# No Collisions at all - allow movement
self.rect.x = old_pos[0]
self.rect.y = old_pos[1]
return True, xCh, yCh, None
# Check if the colision was due to horizontal movement:
self.rect.y = old_pos[1]
if pygame.sprite.spritecollideany(self, group):
# Yes, then indicate horizontal movement should be 0
xCh = 0
# check if collision was due to vertical movement:
self.rect.y += yCh
self.rect.x = old_pos[0]
if pygame.sprite.spritecollideany(self, group):
# Yes - indicate vertical movemnt should be 0
yCh = 0
# if only diagonal movement causes collision, then
# cancel movrment in both axes:
# (i.e. just the diagonal movement would hit the
# obstacle )
if not xCh == 0 and not yCh == 0:
xCh = 0
yCh = 0
self.rect.x = old_pos[0]
self.rect.y = old_pos[1]
return False, xCh, yCh, collided_with
class Block(GameObject):
image = "wall.png"
def setup():
global display, player, living, noliving, gravity, speed, sprites
pygame.init()
display = pygame.display.set_mode((800,600))
player = Player()
block = Block(y=500)
sprites = pygame.sprite.Group()
living = pygame.sprite.Group()
noliving = pygame.sprite.Group()
sprites.add(player)
sprites.add(block)
living.add(player)
noliving.add(block)
gravity = 10
speed = 10
def main():
xCh = 0
yCh = 0
while True:
display.fill((159, 159, 159))
for event in pygame.event.get():
if(event.type == pygame.KEYDOWN):
if(event.key == pygame.K_ESCAPE):
quit()
elif(event.key == pygame.K_a or event.key == pygame.K_LEFT):
xCh = -speed
elif(event.key == pygame.K_d or event.key == pygame.K_RIGHT):
xCh = speed
elif(event.type == pygame.KEYUP):
xCh = 0
yCh = 0
elif(event.type == pygame.QUIT):
quit()
yCh = gravity
for liv in living:
if liv == player:
check_x_ch = xCh
else:
check_x_ch = 0
can_move, xCh, yCh, obstacle = liv.can_move(xCh, yCh, noliving)
liv.rect.x += xCh
liv.rect.y += yCh
# Do other actions if "can_move" indicates a block was hit...
sprites.draw(display)
pygame.display.update()
pygame.time.delay(30)
setup()
main()
答案 1 :(得分:0)
最后一个版本是:
import pygame
class GameObj(pygame.sprite.Sprite):
def __init__(self, image, x, y, s):
super(GameObj, self).__init__()
self.image = pygame.transform.scale(pygame.image.load("img/"+ image), (s, s))
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
class Player(GameObj):
leftUnable = False
rightUnable = False
jumpHeight = 200
jumpSpeed = 5
image = "player.png"
inAir = True
def __init__(self, x, y, s=100):
super(Player,self).__init__(self.image, x, y, s)
class Block(GameObj):
image = "wall.png"
def __init__(self, x, y, s=100):
super(Block, self).__init__(self.image, x, y, s)
def collideNum(sprite, group):
total = 0
for member in group:
if(pygame.sprite.collide_rect(sprite, member)):
total += 1
return total
def setup():
pygame.init()
global display, player, block, sprites, living, noliving, clock
display = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Test")
clock = pygame.time.Clock()
player = Player(100, 0)
block = Block(100, 300)
block1 = Block(200, 400)
block2 = Block(400, 400)
sprites = pygame.sprite.Group()
living = pygame.sprite.Group()
noliving = pygame.sprite.Group()
noliving.add(block)
noliving.add(block1)
noliving.add(block2)
living.add(player)
for liv in living:
sprites.add(liv)
for noliv in noliving:
sprites.add(noliv)
main()
def main():
speed = 5
gravity = 5
xCh, yCh = 0, 0
player.leftUnable = False
player.rightUnable = False
while True:
clock.tick(60)
display.fill((184, 184, 184))
yCh = gravity
for event in pygame.event.get():
if(event.type == pygame.QUIT):
quit()
elif(event.type == pygame.KEYDOWN):
if(event.key == pygame.K_ESCAPE):
quit()
elif((event.key == pygame.K_a or event.key == pygame.K_LEFT) and not player.leftUnable):
for noliv in noliving:
if(pygame.sprite.collide_rect(player, noliv)):
if(noliv.rect.left < player.rect.left < noliv.rect.right and player.rect.bottom > noliv.rect.top + 5):
player.leftUnable = True
break
else:
xCh = -speed
player.leftUnable = False
else:
xCh = -speed
player.leftUnable = False
elif((event.key == pygame.K_d or event.key == pygame.K_RIGHT) and not player.rightUnable):
for noliv in noliving:
if(pygame.sprite.collide_rect(player, noliv)):
if(noliv.rect.left < player.rect.right < noliv.rect.right and player.rect.bottom > noliv.rect.top + 5):
player.rightUnable = True
break
else:
xCh = speed
player.rightUnable = False
else:
xCh = speed
player.rightUnable = False
elif(event.key == pygame.K_SPACE or event.key == pygame.K_w or event.key == pygame.K_UP):
oldPos = player.rect.bottom
xCh = 0
if(not player.inAir):
while player.rect.bottom > oldPos - player.jumpHeight:
clock.tick(60)
display.fill((184, 184, 184))
for ev in pygame.event.get():
if(ev.type == pygame.KEYDOWN):
if(ev.key == pygame.K_d or ev.key == pygame.K_RIGHT):
xCh = speed
elif(ev.key == pygame.K_a or ev.key == pygame.K_LEFT):
xCh = -speed
elif(ev.type == pygame.KEYUP):
xCh = 0
player.rect.x += xCh
player.rect.y -= player.jumpSpeed
player.inAir = True
sprites.draw(display)
pygame.display.update()
elif(event.type == pygame.KEYUP):
xCh = 0
for liv in living:
for noliv in noliving:
if(pygame.sprite.collide_rect(liv, noliv)):
liv.inAir = False
break
else:
liv.inAir = True
for noliv in noliving:
if(pygame.sprite.collide_rect(player, noliv)):
if(noliv.rect.left < player.rect.left < noliv.rect.right and player.rect.bottom > noliv.rect.top + 5):
player.leftUnable = True
if(collideNum(player, noliving) == 1):
player.inAir = True
if(xCh < 0):
xCh = 0
elif(noliv.rect.left < player.rect.right < noliv.rect.right and player.rect.bottom > noliv.rect.top + 5):
player.rightUnable = True
if(collideNum(player, noliving) == 1):
player.inAir = True
if(xCh > 0):
xCh = 0
else:
player.leftUnable = False
player.rightUnable = False
else:
player.leftUnable = False
player.rightUnable = False
if(not player.inAir):
yCh = 0
if(player.rect.top > display.get_size()[1]):
setup()
player.rect.x += xCh
player.rect.y += yCh
sprites.draw(display)
pygame.display.update()
setup()
它运作良好。