pygame-通过平台的玩家

如何解决pygame-通过平台的玩家

我正在使用pygame开发平台游戏。 因此,我有那些平台,而我想做的就是,当我按下向下箭头或S键时,玩家会跌倒平台。我只是想不通正确的方法。如果有人有任何示例,或者您可以帮助我,请在此处提供主要代码:

class Player:
def __init__(self,x,y):
    self.entity = e.entity(x,y,14,29,'player')
    self.movingRight = False
    self.movingLeft = False
    self.momentum = 0
    self.airTimer = 0
    self.movement = [0,0]
    self.platformCollision = False
    self.onPlatform = False
    self.through = False
    self.levelOver = False

def events(self,event,dt):
    if event.type == KEYDOWN:
        if event.key == K_RIGHT:
            self.movingRight = True
        if event.key == K_d:
            self.movingRight = True
        if event.key == K_LEFT:
            self.movingLeft = True
        if event.key == K_a:
            self.movingLeft = True
        if event.key == K_UP:
            if self.airTimer == 0:
                self.momentum = -5
        if event.key == K_w:
            if self.airTimer == 0:
                self.momentum = -5
        if event.key == K_DOWN:
            self.through = not self.through
        if event.key == K_s:
            self.through = not self.through

    if event.type == KEYUP:
        if event.key == K_RIGHT:
            self.movingRight = False
        if event.key == K_d:
            self.movingRight = False
        if event.key == K_LEFT:
            self.movingLeft = False
        if event.key == K_a:
            self.movingLeft = False

def update(self,tile_rects,enemiesList,movingList,notCollisionable,screen,scroll,dt,distance):
    self.movement = [0,0]
    if self.movingRight == True:
        self.movement[0] += 300 * dt
    if self.movingLeft == True:
        self.movement[0] -= 300 * dt
    self.movement[1] += self.momentum*2.4
    self.momentum += 20 * dt
    if self.momentum > 5:
        self.momentum = 5

    if self.movement[0] == 0:
        self.entity.set_action('idle')
    elif self.movement[0] > 0:
        self.entity.set_flip(False)
        self.entity.set_action('run')
    elif self.movement[0] < 0:
        self.entity.set_flip(True)
        self.entity.set_action('run')

    collisionList = self.entity.move(self.movement,self.airTimer)

    exitData = [False,False]

    for platform in collisionList['data']:
        if platform[1][3]:
            self.airTimer = 0
            self.momentum = 0
            self.platformCollision = False
        else:
            self.airTimer = 0
            self.platformCollision = False
        if platform[2] == 'horizontal':
            self.entity.obj.x += distance
        if platform[2] == "static":
            self.onPlatform = True
        if platform[2] == 'throughMiddle':
            if self.through:
                #HERE IS WHERE I DONT KNOW WHAT TO DO
                pass

        else:
            self.onPlatform = False
        if platform[2] == "spikeTop":
            exitData[0] = True
        if platform[2] == "endBall":
            self.levelOver = True
            exitData[1] = True

    if not collisionList['bottom']:
        self.airTimer += 1

    self.entity.changeFrame(1)
    self.entity.display(screen,scroll)
    return exitData

以下是处理实体类的引擎部分:

class entity(object):
global animation_database,animation_higher_database

def __init__(self,size_x,size_y,e_type): # x,type
    self.x = x
    self.y = y
    self.size_x = size_x
    self.size_y = size_y
    self.obj = physics_obj(x,size_y)
    self.animation = None
    self.image = None
    self.animation_frame = 0
    self.animation_tags = []
    self.flip = False
    self.offset = [0,0]
    self.rotation = 0
    self.type = e_type # used to determine animation set among other things
    self.action_timer = 0
    self.action = ''
    self.set_action('idle') # overall action for the entity
    self.entity_data = {}
    self.alpha = None

def set_pos(self,y):
    self.x = x
    self.y = y
    self.obj.x = x
    self.obj.y = y
    self.obj.rect.x = x
    self.obj.rect.y = y

def move(self,momentum,platforms,ramps=[],platRects=[],vertRects=[],airTimer=0):
    collisions = self.obj.move(momentum,ramps,platRects,vertRects,airTimer)
    self.x = self.obj.x
    self.y = self.obj.y
    return collisions

def rect(self):
    return pygame.Rect(self.x,self.y,self.size_x,self.size_y)

def set_flip(self,boolean):
    self.flip = boolean

def set_animation_tags(self,tags):
    self.animation_tags = tags

def set_animation(self,sequence):
    self.animation = sequence
    self.animation_frame = 0

def set_action(self,action_id,force=False):
    if (self.action == action_id) and (force == False):
        pass
    else:
        self.action = action_id
        anim = animation_higher_database[self.type][action_id]
        self.animation = anim[0]
        self.set_animation_tags(anim[1])
        self.animation_frame = 0

def get_entity_angle(entity_2):
    x1 = self.x+int(self.size_x/2)
    y1 = self.y+int(self.size_y/2)
    x2 = entity_2.x+int(entity_2.size_x/2)
    y2 = entity_2.y+int(entity_2.size_y/2)
    angle = math.atan((y2-y1)/(x2-x1))
    if x2 < x1:
        angle += math.pi
    return angle

def get_center(self):
    x = self.x+int(self.size_x/2)
    y = self.y+int(self.size_y/2)
    return [x,y]

def clear_animation(self):
    self.animation = None

