C++ 蛇形游戏边界碰撞

C++ 蛇形游戏边界碰撞,c++,codeblocks,C++,Codeblocks,我想知道是否有人能帮我写一些代码。 下面我有一些可控圆的代码(代表蛇头) 我可以在下面的代码中修改/添加什么,以便当蛇与边界/边界碰撞时,它会从另一侧重复 我已将游戏分辨率设置为1024x768 非常感谢您的帮助:) snake.cpp #include "snake.hpp" #include <cstdlib> void Snake::move() { switch(direction_){ case Direction::North: pos

我想知道是否有人能帮我写一些代码。 下面我有一些可控圆的代码(代表蛇头)

我可以在下面的代码中修改/添加什么,以便当蛇与边界/边界碰撞时,它会从另一侧重复

我已将游戏分辨率设置为1024x768

非常感谢您的帮助:)

snake.cpp

#include "snake.hpp"

#include <cstdlib>

void Snake::move()
{
    switch(direction_){
    case Direction::North:
        position_.y += 10;
        break;
    case Direction::East:
        position_.x += 10;
        break;
    case Direction::South:
        position_.y -= 10;
        break;
    case Direction::West:
        position_.x -= 10;
    }

}


void Snake::render(prg::Canvas& canvas) const
{
    canvas.drawCircle(getPosition().x, getPosition().y,20,prg::Colour::WHITE);
}

void Snake::changeDirection(Direction new_direction)
{
    direction_ = new_direction;
}
#include "play_state.hpp"
#include "ai_snake.hpp"
#include "player_snake.hpp"
#include <iostream>

const size_t MaxShapes {5};
const unsigned int MaxScale {5};

bool PlayState::onCreate()
{
    snakes_.push_back(new AISnake);
    snakes_.back()->setPosition(Position(100,100));
    snakes_.push_back(new PlayerSnake);
    snakes_.back()->setPosition(Position(50,50));

    double x, y;
    for(unsigned shape = 0;shape < MaxShapes;shape++)
    {
        x = (double)(rand() % prg::application.getScreenWidth());
        y = (double)(rand() % prg::application.getScreenHeight());

        shapes_.push_back(Square({x, y}));

    }
    return true;
}

bool PlayState::onDestroy()
{
    return true;
}

void PlayState::onEntry()
{
    prg::application.addKeyListener(*this);
    game_timer_.start();
}

void PlayState::onExit()
{
    prg::application.removeKeyListener(*this);
    game_timer_.stop();
}

void PlayState::onUpdate()
{

}

void PlayState::onRender(prg::Canvas& canvas)
{
    const std::string text = "";

    canvas.blitFast(
        background_,
        canvas.getWidth() / 2 - background_.getWidth() / 2,
        canvas.getHeight() / 2 - background_.getHeight() / 2
    );

    prg::uint text_dims[2];
    prg::Font::MASSIVE.computePrintDimensions(text_dims, text);
    prg::Font::MASSIVE.print(
      canvas,
      prg::application.getScreenWidth() / 2 - text_dims[0] / 2,
      prg::application.getScreenHeight() / 2 - text_dims[1] / 2,
      prg::Colour::RED,
      text);

    for(const auto snake : snakes_) {
    snake->render(canvas);
    }

    for(Shape shapes : shapes_) {
    shapes.render(canvas);
    }
}

bool PlayState::onKey(const prg::IKeyEvent::KeyEvent& key_event)
{
    if(key_event.key_state == KeyEvent::KB_DOWN) {
        switch(key_event.key) {
        case KeyEvent::KB_ESC_KEY:
            prg::application.exit();
            break;

        }
    }
    return true;
}

void PlayState::onTimer(prg::Timer& timer)
{
    for(auto snake : snakes_) {
        snake->move();
    }
}
#if !defined PLAY_STATE_HPP
#define PLAY_STATE_HPP

#include <prg_interactive.hpp>
#include "snake.hpp"
#include "square.hpp"
#include <list>


//Example of forward declaration of Snake class
class Snake;

class PlayState final : public prg::IAppState,
                        public prg::IKeyEvent,
                        public prg::ITimerEvent {
public:
    PlayState() = default;
    bool onCreate() override;
    bool onDestroy() override;
    void onEntry() override;
    void onExit() override;
    void onUpdate() override;
    void onRender(prg::Canvas& canvas) override;

    bool onKey(const prg::IKeyEvent::KeyEvent& key_event) override;
    void onTimer(prg::Timer& timer) override;

private:
    //Snake* snakes_[2] {nullptr,nullptr};
    std::list<Snake*> snakes_;
    prg::Timer game_timer_ {0, 1000 / 30, *this};
    const prg::Image background_ {prg::ImageFile("resources/images/border.bmp").load()};

        std::vector<Shape> shapes_;
};

