Python 我如何修复碰撞,使我能够站在箱子上而不会从箱子上摔下来
我想做的是让我的碰撞检测允许我跳到一个正方形上,但它似乎不起作用。这是主循环的最底部Python 我如何修复碰撞,使我能够站在箱子上而不会从箱子上摔下来,python,pygame,Python,Pygame,我想做的是让我的碰撞检测允许我跳到一个正方形上,但它似乎不起作用。这是主循环的最底部 # --- COLLISION is at the bottom of main loop # ------ # this is a pygame module that I imported import pygame pygame.init() # this is just my screen I created win defines it win = pygame.display.set_mode((
# --- COLLISION is at the bottom of main loop
# ------
# this is a pygame module that I imported
import pygame
pygame.init()
# this is just my screen I created win defines it
win = pygame.display.set_mode((500,500))
# this is my caption for my game
pygame.display.set_caption("Just Tryna learn Something")
# these are my coordinates for my enemy where it will spawn
cordx = 300
cordy = 300
heights = 70
widths = 70
# my Player Coordinate and its speed and and its Jump
x = 200
y = 200
height = 40
width = 40
speed = 5
isJump = False
jumpCount = 10
# main loop
# main loop for my game
running = True
while running:
pygame.time.delay(100)
win.fill((0,0,0))
#-----------------------------------------------------------------------------------------
# this here draws my player in my window
Player = pygame.draw.rect(win, (140, 0,150), (x, y, height, width))
#-----------------------------------------------------------------------------------------
# this here draws my enemy
Enemy = pygame.draw.rect(win, (90,90,90), (cordx, cordy, heights, widths))
#=-------------------------------------------------------------------------------------
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
#-------------------------------------------------------------------
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
x -= speed
if keys[pygame.K_RIGHT]:
x += speed
# this here is my functions for movement and Jumping
if not(isJump):
if keys[pygame.K_UP]:
y -= speed
if keys[pygame.K_DOWN]:
y += speed
if keys[pygame.K_SPACE]:
isJump = True
else:
if jumpCount >= -10:
y -= (jumpCount * abs(jumpCount)) * 0.5
jumpCount -= 1
else:
jumpCount = 10
isJump = False
# COLLISION here is my collision detect and collision
its suppose to make me stand on the square with my little box when I jump on it
but it doesnt seem to work (Enemy) is the big box and (Player) is the little box
if Player.colliderect(Enemy):
pygame.draw.rect(win, (150,0,140), (50, 50, 20, 70))
if Player.top >= 375 and Player.top <= 370:
x = 375
# ---------------------------------------------------------
pygame.display.update()
pygame.quit()
#---碰撞位于主循环的底部
# ------
#这是我导入的pygame模块
导入pygame
pygame.init()
#这只是我创建的屏幕,win定义了它
win=pygame.display.set_模式((500500))
#这是我的游戏说明
pygame.display.set_标题(“只是尝试学习一些东西”)
#这是我的敌人的坐标
cordx=300
科迪=300
高度=70
宽度=70
#我的球员坐标,速度和跳跃
x=200
y=200
高度=40
宽度=40
速度=5
isJump=False
跳数=10
#主回路
#我的游戏主循环
运行=真
运行时:
pygame.时间延迟(100)
胜利填充((0,0,0))
#-----------------------------------------------------------------------------------------
#这将在我的窗口中绘制我的播放器
Player=pygame.draw.rect(赢,(1400150),(x,y,高度,宽度))
#-----------------------------------------------------------------------------------------
#这吸引了我的敌人
敌人=pygame.draw.rect(赢,(90,90,90),(cordx,cordy,高度,宽度))
#=-------------------------------------------------------------------------------------
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
运行=错误
#-------------------------------------------------------------------
keys=pygame.key.get_pressed()
如果键[pygame.K_左]:
x-=速度
如果键[pygame.K_RIGHT]:
x+=速度
#这是我的动作和跳跃功能
如果不是(isJump):
如果键[pygame.K_UP]:
y-=速度
如果键[pygame.K_DOWN]:
y+=速度
如果键[pygame.K_SPACE]:
isJump=True
其他:
如果跳线计数>=-10:
y-=(跳数*绝对值(跳数))*0.5
跳转计数-=1
其他:
跳数=10
isJump=False
#碰撞这里是我的碰撞检测和碰撞
当我跳上我的小盒子时,它会让我站在广场上
但它似乎不起作用(敌人)是大盒子,(玩家)是小盒子
如果玩家.collide rect(敌人):
pygame.draw.rect(赢,(150,0140),(50,50,20,70))
如果Player.top>=375且Player.top连续让玩家摔倒。如果玩家没有跳跃,则在每一帧中添加一个变量fall=0
,并将该变量添加到y
,并递增fall
。如果玩家达到最大跳跃高度(jumpCount==0
),则跳跃结束:
如果不是isJump:
y+=下降
下降+=1
# [...]
其他:
如果jumpCount>0:
y-=(跳数*绝对值(跳数))*0.5
跳转计数-=1
其他:
跳数=10
isJump=False
通过设置播放机的y
坐标,将播放机限制在窗口底部(500)和区块顶部:
Player.topleft=(x,y)
碰撞=错误
如果玩家.collide rect(敌人):
y=敌人。顶级球员。身高
碰撞=真
如果Player.bottom>=500:
y=500-运动员身高
碰撞=真
只有当运动员站在地面或障碍物上时,才允许跳跃:
如果发生碰撞:
如果键[pygame.K_SPACE]:
isJump=True
下降=0
此外,使用and,而不是pygame.time.delay()
,以实现平稳移动。通过每秒触发器(FPS
)控制速度:
FPS=60
clock=pygame.time.clock()
运行=真
运行时:
时钟滴答声(FPS)
#pygame.时间延迟(100)
请参见示例:
#---碰撞位于主循环的底部
# ------
#这是我导入的pygame模块
导入pygame
pygame.init()
#这只是我创建的屏幕,win定义了它
win=pygame.display.set_模式((500500))
#这是我的游戏说明
pygame.display.set_标题(“只是尝试学习一些东西”)
#这是我的敌人的坐标
cordx=300
科迪=350
高度=70
宽度=70
#我的球员坐标,速度和跳跃
x=200
y=200
高度=40
宽度=40
速度=5
isJump=False
跳数=10
下降=0
FPS=60
clock=pygame.time.clock()
#主回路
#我的游戏主循环
运行=真
运行时:
时钟滴答声(FPS)
#pygame.时间延迟(100)
胜利填充((0,0,0))
#-----------------------------------------------------------------------------------------
#这将在我的窗口中绘制我的播放器
Player=pygame.draw.rect(赢,(1400150),(x,y,高度,宽度))
#-----------------------------------------------------------------------------------------
#这吸引了我的敌人
敌人=pygame.draw.rect(赢,(90,90,90),(cordx,cordy,高度,宽度))
#=-------------------------------------------------------------------------------------
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
运行=错误
#-------------------------------------------------------------------
keys=pygame.key.get_pressed()
如果键[pygame.K_左]:
x-=速度
如果键[pygame.K_RIGHT]:
x+=速度
#这是我的动作和跳跃功能
如果不是,请跳转:
y+=下降
下降+=1
Player.topleft=(x,y)
碰撞=错误
如果玩家.collide rect(敌人):
碰撞=真
y=敌人。顶级球员。身高
如果Player.right>敌方.left和Player.left<敌方.left:
x=敌方。左侧-玩家。宽度
如果Player.left<敌方.right和Player.right>敌方.right:
x=敌人。对
如果Player.bottom>=500:
碰撞=真
y=500-运动员身高