def set_image(self,image):
    self.image = image

def set_offset(self,offset):
    self.offset = offset

def set_frame(self,amount):
    self.animation_frame = amount

def handle(self):
    self.action_timer += 1
    self.change_frame(1)

def changeFrame(self,amount):
    self.animation_frame += amount
    if self.animation != None:
        while self.animation_frame < 0:
            if 'loop' in self.animation_tags:
                self.animation_frame += len(self.animation)
            else:
                self.animation = 0
        while self.animation_frame >= len(self.animation):
            if 'loop' in self.animation_tags:
                self.animation_frame -= len(self.animation)
            else:
                self.animation_frame = len(self.animation)-1

def get_current_img(self):
    if self.animation == None:
        if self.image != None:
            return flip(self.image,self.flip)
        else:
            return None
    else:
        return flip(animation_database[self.animation[self.animation_frame]],self.flip)

def get_drawn_img(self):
    image_to_render = None
    if self.animation == None:
        if self.image != None:
            image_to_render = flip(self.image,self.flip).copy()
    else:
        image_to_render = flip(animation_database[self.animation[self.animation_frame]],self.flip).copy()
    if image_to_render != None:
        center_x = image_to_render.get_width()/2
        center_y = image_to_render.get_height()/2
        image_to_render = pygame.transform.rotate(image_to_render,self.rotation)
        if self.alpha != None:
            image_to_render.set_alpha(self.alpha)
        return image_to_render,center_x,center_y

def display(self,surface,scroll):
    image_to_render = None
    if self.animation == None:
        if self.image != None:
            image_to_render = flip(self.image,self.rotation)
        if self.alpha != None:
            image_to_render.set_alpha(self.alpha)
        blit_center(surface,image_to_render,(int(self.x)-scroll[0]+self.offset[0]+center_x,int(self.y)-scroll[1]+self.offset[1]+center_y))

以及处理碰撞的功能:

class physics_obj(object):

def __init__(self,x_size,y_size):
    self.width = x_size
    self.height = y_size
    self.rect = pygame.Rect(x,self.width,self.height)
    self.x = x
    self.y = y
    self.prevX = 0

def move(self,movement,tiles,enemiesList=[],movingList=[],notCollisionable=[],airTimer=0):
    #tile_rects,notCollisionable
    self.x += movement[0]
    self.rect.x = int(self.x)

    collision_types = {'top':False,'bottom':False,'right':False,'left':False,'slant_bottom':False,'data':[]}
    # added collision data to "collision_types". ignore the poorly chosen variable name
    #====================================================================
    block_hit_list = collision_test(self.rect,tiles)
    for block in block_hit_list:
        type = "tile"
        markers = [False,False,False]
        if movement[0] > 0:
            self.rect.right = block.left
            collision_types['right'] = True
            markers[0] = True
        elif movement[0] < 0:
            self.rect.left = block.right
            collision_types['left'] = True
            markers[1] = True
        collision_types['data'].append([block,markers,type])
        self.x = self.rect.x
    self.y += movement[1]
    self.rect.y = int(self.y)
    block_hit_list = collision_test(self.rect,False]
        if movement[1] > 0:
            self.rect.bottom = block.top
            collision_types['bottom'] = True
            markers[2] = True
        elif movement[1] < 0:
            self.rect.top = block.bottom
            collision_types['top'] = True
            markers[3] = True
        collision_types['data'].append([block,type])
        self.change_y = 0
        self.y = self.rect.y
    #====================================================================
    block_hit_list = movingCollision(self.rect,movingList)
    for block in block_hit_list:
        type = block.type
        markers = [False,False]
        tol = abs(self.rect.bottom - block.entity.obj.rect.top)
        if movement[1] > 0 and tol < 16:
            self.rect.bottom = block.entity.obj.rect.top
            collision_types['bottom'] = True
            markers[2] = True
        collision_types['data'].append([block.entity.obj.rect,enemiesList)
    for block in block_hit_list:
        type = block.type
        markers = [False,False]
        if movement[0] > 0:
            self.rect.right = block.entity.obj.rect.left
            collision_types['right'] = True
            markers[0] = True
        elif movement[0] < 0:
            #self.rect.left = block.entity.obj.rect.right
            collision_types['left'] = True
            markers[1] = True
        elif movement[1] > 0:
            #self.rect.bottom = block.entity.obj.rect.top
            collision_types['bottom'] = True
            markers[2] = True
        elif movement[1] < 0:
            #self.rect.top = block.entity.obj.rect.bottom
            collision_types['top'] = True
            markers[3] = True
        collision_types['data'].append([block.entity.obj.rect,type])
    #====================================================================
    block_hit_list = movingCollision(self.rect,notCollisionable)
    for block in block_hit_list:
        type = block.type
        markers = [False,False]
        if movement[0] > 0:
            collision_types['right'] = True
            markers[0] = True
        elif movement[0] < 0:
            #self.rect.left = block.entity.obj.rect.right
            collision_types['left'] = True
            markers[1] = True
        elif movement[1] > 0:
            #self.rect.bottom = block.entity.obj.rect.top
            collision_types['bottom'] = True
            markers[2] = True
        elif movement[1] < 0:
            #self.rect.top = block.entity.obj.rect.bottom
            collision_types['top'] = True
            markers[3] = True
        collision_types['data'].append([block.entity.obj.rect,type])
    return collision_types

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-