#endif // PLAY_STATE_HPP
#包括“snake.hpp”
#包括
void Snake::move()
{
开关(方向){
案例方向:北:
位置y+=10;
打破
案件方向:东:
位置_x+=10;
打破
案例方向:南部:
位置y-=10;
打破
案例方向:西:
位置_x-=10;
}
}
void Snake::render(prg::Canvas&Canvas)常量
{
canvas.drawCircle(getPosition().x,getPosition().y,20,prg::color::WHITE);
}
void Snake::changeDirection(方向新建方向)
{
方向=新的方向;
}
播放状态.cpp

#include "snake.hpp"

#include <cstdlib>

void Snake::move()
{
    switch(direction_){
    case Direction::North:
        position_.y += 10;
        break;
    case Direction::East:
        position_.x += 10;
        break;
    case Direction::South:
        position_.y -= 10;
        break;
    case Direction::West:
        position_.x -= 10;
    }

}


void Snake::render(prg::Canvas& canvas) const
{
    canvas.drawCircle(getPosition().x, getPosition().y,20,prg::Colour::WHITE);
}

void Snake::changeDirection(Direction new_direction)
{
    direction_ = new_direction;
}
#include "play_state.hpp"
#include "ai_snake.hpp"
#include "player_snake.hpp"
#include <iostream>

const size_t MaxShapes {5};
const unsigned int MaxScale {5};

bool PlayState::onCreate()
{
    snakes_.push_back(new AISnake);
    snakes_.back()->setPosition(Position(100,100));
    snakes_.push_back(new PlayerSnake);
    snakes_.back()->setPosition(Position(50,50));

    double x, y;
    for(unsigned shape = 0;shape < MaxShapes;shape++)
    {
        x = (double)(rand() % prg::application.getScreenWidth());
        y = (double)(rand() % prg::application.getScreenHeight());

        shapes_.push_back(Square({x, y}));

    }
    return true;
}

bool PlayState::onDestroy()
{
    return true;
}

void PlayState::onEntry()
{
    prg::application.addKeyListener(*this);
    game_timer_.start();
}

void PlayState::onExit()
{
    prg::application.removeKeyListener(*this);
    game_timer_.stop();
}

void PlayState::onUpdate()
{

}

void PlayState::onRender(prg::Canvas& canvas)
{
    const std::string text = "";

    canvas.blitFast(
        background_,
        canvas.getWidth() / 2 - background_.getWidth() / 2,
        canvas.getHeight() / 2 - background_.getHeight() / 2
    );

    prg::uint text_dims[2];
    prg::Font::MASSIVE.computePrintDimensions(text_dims, text);
    prg::Font::MASSIVE.print(
      canvas,
      prg::application.getScreenWidth() / 2 - text_dims[0] / 2,
      prg::application.getScreenHeight() / 2 - text_dims[1] / 2,
      prg::Colour::RED,
      text);

    for(const auto snake : snakes_) {
    snake->render(canvas);
    }

    for(Shape shapes : shapes_) {
    shapes.render(canvas);
    }
}

bool PlayState::onKey(const prg::IKeyEvent::KeyEvent& key_event)
{
    if(key_event.key_state == KeyEvent::KB_DOWN) {
        switch(key_event.key) {
        case KeyEvent::KB_ESC_KEY:
            prg::application.exit();
            break;

        }
    }
    return true;
}

void PlayState::onTimer(prg::Timer& timer)
{
    for(auto snake : snakes_) {
        snake->move();
    }
}
#if !defined PLAY_STATE_HPP
#define PLAY_STATE_HPP

#include <prg_interactive.hpp>
#include "snake.hpp"
#include "square.hpp"
#include <list>


//Example of forward declaration of Snake class
class Snake;

class PlayState final : public prg::IAppState,
                        public prg::IKeyEvent,
                        public prg::ITimerEvent {
public:
    PlayState() = default;
    bool onCreate() override;
    bool onDestroy() override;
    void onEntry() override;
    void onExit() override;
    void onUpdate() override;
    void onRender(prg::Canvas& canvas) override;

    bool onKey(const prg::IKeyEvent::KeyEvent& key_event) override;
    void onTimer(prg::Timer& timer) override;

private:
    //Snake* snakes_[2] {nullptr,nullptr};
    std::list<Snake*> snakes_;
    prg::Timer game_timer_ {0, 1000 / 30, *this};
    const prg::Image background_ {prg::ImageFile("resources/images/border.bmp").load()};

        std::vector<Shape> shapes_;
};

