Python游戏运动

时间:2015-12-12 19:47:08

标签: python cocos2d-python

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

要做到这一点,我开始使用一个实际的python游戏,我找到了一些在线代码。

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

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

有人可以给我一个关于我做错的提示吗?

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

GitHub Link :: 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")

1 个答案:

答案 0 :(得分:1)

像@Blckknght所指出的那样,绘制图层Game并运行其他计划函数(在您的情况下为Game.update)仅在调用cocos.director.director.run(game_scene)之后启动。这就是为什么你看不到速度更新 - 它们是在绘图开始时完成的。

您应该删除通话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.update