1

我正在为一个商业问题进行模拟,但我正在尝试构建一个 2d 游戏来演示模拟的动作。

为此,我从一个实际的 Python 游戏开始,我在网上找到了一些代码。

目标是通过随机整数创建 100 次模拟运动(上、下、左、右、停止)。例如,random = 1,然后向左移动。

出于某种原因,我能够发送第一个动作,但游戏忽略了第二个到第 100 个动作。

有人可以给我一个提示我做错了什么吗?

我将非常感谢某人的专业知识。

GitHub链接:: https://github.com/deacons2016/pygame

from __future__ import division

import random
from cocos.actions import AccelDeccel
from cocos.actions import Delay
from cocos.actions import JumpBy
from cocos.actions import Move
from cocos.actions import MoveBy
from cocos.actions import Repeat
from cocos.actions import Reverse
from cocos.actions import RotateBy
from pyglet.window import key

import cocos
import cocos.collision_model as cm
import resources
import time


class Game(cocos.layer.ColorLayer):
    is_event_handler = True

    def __init__(self):
        super(Game, self).__init__(102, 102, 225, 255)

        self.collision_manager = cm.CollisionManagerBruteForce()

        self.player = cocos.sprite.Sprite(resources.player)
        self.player.position = 400, 25
        self.player.velocity = 0, 0
        self.player.speed = 150
        self.add(self.player, z=2)

        self.player.cshape = cm.AARectShape(
            self.player.position,
            self.player.width//2,
            self.player.height//2
        )
        self.collision_manager.add(self.player)

        self.boss = cocos.sprite.Sprite(resources.player)
        self.boss.position = 400, 600
        self.boss.scale = 0.4
        self.add(self.boss, z=1)

        self.boss.cshape = cm.AARectShape(
            self.boss.position,
            self.boss.width//2,
            self.boss.height//2
        )
        self.collision_manager.add(self.boss)

        self.batch = cocos.batch.BatchNode()
        self.enemies = [cocos.sprite.Sprite(resources.player)
                        for i in range(6)]
        positions = ((250, 125), (550, 125), (300, 325), (500, 325),
                     (150, 475), (650, 475))
        for num, enem in enumerate(self.enemies):
            enem.position = positions[num]
            enem.cshape = cm.AARectShape(
                enem.position,
                enem.width//2,
                enem.height//2
            )
            self.collision_manager.add(enem)
            self.batch.add(enem)

        self.add(self.batch, z=1)
        self.player.do(Move())

        move_basic = MoveBy((120, 0), 1)
        self.enemies[0].do(Repeat(move_basic + Reverse(move_basic)))
        self.enemies[1].do(Repeat(Reverse(move_basic) + move_basic))

        move_complex = (MoveBy((-75, 75), 1) +
                        Delay(0.5) +
                        MoveBy((-75, -75), 1) +
                        Delay(0.5) +
                        MoveBy((75, -75), 1) +
                        Delay(0.5) +
                        MoveBy((75, 75), 1) +
                        Delay(0.5))
        self.enemies[2].do(Repeat(move_complex))
        self.enemies[3].do(Repeat(Reverse(move_complex)))

        move_jump = AccelDeccel(JumpBy((200, 0), 75, 3, 3))
        move_jump_rot = AccelDeccel(RotateBy(360, 3))
        self.enemies[4].do(Repeat(move_jump + Reverse(move_jump)))
        self.enemies[4].do(Repeat(move_jump_rot + Reverse(move_jump_rot)))
        self.enemies[5].do(Repeat(Reverse(move_jump) + move_jump))
        self.enemies[5].do(Repeat(Reverse(move_jump_rot) + move_jump_rot))

        self.schedule(self.update)

    def simulate(self):
        x = 100
        while x > 0:
            rand = random.randint(1,5)
            if rand == 1:
                self.movePlayer("left")
                time.sleep(.05)
                print("left")
            elif rand == 2:
                self.movePlayer("right")
                time.sleep(.05)
                print("right")
            elif rand == 3:
                self.movePlayer("up")
                time.sleep(.05)
                print("up")
            elif rand == 4:
                self.movePlayer("down")
                time.sleep(.05)
                print("down")

            elif rand == 5:
                self.movePlayer("space")
                time.sleep(.05)
                print("space")
            x -= 1

    def update(self, dt):
        self.player.cshape.center = self.player.position
        for enem in self.enemies:
            enem.cshape.center = enem.position

        collisions = self.collision_manager.objs_colliding(self.player)
        if collisions:
            if self.boss in collisions:
                print("You won!")
            cocos.director.director.pop()

    def movePlayer(self, symbol):

         if symbol == "left":
                self.player.velocity = -self.player.speed, 0
         elif symbol == "right":
                self.player.velocity = self.player.speed, 0
         elif symbol == "up":
                self.player.velocity = 0, self.player.speed
         elif symbol == "down":
                self.player.velocity = 0, -self.player.speed
         elif symbol == "space":
                self.player.velocity = 0, 0


if __name__ == '__main__':
    cocos.director.director.init(
        width=800,
        height=650,
        caption="Catch your husband!"
    )

    game_layer = Game()
    game_scene = cocos.scene.Scene(game_layer)

    game_layer.simulate()

    cocos.director.director.run(game_scene)

    print("after")
4

1 回答 1

1

就像@Blckknght 指出的那样,仅在调用 cocos.director.director.run(game_scene) 之后才开始绘制图层Game并运行其其他预定功能(在您的情况下Game.update)。这就是为什么您看不到速度更新的原因 - 它们在绘图开始时完成。

您应该删除呼叫game_layer.simulate(),因为它没有预期的效果。

然后你可以这样做:

def simulate(self, dt):
    if self.x > 0:  # set this to 100 in constructor
        rand = random.randint(1,5)
        if rand == 1:
            self.movePlayer("left")
            print("left")
        elif rand == 2:
            self.movePlayer("right")
            print("right")
        elif rand == 3:
            self.movePlayer("up")
            print("up")
        elif rand == 4:
            self.movePlayer("down")
            print("down")

        elif rand == 5:
            self.movePlayer("space")
            print("space")
        x -= 1

请注意,我删除了对time.sleep. 不确定这些是否会导致问题,但最好调用 cocos2d 中的专用schedule_interval(或schedule)函数。

在的构造函数中Game

self.schedule_interval(self.simulate, 0.05)
self.schedule_interval(self.update, 0.05)

要安排Game.simulate您必须将函数的签名更改dt为第二个参数,就像Game.updatehas 一样。

于 2015-12-13T09:50:19.387 回答