#endif // PLAY_STATE_HPP
#包括“play_state.hpp”
#包括“爱蛇水电站”
#包括“player_snake.hpp”
#包括
常量大小\u t最大形状{5};
常量无符号整数MaxScale{5};
boolplaystate::onCreate()
{
蛇。推回(新艾斯纳克);
蛇背()->设置位置(位置(100100));
蛇。推回(新玩家);
蛇背()->设置位置(位置(50,50));
双x,y;
对于(无符号形状=0;形状render(画布);
}
用于(形状:形状){
形状。渲染(画布);
}
}
bool PlayState::onKey(const prg::IKeyEvent::keyeevent和key_事件)
{
if(key\u event.key\u state==KeyEvent::KB\u DOWN){
开关(按键事件按键){
案例键事件::KB\U ESC\U键:
prg::application.exit();
打破
}
}
返回true;
}
void PlayState::onTimer(prg::Timer和Timer)
{
用于(自动蛇:蛇){
蛇->移动();
}
}
播放州hpp

#include "snake.hpp"

#include <cstdlib>

void Snake::move()
{
    switch(direction_){
    case Direction::North:
        position_.y += 10;
        break;
    case Direction::East:
        position_.x += 10;
        break;
    case Direction::South:
        position_.y -= 10;
        break;
    case Direction::West:
        position_.x -= 10;
    }

}


void Snake::render(prg::Canvas& canvas) const
{
    canvas.drawCircle(getPosition().x, getPosition().y,20,prg::Colour::WHITE);
}

void Snake::changeDirection(Direction new_direction)
{
    direction_ = new_direction;
}
#include "play_state.hpp"
#include "ai_snake.hpp"
#include "player_snake.hpp"
#include <iostream>

const size_t MaxShapes {5};
const unsigned int MaxScale {5};

bool PlayState::onCreate()
{
    snakes_.push_back(new AISnake);
    snakes_.back()->setPosition(Position(100,100));
    snakes_.push_back(new PlayerSnake);
    snakes_.back()->setPosition(Position(50,50));

    double x, y;
    for(unsigned shape = 0;shape < MaxShapes;shape++)
    {
        x = (double)(rand() % prg::application.getScreenWidth());
        y = (double)(rand() % prg::application.getScreenHeight());

        shapes_.push_back(Square({x, y}));

    }
    return true;
}

bool PlayState::onDestroy()
{
    return true;
}

void PlayState::onEntry()
{
    prg::application.addKeyListener(*this);
    game_timer_.start();
}

void PlayState::onExit()
{
    prg::application.removeKeyListener(*this);
    game_timer_.stop();
}

void PlayState::onUpdate()
{

}

void PlayState::onRender(prg::Canvas& canvas)
{
    const std::string text = "";

    canvas.blitFast(
        background_,
        canvas.getWidth() / 2 - background_.getWidth() / 2,
        canvas.getHeight() / 2 - background_.getHeight() / 2
    );

    prg::uint text_dims[2];
    prg::Font::MASSIVE.computePrintDimensions(text_dims, text);
    prg::Font::MASSIVE.print(
      canvas,
      prg::application.getScreenWidth() / 2 - text_dims[0] / 2,
      prg::application.getScreenHeight() / 2 - text_dims[1] / 2,
      prg::Colour::RED,
      text);

    for(const auto snake : snakes_) {
    snake->render(canvas);
    }

    for(Shape shapes : shapes_) {
    shapes.render(canvas);
    }
}

bool PlayState::onKey(const prg::IKeyEvent::KeyEvent& key_event)
{
    if(key_event.key_state == KeyEvent::KB_DOWN) {
        switch(key_event.key) {
        case KeyEvent::KB_ESC_KEY:
            prg::application.exit();
            break;

        }
    }
    return true;
}

void PlayState::onTimer(prg::Timer& timer)
{
    for(auto snake : snakes_) {
        snake->move();
    }
}
#if !defined PLAY_STATE_HPP
#define PLAY_STATE_HPP

#include <prg_interactive.hpp>
#include "snake.hpp"
#include "square.hpp"
#include <list>


