在pygame 2d滚动平台游戏中定位并添加敌人?

时间:2019-02-04 07:13:33

标签: python pygame

我一直在尝试使用python和pygame模块创建mario类型的2d侧滚动平台游戏。我使用了来自programarcadegames的教程来编写平台和播放器的代码,但我只是想不出如何实现以特定x和y坐标生成的敌人并将它们添加为可碰撞的精灵(这会“杀死”一个玩家)点击)?

教程代码如下:

http://programarcadegames.com/python_examples/show_file.php?file=platform_moving.py

我曾尝试为敌人的精灵创建基本类,并使其前后移动,但定位是我的主要问题。

这是我的代码:(水平滚动时敌人确实会出现一点故障)

class Enemy(pygame.sprite.Sprite):

    def __init__(self):

        super().__init__()

        width = 30
        height = 30
        self.image = pygame.Surface([width, height])
        self.image.fill(BLUE)

        # Set a reference to the image rect.
        self.rect = self.image.get_rect()

        # Set speed vector of player
        self.change_x = random.randint(3, 4)
        self.change_y = 0

    def update(self):

        self.rect.centerx += self.change_x
        if self.rect.right <= 0 or self.rect.left >= 100:
            self.change_x *= -1

1 个答案:

答案 0 :(得分:1)

对于与播放器的碰撞,我建议您这样:

WARNING: SQLCommand execution failure: java.io.IOException: prepare query failed[nQSError: 43113] Message returned from OBIS.
[nQSError: 27002] Near <table>: Syntax error [nQSError: 26012] .
Failed query text:
drop table "Sample Targets Lite"...C_0DEFAULT  
Feb 04, 2019 11:39:08 PM oracle.odi.runtime.agent.execution.sql.SQLCommand
WARNING: SQLCommand execution failure: java.io.IOException: prepare query failed[nQSError: 43113] Message returned from OBIS.
[nQSError: 27002] Near <table>: Syntax error [nQSError: 26012] .
Failed query text:
create table "Sample Targets Lite"...C_0DEFAULT
(DEPARTMENT_NAME    VARCHAR(30) 
);  
Feb 04, 2019 11:39:09 PM oracle.odi.agent
WARNING: Agent Internal encountered an error: ODI-1226: Step Physical_STEP fails after 1 attempt(s).
Feb 04, 2019 11:39:09 PM oracle.odi.agent
SEVERE: Session ggfgfggf_Physical_SESS (59) fails with return code ODI-1298.
ODI-1217: Session ggfgfggf_Physical_SESS (59) fails with return code ODI-1298.
ODI-1226: Step Physical_STEP fails after 1 attempt(s).
ODI-1227: Task SERIAL-MAP_MAIN- fails on the source connection <Empty Value>.
ODI-1298: Serial task "SERIAL-MAP_MAIN- (10)" failed because child task "SERIAL-EU-LS_HR_CLASS_PROJECT_1_UNIT (20)" is in error.
ODI-1298: Serial task "SERIAL-EU-LS_HR_CLASS_PROJECT_1_UNIT (20)" failed because child task "Create work table-LKM SQL to SQL (JYTHON)- (40)" is in error.
ODI-1228: Task Create work table-LKM SQL to SQL (JYTHON)- fails on the target connection BI_connect.
Caused By: java.sql.SQLException: java.io.IOException: prepare query failed[nQSError: 43113] Message returned from OBIS.
[nQSError: 27002] Near <table>: Syntax error [nQSError: 26012] .
    at oracle.bi.jdbc.AnaJdbcStatementImpl.executeQuery(AnaJdbcStatementImpl.java:387)
...

Feb 04, 2019 11:39:09 PM oracle.odi.agent
SEVERE: Agent  encountered an error: ODI-1217: Session ggfgfggf_Physical_SESS (59) fails with return code ODI-1298. Caused by: ODI-1226: Step Physical_STEP fails after 1 attempt(s).
Feb 04, 2019 11:58:51 PM null
INFO: Disconnecting from Source.
Feb 04, 2019 11:58:51 PM null
INFO: Disconnecting from Repository
Feb 04, 2019 11:58:51 PM null
INFO: Reverse End.

“敌人”必须是一个精灵组。要创建一个精灵组:

#in your gameloop
playerEnemyCollision = pygame.sprite.spritecollide(player, enemies, False)

要创建一个新的敌人并将其添加到组中,只需键入:

#outside your gameloop
enemies = pygame.sprite.Group()

现在您可以使用以下方法检查碰撞情况:

#outside your gameloop
en = Enemy()
en.rect.x = XX #set your Enemies x-Position
en.rect.y = YY #set your Enemies y-Position
en.add(enemies) #adds the enemy "en" to the sprite-group "enemies"

在大多数情况下,改变精灵的位置以使其超出“敌人级”之外的正常移动并不是一个好主意。 希望我能帮助您解决您的问题。 Twistios