- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图让我的玩家 rect 在侧面和底部与我的敌人 rect 碰撞,因此玩家 rect 不会抛出敌人的 rect 但我不知道为什么它一直将我传送到它根本不起作用 VIDEo << 正如您在视频中看到的,感谢您的帮助,我只需要我的 rect 在我侧面碰撞时不要 throw 或上升
我的碰撞部分
playerman.isJump = False
collide = False
for platform in platforms:
if playerman.rect.colliderect(platform.rect):
collide = True
playerman.isJump = False
playerman.y = platform.rect.top - playerman.height + 1
if playerman.rect.right > platform.rect.left and playerman.rect.left < platform.rect.left - playerman.width:
playerman.x = platform.rect.left - playerman.width
if playerman.rect.left < platform.rect.right and playerman.rect.right > platform.rect.right + playerman.width:
playerman.x = platform.rect.right
没有图像只有方块你可以测试代码
import pygame,random
window = pygame.display.set_mode((800,800), pygame.NOFRAME)
# player factory
class player:
def __init__(self,x,y,height,width,color):
self.x = x
self.y = y
self.height = height
self.width = width
self.color = color
self.speed = 4
self.fall = 0
self.isJump = False
self.jumpCount = 10
self.rect = pygame.Rect(x,y,height,width)
def draw(self):
self.rect.topleft = (self.x,self.y)
pygame.draw.rect(window,self.color,self.rect)
# player factory
class platform:
def __init__(self,x,y,height,width,color):
self.x = x
self.y = y
self.height = height
self.width = width
self.color = color
self.rect = pygame.Rect(x,y,height,width)
def draw(self):
self.rect.topleft = (self.x,self.y)
pygame.draw.rect(window,self.color,self.rect)
white = (255,255,255)
platform1 = platform(590,590,60,60,white)
platforms = [platform1]
# make the player
pink = (205,105,205)
playerman = player(300,300,60,60,pink)
# the game fps
clock = pygame.time.Clock()
FPS = 30
def keyevents():
if keys[pygame.K_w]:
playerman.y -= playerman.speed
if keys[pygame.K_s]:
playerman.y += playerman.speed
if keys[pygame.K_d]:
playerman.x += playerman.speed
if keys[pygame.K_a]:
playerman.x -= playerman.speed
if keys[pygame.K_SPACE]:
playerman.isJump = True
def jump():
if playerman.isJump:
if playerman.jumpCount >= -10:
neg = 1
if playerman.jumpCount < 0:
neg = -1
playerman.y -= playerman.jumpCount**2 * 0.5 * neg
playerman.jumpCount -= 1
else:
playerman.isJump = False
playerman.jumpCount = 10
collide = False # this makes the player fall down up to
# move the player
if not playerman.isJump:
playerman.y += playerman.fall
playerman.fall += 1
playerman.isJump = False
if playerman.rect.bottom >= 680:
collide = True
playerman.isJump = False
playerman.JumpCount = 10
playerman.y = 680 - playerman.height
if collide:
if keys[pygame.K_SPACE]:
playerman.isJump = True
playerman.fall = 0
# redraw the window
def redraw():
playerman.draw()
platform.draw()
# main loop
runninggame = True
while runninggame:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
runningggame = False
# key event
keys = pygame.key.get_pressed()
# this is the key events
keyevents()
jump() # JUMP AND COLLISION on the grawn
playerman.isJump = False
collide = False
for platform in platforms:
if playerman.rect.colliderect(platform.rect):
collide = True
playerman.isJump = False
playerman.y = platform.rect.top - playerman.height + 1
if playerman.rect.right > platform.rect.left and playerman.rect.left < platform.rect.left - playerman.width:
playerman.x = platform.rect.left - playerman.width
if playerman.rect.left < platform.rect.right and playerman.rect.right > platform.rect.right + playerman.width:
playerman.x = platform.rect.right
window.fill((0,0,0))
redraw()
pygame.display.update()
pygame.quit()
我只是在寻找一个基本的碰撞,我触摸我的敌人左右和底部的矩形,它不会扔掉,如果我在顶部碰撞,它会让我的玩家站在上面
最佳答案
我终于成功解决了!这里有一些代码可以让一切正常工作。如果您愿意,我可以在聊天中向您描述我所做的更改以及他们所做的更改。请注意,我在 Python 3 中对此进行了编程,因此您可能需要对其进行一些更改才能在 Python 2 中运行。
import pygame,random
window = pygame.display.set_mode((800,800), pygame.NOFRAME)
# player factory
class player:
def __init__(self,x,y,height,width,color):
self.x = x
self.y = y
self.height = height
self.width = width
self.moveleft = True
self.moveright = True
self.color = color
self.speed = 4
self.fall = 0
self.isJump = False
self.jumpCount = 10
self.rect = pygame.Rect(x,y,height,width)
def draw(self):
self.rect.topleft = (self.x,self.y)
pygame.draw.rect(window,self.color,self.rect)
# player factory
class platform:
def __init__(self,x,y,height,width,color):
self.x = x
self.y = y
self.height = height
self.width = width
self.color = color
self.rect = pygame.Rect(x,y,height,width)
def draw(self):
self.rect.topleft = (self.x,self.y)
pygame.draw.rect(window,self.color,self.rect)
white = (255,255,255)
platform1 = platform(590,590,60,60,white)
platforms = [platform1]
# make the player
pink = (205,105,205)
playerman = player(300,300,60,60,pink)
# the game fps
clock = pygame.time.Clock()
FPS = 30
def keyevents():
if keys[pygame.K_w]:
playerman.y -= playerman.speed
if keys[pygame.K_s]:
playerman.y += playerman.speed
if keys[pygame.K_d]:
if playerman.moveright:
playerman.x += playerman.speed
if keys[pygame.K_a]:
if playerman.moveleft:
playerman.x -= playerman.speed
if keys[pygame.K_SPACE]:
playerman.isJump = True
def jump():
if playerman.isJump:
if playerman.jumpCount >= -10:
neg = 1
if playerman.jumpCount < 0:
neg = -1
playerman.y -= playerman.jumpCount**2 * 0.5 * neg
playerman.jumpCount -= 1
else:
playerman.isJump = False
playerman.jumpCount = 10
collide = False # this makes the player fall down up to
# move the player
if not playerman.isJump:
playerman.y += playerman.fall
playerman.fall += 1
playerman.isJump = False
if playerman.rect.bottom >= 680:
collide = True
playerman.isJump = False
playerman.JumpCount = 10
playerman.y = 680 - playerman.height
if collide:
if keys[pygame.K_SPACE]:
playerman.isJump = True
playerman.fall = 0
# redraw the window
def redraw():
playerman.draw()
platform.draw()
# main loop
runninggame = True
while runninggame:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
runningggame = False
# key event
keys = pygame.key.get_pressed()
# this is the key events
keyevents()
jump() # JUMP AND COLLISION on the grawn
playerman.isJump = False
collide = False
for platform in platforms:
if playerman.rect.colliderect(platform.rect):
collide = True
playerman.isJump = False
if (platform.rect.collidepoint(playerman.rect.right, playerman.rect.bottom) or
platform.rect.collidepoint(playerman.rect.left, playerman.rect.bottom)):
playerman.y = platform.rect.top - playerman.height + 1
playerman.moveright = True
playerman.moveleft = True
if (platform.rect.collidepoint(playerman.rect.right, playerman.rect.top) or
platform.rect.collidepoint(playerman.rect.right, playerman.rect.bottom - 10)):
playerman.moveright = False
elif (platform.rect.collidepoint(playerman.rect.left, playerman.rect.top) or
platform.rect.collidepoint(playerman.rect.left, playerman.rect.bottom - 10)):
playerman.moveleft = False
else:
playerman.moveright = True
playerman.moveleft = True
window.fill((0,0,0))
redraw()
pygame.display.update()
pygame.quit()
关于python - 我怎样才能让我的玩家 Rect 在我的敌人 Rect 的侧面和底部碰撞?小游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62480974/
现在我正在学习一些 Javascript,但由于我的技能很低,现在遇到了很多问题。我需要一些帮助来解决这段代码中的几个问题。 我正在尝试编写一个名为“打鱼”的游戏。它有一个计时器、分数和 onclic
我知道网上有几个类似的问题,但没有一个真正对我有帮助。我只是想绘制一个网格,并为用户提供点击这些网格单元格的选项。每次用户单击时,单元格的颜色/填充应从黑色变为白色。 我目前正在做的事情如下: BLA
我正在创建一个小游戏,其中一只猫必须捕获一只随机移动的老鼠。代码背后的想法是首先随机移动鼠标,然后检查猫相对于鼠标的位置。该代码对于较小的网格(CatAndMouse(...) 中的第一个输入)工作得
这个问题在这里已经有了答案: Iterating through a Collection, avoiding ConcurrentModificationException when removi
本文实例为大家分享了C语言实现三子棋(井字棋)小游戏的具体代码,供大家参考,具体内容如下 推荐阅读顺序(不建议跳过) 先看实现之后的界面 —— 然后看分析程序要实现的步骤 —— 之后在看翻到te
我想在我正在开发的简单平台游戏中实现实体块,但我不知道如何实现。 我尝试了很多东西,但没有任何效果。 我想做以下事情: 当我跳上去时,我会留在那里 当我跳到它下面时,我不会到达顶部 当我从左侧跳时,我
我正在 WinForms 中制作简单的图形游戏,目前我希望在游戏开始时显示一个菜单。我唯一的问题是我不确定应用程序本身的结构,这是我目前所拥有的: 1) 要有菜单形式和游戏形式。选择新游戏后,创建游戏
我试图让我的玩家 rect 在侧面和底部与我的敌人 rect 碰撞,因此玩家 rect 不会抛出敌人的 rect 但我不知道为什么它一直将我传送到它根本不起作用 VIDEo platform.rec
我是一名优秀的程序员,十分优秀!