//Example of forward declaration of Snake class
class Snake;

class PlayState final : public prg::IAppState,
                        public prg::IKeyEvent,
                        public prg::ITimerEvent {
public:
    PlayState() = default;
    bool onCreate() override;
    bool onDestroy() override;
    void onEntry() override;
    void onExit() override;
    void onUpdate() override;
    void onRender(prg::Canvas& canvas) override;

    bool onKey(const prg::IKeyEvent::KeyEvent& key_event) override;
    void onTimer(prg::Timer& timer) override;

private:
    //Snake* snakes_[2] {nullptr,nullptr};
    std::list<Snake*> snakes_;
    prg::Timer game_timer_ {0, 1000 / 30, *this};
    const prg::Image background_ {prg::ImageFile("resources/images/border.bmp").load()};

        std::vector<Shape> shapes_;
};

#endif // PLAY_STATE_HPP
#如果!定义的播放状态HPP
#定义播放状态HPP
#包括
#包括“snake.hpp”
#包括“square.hpp”
#包括
//Snake类的前向声明示例
蛇类;
类播放状态最终:公共prg::IAppState,
公共prg::IKeyEvent,
公共prg::ITimerEvent{
公众:
PlayState()=默认值;
bool onCreate()覆盖;
bool onDestroy()覆盖;
void onEntry()覆盖;
void onExit()覆盖;
void onUpdate()覆盖;
void onRender(prg::Canvas&Canvas)覆盖;
bool onKey(const prg::IKeyEvent::keyeevent和key_事件)覆盖;
void onTimer(prg::Timer&Timer)覆盖;
私人:
//Snake*snakes_u2{nullptr,nullptr};
std::列出每一条蛇;
计时器游戏{0,1000/30,*this};
const prg::Image background_{prg::ImageFile(“resources/images/border.bmp”).load();
向量形状;
};
#endif//播放\州\水电站

这适用于1024x768的边界:

if (position_.x < 0) position_.x = 1023; else if (position_.x > 1023) position_.x = 0;
if (position_.y < 0) position_.y = 767; else if (position_.y > 767) position_.y = 0;
if(位置x<0)位置x=1023;否则,如果(位置_x>1023)位置_x=0;
如果(位置y<0)位置y=767;否则,如果(位置y>767)位置y=0;
请注意,宽度1024表示X介于01023之间

从您的代码判断,您使用的是10x10块。如果是这样,您应该根据自己的需要自定义上面的代码

编辑:

根据评论和我的回答,我将此代码合并为您可以遵循的代码:

#include "snake.hpp"

#include <cstdlib>

void Snake::move()
{
    switch(direction_){
    case Direction::North:
        position_.y += 1;
        break;
    case Direction::East:
        position_.x += 1;
        break;
    case Direction::South:
        position_.y -= 1;
        break;
    case Direction::West:
        position_.x -= 1;
    }

    if (position_.x < 0) position_.x = 63; else if (position_.x > 63) position_.x = 0;
    if (position_.y < 0) position_.y = 47; else if (position_.y > 47) position_.y = 0;
}


void Snake::render(prg::Canvas& canvas) const
{
    canvas.drawCircle(getPosition().x * 16, getPosition().y * 16,16,prg::Colour::WHITE);
}

void Snake::changeDirection(Direction new_direction)
{
    direction_ = new_direction;
}
#包括“snake.hpp”
#包括
void Snake::move()
{
开关(方向){
案例方向:北:
位置y+=1;
打破
案件方向:东:
位置_x+=1;
打破
案例方向:南部:
位置y-=1;
打破
案例方向:西:
位置_x-=1;
}
如果(位置x<0)位置x=63;否则如果(位置x>63)位置x=0;
如果(位置y<0)位置y=47;否则如果(位置y>47)位置y=0;
}
void Snake::render(prg::Canvas&Canvas)常量
{
canvas.drawCircle(getPosition().x*16,getPosition().y*16,16,prg::color::WHITE);
}
void Snake::changeDirection(方向新建方向)
{
方向=新的方向;
}
这是一个16x16像素的64x48网格,分辨率为1024x768


玩得开心

你不能让X坐标小于零或大于1024,或者Y坐标小于零或大于768。现在想想你应该在哪里检查。谢谢你的回复,你提到的基本上就是我需要在某处实现的东西。我对C++很陌生,但我想我需要某种类型的转换用例语句。此外,我希望有一些网格系统(20x20或其他)来处理蛇的移动。你知道这怎么可能吗