Python 我如何使我的播放器与我的平台发生碰撞
所以我一直在想,当我的玩家碰到我平台的任何一边时,玩家会表现得像有一面墙,例如 它只是从两边飞过去,只是和顶部相撞。我希望它停止这样做,实际上与侧面和底部发生碰撞,我曾尝试观看一些教程,但大多数教程都没有谈论不同的主题,或者我只是没有理解,我也尝试了下面显示的代码,但这不起作用 我试过的代码Python 我如何使我的播放器与我的平台发生碰撞,python,pygame,Python,Pygame,所以我一直在想,当我的玩家碰到我平台的任何一边时,玩家会表现得像有一面墙,例如 它只是从两边飞过去,只是和顶部相撞。我希望它停止这样做,实际上与侧面和底部发生碰撞,我曾尝试观看一些教程,但大多数教程都没有谈论不同的主题,或者我只是没有理解,我也尝试了下面显示的代码,但这不起作用 我试过的代码 if event.type == pygame.KEYDOWN: if event.key == pygame.K_d: x_change = -7
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_d:
x_change = -7
if event.key == pygame.K_a:
x_change = 7
if event.type == pygame.KEYUP:
if event.key == pygame.K_d or event.key == pygame.K_a:
x_change = 0
x += x_change
if x > 500 - playerman.width or x < 0:
x = old_x
如果event.type==pygame.KEYDOWN:
如果event.key==pygame.K\u d:
x_变化=-7
如果event.key==pygame.K_a:
x_变化=7
如果event.type==pygame.KEYUP:
如果event.key==pygame.K_d或event.key==pygame.K_a:
x_变化=0
x+=x_变化
如果x>500-玩家宽度或x<0:
x=旧的
我的完整代码
import pygame
pygame.init
window = pygame.display.set_mode((700,500))
pygame.display.set_caption("Noobs First Game")
# Playerman
class Player:
def __init__(self,x,y,width,height,color):
self.x = x
self.y = y
self.width = width
self.height = height
self.color = color
self.speed = 5
self.isJump = False
self.JumpCount = 10
self.fall = 0
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)
class bullet(object):
def __init__(self,x,y,color):
self.x = x
self.y = y
self.color = color
self.speed = 10
self.color = color
self.rect.topleft = (self.x,self.y)
def draw(self):
self.rect.topleft = (self.x,self.y)
pygame.draw.rect(window,self.color,self.rect)
class Platform:
def __init__(self,x,y,width,height,color):
self.x = x
self.y =y
self. width = width
self.color = color
self.height = height
self.color = color
self.rect = pygame.Rect(x,y,width,height)
def draw(self):
self.rect.topleft = (self.x,self.y)
pygame.draw.rect(window,self.color,self.rect)
# Colors for hitbox
white = (255,255,255)
# Drawing Player
playerman = Player(255,255,40,40,white)
#Drawing Platforms
platform1 = Platform(200,470,100,30,white)
platform2 = Platform(400,410,100,30,white)
# List
platforms = [platform1,platform2]
# Windows color
def redrawwindow():
window.fill((0,0,0))
# Drawing the player and other stuff to the screen
playerman.draw()
for Platform in platforms:
Platform.draw()
x = 10
y = 10
x_change = 0
y_change = 0
old_x = x
old_y = y
fps = (30)
clock = pygame.time.Clock()
run = True
while run:
clock.tick(fps)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_d:
x_change = -7
if event.key == pygame.K_a:
x_change = 7
if event.type == pygame.KEYUP:
if event.key == pygame.K_d or event.key == pygame.K_a:
x_change = 0
x += x_change
if x > 500 - playerman.width or x < 0:
x = old_x
# lets player move
keys = pygame.key.get_pressed()
if keys[pygame.K_a]and playerman.x > playerman.speed:
playerman.x -= playerman.speed
if keys[pygame.K_d]and playerman.x < 700 - playerman.height - playerman.speed:
playerman.x += playerman.speed
if keys[pygame.K_w]and playerman.y > playerman.speed:
playerman.y -= playerman.speed
if keys[pygame.K_s]and playerman.y <500 - playerman.width - playerman.speed:
playerman.y += playerman.speed
# About isJump
if not playerman.isJump:
playerman.y += playerman.fall
playerman.fall += 1
playerman.isJump = False
# this part lets you jump on platform only the top
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
# colliding with floor
if playerman.rect.bottom >= 500:
collide = True
playerman.isJump = False
playerman.Jumpcount = 10
playerman.y = 500 - playerman.height
# Jumping
if collide:
if keys[pygame.K_SPACE]:
playerman.isJump = True
playerman.fall = 0
# Jump Count
else:
if playerman.JumpCount >= 0:
playerman.y -= (playerman.JumpCount*abs(playerman.JumpCount))*0.3
playerman.JumpCount -= 1
else:
playerman.isJump = False
playerman.JumpCount = 10
redrawwindow()
pygame.display.update()
pygame.quit()
导入pygame
pygame.init
window=pygame.display.set_模式((700500))
pygame.display.set_标题(“Noobs第一场游戏”)
#玩家
职业球员:
定义初始值(自、x、y、宽度、高度、颜色):
self.x=x
self.y=y
self.width=宽度
自我高度=高度
self.color=颜色
自身速度=5
self.isJump=False
self.JumpCount=10
self.fall=0
self.rect=pygame.rect(x,y,高度,宽度)
def牵引(自):
self.rect.topleft=(self.x,self.y)
pygame.draw.rect(窗口,self.color,self.rect)
类项目符号(对象):
定义初始值(自、x、y、颜色):
self.x=x
self.y=y
self.color=颜色
自身速度=10
self.color=颜色
self.rect.topleft=(self.x,self.y)
def牵引(自):
self.rect.topleft=(self.x,self.y)
pygame.draw.rect(窗口,self.color,self.rect)
课程平台:
定义初始值(自、x、y、宽度、高度、颜色):
self.x=x
self.y=y
自己宽度=宽度
self.color=颜色
自我高度=高度
self.color=颜色
self.rect=pygame.rect(x,y,宽度,高度)
def牵引(自):
self.rect.topleft=(self.x,self.y)
pygame.draw.rect(窗口,self.color,self.rect)
#hitbox的颜色
白色=(255255)
#绘图播放器
玩家=玩家(255255,40,40,白色)
#绘图平台
平台1=平台(200470100,30,白色)
平台2=平台(400410100,30,白色)
#名单
平台=[平台1,平台2]
#窗户颜色
def redrawwindow():
窗口填充((0,0,0))
#将播放器和其他内容绘制到屏幕上
playerman.draw()
对于平台中的平台:
平台绘制()
x=10
y=10
x_变化=0
y_变化=0
old_x=x
老y=y
fps=(30)
clock=pygame.time.clock()
运行=真
运行时:
时钟滴答声(fps)
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
运行=错误
如果event.type==pygame.KEYDOWN:
如果event.key==pygame.K\u d:
x_变化=-7
如果event.key==pygame.K_a:
x_变化=7
如果event.type==pygame.KEYUP:
如果event.key==pygame.K_d或event.key==pygame.K_a:
x_变化=0
x+=x_变化
如果x>500-玩家宽度或x<0:
x=旧的
#让玩家移动
keys=pygame.key.get_pressed()
如果键[pygame.K_a]和playerman.x>playerman.speed:
playerman.x-=playerman.speed
如果键[pygame.K_d]和playerman.x<700-playerman.height-playerman.speed:
playerman.x+=playerman.speed
如果键[pygame.K_w]和playerman.y>playerman.speed:
playerman.y-=playerman.speed
如果键[pygame.K_s]和playerman.y Platform.rect.left和playerman.rect.leftPlatform.rect.right+playerman.width:
playerman.x=Platform.rect.right
#与地面相撞
如果playerman.rect.bottom>=500:
碰撞=真
playerman.isJump=False
playerman.Jumpcount=10
playerman.y=500-playerman.height
#跳跃
如果发生碰撞:
如果键[pygame.K_SPACE]:
playerman.isJump=True
playerman.fall=0
#跳转计数
其他:
如果playerman.JumpCount>=0:
playerman.y-=(playerman.JumpCount*abs(playerman.JumpCount))*0.3
playerman.JumpCount-=1
其他:
playerman.isJump=False
playerman.JumpCount=10
重画窗口()
pygame.display.update()
pygame.quit()
要与侧面发生碰撞,请使用以下方法:
x = (#whatever)
y = (#whatever)
x_change = 0
y_change = 0
old_x = x
old_y = y
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -7
if event.key == pygame.K_RIGHT:
x_change = 7
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
x += x_change
if x > display_width - player_width or x < 0:
x = old_x
x=(#随便什么)
y=(#随便什么)
x_变化=0
y_变化=0
old_x=x
老y=y
如果event.type==pygame.KEYDOWN:
如果event.key==pygame.K_左:
x_变化=-7
如果event.key==pygame.K_RIGHT:
x_变化=7
如果event.type==pygame.KEYUP:
如果event.key==pygame.K_左或event.key==pygame.K_右:
x_变化=0
x+=x_变化
如果x>显示宽度-玩家宽度或x<0:
x=旧的
需要设置一些变量,如显示宽度和播放器宽度、x和y。要与侧面碰撞,请使用以下内容:
x = (#whatever)
y = (#whatever)
x_change = 0
y_change = 0
old_x = x
old_y = y
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -7
if event.key == pygame.K_RIGHT:
x_change = 7
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
x += x_change
if x > display_width - player_width or x < 0:
x = old_x
x=(#随便什么)
y=(#随便什么)
x_变化=0
y_变化=0
old_x=x
老y=y
如果event.type==pygame.KEYDOWN:
如果event.key==pygame.K_左:
x_变化=-7
如果event.key==pygame.K_RIGHT:
x_变化=7
如果event.type==pygame.KEYUP:
如果event.key==pygame.K_左或event.key==pygame.K_右:
零钱