I have tried to make it look like falling snow but snow is coming from everywhere and I have to make falling snow from library graphics.h. I don't know how to make it move but I already add x and y. For int speed I don't know to use it I see from other code which is from movement of the ball. This code is for my assignment, can you guys help me?
#include <graphics.h>
class Snow
{
private:
int x,y;
int color;
int speed;
public:
Snow(int _x, int _y, int _color, int _speed)
{
x=_x;
y=_y;
color=_color;
speed=_speed;
}
int getColor()
{
return color;
}
int getSpeed()
{
return speed;
}
int getX()
{
return x;
}
int getY()
{
return y;
}
void setX(int value)
{
//x=rand();
x=value;
}
void setY (int value)
{
//y=rand();
y=value;
}
void setColor(int value)
{
color=value;
}
void setSpeed(int value)
{
speed=value;
}
void draw()
{
setcolor(color);
setfillstyle(SOLID_FILL,color);
fillellipse(x,y,2,2);
}
void undraw()
{
setcolor(BLACK);
fillellipse(x,y,2,2);
}
void move()
{
undraw();
x=rand() % getmaxwidth();
y=rand() % getmaxheight();
draw();
}
};
int main()
{
int screenWidth = getmaxwidth();
int screenHeight = getmaxheight();
initwindow(screenWidth, screenHeight, "Snowy day");
Snow p1(0,0,WHITE,10);
while (!kbhit())
{
delay(100);
p1.move();
}
return 0;
}
You should use speed in move function. For example:
void move() {
undraw();
x=rand() % getmaxwidth();
// fall
y += speed;
draw();
}
Related
I am using an SDL Template and I am trying to implement gravity into my code so I am trying to set a Y as the strength of my gravity in update but when I try to use a pointer to setY I always get an error "a pointer to a bound function may only be used to call the function using a setter" the code below is from my GameScene.cpp
#include "GameScene.h"
GameScene::GameScene()
{
// Register and add game objects on constructor
player = new Player();
this->addGameObject(player);
floor = new Floor();
this->addGameObject(floor);
}
GameScene::~GameScene()
{
delete player;
}
void GameScene::start()
{
Scene::start();
// Initialize any scene logic here
}
void GameScene::draw()
{
Scene::draw();
}
void GameScene::update()
{
Scene::update();
if (player->getOnFloor() == false) {
player->setY -= 1;
}
else {
player->setY = 0;
}
}
This code is from my Player.h where the setters and getters are located
#pragma once
#include "GameObject.h"
#include "common.h"
#include "draw.h"
class Player :
public GameObject
{
public:
void start();
void update();
void draw();
//X Setter
void setX(int x) {
x = x;
}
//X Getter
int getX() {
return x;
}
//Y Setter
void setY(int y) {
y = y;
}
//Y Getter
int getY() {
return y;
}
//Height Setter
void setHeight(int height) {
height = height;
}
//Height Getter
int getHeight() {
return height;
}
//Width Setter
void setWidth(int width) {
width = width;
}
//Width Getter
int getWidth() {
return width;
}
//OnFloor Setter
void setOnFloor(int onFloor) {
onFloor = onFloor;
}
//OnFloor Getter
int getOnFloor() {
return onFloor;
}
private:
SDL_Texture* texture;
int x;
int y;
int height;
int width;
int speed;
bool onFloor;
};
I have tried putting () beside the setY like this
if (player->getOnFloor() == false) {
player->setY() -= 1;
}
else {
player->setY)( = 0;
}
But it still did not work, I was expecting it to make my player fall down but VS studio kept showing me the error
this may be a noobie question but I am still a noobie programmer so please bear with me
EDITED:
The player is now falling upwards, I tried doing -= 1 to make him fall downwards
void GameScene::update()
{
Scene::update();
if (player->getOnFloor() == false) {
player->setY(player->getY() -= 1);
}
else {
player->setY(0);
}
}
but I get an error on player in "player->getY()" saying that expression must be a modifiable lvalue
The error you are getting is because you are trying to use the setY setter function as a variable. In order to use the setter function to set the y property of the player object, you need to call the setY function and pass in the new value for y as an argument. Here is how you would use the setY setter function in your GameScene::update function:
void GameScene::update()
{
Scene::update();
if (player->getOnFloor() == false) {
player->setY(player->getY() + 1);
}
else {
player->setY(0);
}
}
Note that in this example, we are calling the getY getter function to get the current value of y, and then subtracting 1 from it before passing the result to the setY setter function. This will update the y property of the player object and make it fall down by 1 unit each time the update function is called.
I think I should write implementation for my Circle class, but I'm not sure and I don't know, how to transfer Color as function parameter in main bcz compiler doesn't work with sf::Color::Red or just Red as function parameter in main function
#include <SFML/Graphics.hpp>
using namespace sf;
const int APPLICATION_WIDTH = 400;
const int APPLICATION_HEIGHT = 300;
class Figure : public sf::Shape{
protected:
double m_x0 = 0, m_y0 = 0;
float m_angle = 0;
double m_scale;
public:
virtual ~Figure() {}
void setCoordX(double x0) { m_x0 = x0; }
void setCoordY(double y0) { m_y0 = y0; }
void setAngle(float angle) { m_angle = angle; }
void setScale(double scale) { m_scale = scale; }
double getCoordX() { return m_x0; }
double getCoordY() { return m_y0; }
float getAngle() { return m_angle; }
double getScale() { return m_scale; }
virtual void drawFigure(sf::RenderWindow& w) = 0;
//virtual void moveFigure(sf::RenderWindow& w, const double vx, const double vy) = 0;
void hideFigure(sf::RenderWindow& w);
virtual void rotateFigure(sf::RenderWindow& w) = 0;
//virtual void scaleFigure(const double vx, const double vy) = 0;
};
void Figure::rotateFigure(sf::RenderWindow& w) {
sf::Shape::rotate(m_angle);
}
void Figure::hideFigure(sf::RenderWindow& w) {
sf::Shape::setFillColor(sf::Color::Transparent);
}
class Circle : public Figure {
private:
CircleShape m_obj;
//double m_x1 , m_y1;
double m_radius = 1;
Vector2f getPoint(std::size_t index) const override {
return m_obj.getPoint(index);
}
std::size_t getPointCount() const override {
return m_obj.getPointCount();
}
public:
//void setCoordX1(double x1) { m_x1 = x1; }
void setRad(double r) { m_radius = r; }
double getRad() { return m_obj.getRadius(); }
double getCenterX() { return m_obj.getRadius(); }
double getCenterY() { return m_obj.getRadius(); }
void drawFigure(sf::RenderWindow& w);
//void moveFigure(sf::RenderWindow& w, const double vx, const double vy);
//void hideFigure(sf::RenderWindow& w) override;
void rotateFigure(sf::RenderWindow& w) override;
};
void Circle::drawFigure(sf::RenderWindow& w) {
m_obj.setRadius(m_radius);
m_obj.setPosition(m_x0, m_y0);
w.draw(m_obj);
}
//void Circle::hideFigure(sf::RenderWindow &w) {
// m_obj.setFillColor(sf::Color::Transparent);
//}
void Circle::rotateFigure(sf::RenderWindow& w) {
//m_obj.setFillColor(sf::Color::Magenta); // if I'll paint it here, it works
m_obj.rotate(m_angle);
}
int main()
{
RenderWindow window(VideoMode(APPLICATION_WIDTH, APPLICATION_HEIGHT), "Lab 6 using SFML");
Circle a, b, c, d;
a.setFillColor(sf::Color::Red);
b.setFillColor(sf::Color::Green);
//c.setFillColor(sf::Color::Blue);
//d.setFillColor(sf::Color::Magenta);
a.setRad(32);
a.setCoordX(50); a.setCoordY(34);
b.setRad(16);
b.setCoordX(10); b.setCoordY(34);
b.setAngle(45);
b.rotateFigure(window);
//b.hideFigure(window);
while (window.isOpen())
{
Event event;
while (window.pollEvent(event))
{
if (event.type == Event::Closed)
window.close();
}
//sf::CircleShape circle;
//circle.setRadius(50);
//circle.setFillColor(sf::Color::Green);
//circle.setPosition((APPLICATION_WIDTH - (circle.getRadius()*2))/2.0,
// (APPLICATION_HEIGHT - (circle.getRadius()*2))/2.0);
//circle.move(30, 30);
//window.draw(circle);
a.drawFigure(window);
b.drawFigure(window);
window.display();
}
return 0;
}
sf::Color is a class that can be passed as a parameter. You can read how sf::Color works in the documentation: https://www.sfml-dev.org/documentation/2.5.1/classsf_1_1Color.php
Example:
#include <SFML/Graphics.hpp>
class MyCircle
{
public:
MyCircle()
{
mCircle.setRadius(50.f);
}
void setColor(sf::Color color)
{
mCircle.setFillColor(color);
}
void draw(sf::RenderWindow& window)
{
window.draw(mCircle);
}
private:
sf::CircleShape mCircle;
};
int main()
{
sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
MyCircle circle;
circle.setColor(sf::Color::Red);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
circle.draw(window);
window.display();
}
return 0;
}
Hey I am trying to make this space game. Now I have developed my ship, and am able to display it. However I would like to be able to use the class for more than one object. I can do this with a constructor but have no clue how to get a constructor working, what changes would I need to make to my code to make the object take an int value as a constructor and allow me to make multiple ships with the code by calling the object.
Here is my header file.
//
// Ship.hpp
// Zerg_Invasion
//
// Created by Flik Wolf on 11/9/15.
//
//
#ifndef Ship_h
#define Ship_h
#include <stdio.h>
#include "ofMain.h"
class Ship {
public:
// Constructor
Ship();
// Methods
void moveLeft();
void moveRight();
void load();
void draw();
void fire();
void keyPressed();
// Properties
int x;
int y;
ofColor color;
ofImage cat;
};
#endif
and here is my CPP file.
//
// Ship.cpp
// Zerg_Invasion
//
// Created by Flik Wolf on 11/9/15.
//
//
#include "Ship.h"
Ship::Ship() {
// Set the initial color
//color.set( ofRandom(255), ofRandom(255), ofRandom(255));
// Initial x position of the ball
x = 450;
// Initial y position of the ball
y = 200;
}
void Ship::moveLeft() {
x -= 10;
}
void Ship::moveRight() {
x += 10;
}
void Ship::load() {
cat.load("spaceShip.png");
}
void Ship::draw() {
cat.draw(x, y);
// ofCircle(x, y, 30);
}
void Ship::fire() {
ofSetColor(255, 255, 255);
ofCircle(x, 200, 2);
}
Also here is the .h and .cpp files for Openframeworks which I am using for graphics.
#pragma once
#include "ofMain.h"
#include "Ship.h"
class ofApp : public ofBaseApp {
public:
void setup();
void update();
void draw();
void keyPressed(int key);
void keyReleased(int key);
void mouseMoved(int x, int y);
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void mouseEntered(int x, int y);
void mouseExited(int x, int y);
void windowResized(int w, int h);
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);
Ship theShip;
};
#include "ofApp.h"
//--------------------------------------------------------------
void ofApp::setup() {
// Smooth edges
ofEnableSmoothing();
// Fixed framerate
ofSetFrameRate(120);
theShip.load();
// No need to define the initial position of the ball
// because the Ball constructor does it for you
}
//--------------------------------------------------------------
void ofApp::update() {
// theShip.move();
}
//--------------------------------------------------------------
void ofApp::draw() {
ofBackground(0);
std::vector <int> nums;
nums.push_back(0);
nums.push_back(1);
nums.push_back(3);
nums.push_back(4);
nums.push_back(5);
nums.push_back(6);
nums.push_back(7);
nums.push_back(8);
cout << nums[0] << endl;
cout << nums[1] << endl;
theShip.draw();
}
//--------------------------------------------------------------
void ofApp::keyPressed(int key) {
if (key == 'a')
{
theShip.moveLeft();
}
if (key == 'd')
{
theShip.moveRight();
}
}
//--------------------------------------------------------------
void ofApp::keyReleased(int key) {
}
//--------------------------------------------------------------
void ofApp::mouseMoved(int x, int y) {
}
//--------------------------------------------------------------
void ofApp::mouseDragged(int x, int y, int button) {
}
//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button) {
theShip.fire();
}
//--------------------------------------------------------------
void ofApp::mouseReleased(int x, int y, int button) {
}
//--------------------------------------------------------------
void ofApp::mouseEntered(int x, int y) {
}
//--------------------------------------------------------------
void ofApp::mouseExited(int x, int y) {
}
//--------------------------------------------------------------
void ofApp::windowResized(int w, int h) {
}
//--------------------------------------------------------------
void ofApp::gotMessage(ofMessage msg) {
}
//--------------------------------------------------------------
void ofApp::dragEvent(ofDragInfo dragInfo) {
}
As mentioned, the constructor in your case should only produce one ship, and this should be the case with all object constructors.
However, it's still easy enough to create and maintain multiple ships (as you've implemented them) if you use a container like std::vector.
Containing multiple ships:
To create a container for your ships, you can use a vector like so:
std::vector<Ship> Ships;
Adding new ships:
To add additional ships to it, you can use std::vector::push_back():
Ships.push_back(Ship()); //Adds a new ship to 'Ships'
Updating the ships:
There are a couple of ways to cycle through your ships:
for (auto& i : Ships)
i.Update(); //Some updating function for each ship
Or, if you need to keep track of the specific position of each ship inside the vector:
for (unsigned int i = 0; i < Ships.size(); ++i)
Ships[i].Update() //The same updating function
How about to get the x and y as constructor argument?
in the .h file:
struct Ship {
// Constructor
Ship(int _x = 450, int _y = 200);
// ...
in the cpp file:
Ship::Ship(int _x, int _y) : x{_x}, y{_y} {
// Set the initial color
//color.set( ofRandom(255), ofRandom(255), ofRandom(255));
}
I'm trying to code a game with Allegro 4 and I've hit a weird bump. The linker is claiming an undefined reference to the destructor in two of my classes, but I've done nothing with it. What could be the issue? Here is my code:
Entity.h:
#pragma once
#include <allegro.h>
struct Rectangle
{
int x;
int y;
int w;
int h;
};
typedef enum {
FACE,
POOP
} EntityType;
class Entity
{
private:
EntityType m_EntityType;
BITMAP *m_Sprite;
int m_Score;
int m_X;
int m_Y;
Rectangle *m_Hitbox;
public:
Entity();
virtual ~Entity() {destroy_bitmap(m_Sprite);}
BITMAP *GetSprite() {return m_Sprite;}
int GetScore() {return m_Score;}
int GetX() {return m_X;}
int GetY() {return m_Y;}
Rectangle GetHitbox() {return *m_Hitbox;}
void SetSprite(EntityType type);
void SetScore(int value) {m_Score = value;}
void SetX(int value) {m_X = value;}
void SetY(int value) {m_Y = value;}
void SetHitbox(EntityType type);
};
Entity.cpp:
#include "Entity.h"
void Entity::SetSprite(EntityType type)
{
if (type == FACE)
m_Sprite = load_bitmap("face.bmp", NULL);
else if (type == POOP)
m_Sprite = load_bitmap("poop.bmp", NULL);
}
void Entity::SetHitbox(EntityType type)
{
if (type == FACE)
{
GetHitbox().x = m_X;
GetHitbox().y = m_Y;
GetHitbox().w = m_X + 32;
GetHitbox().h = m_Y + 32;
}
else if (type == POOP)
{
GetHitbox().x = m_X;
GetHitbox().y = m_Y;
GetHitbox().w = m_X + 16;
GetHitbox().h = m_Y + 16;
}
}
So, im trying to create a pacman game in SFML, i created a class named fant for my ghosts,
im trying to pass a sf::Image has a parameter in my class, but it seems that I got a variable without a value, cause it load a blank image, but it has the same size (18x18 pixels) of the image im trying to load.
My class is
class fant {
public:
void SetX(int i);
int GetX();
void SetY(int i);
int GetY();
void SetDX(int i);
int GetDX();
void SetDY(int i);
int GetDY();
void Sprite(sf::Sprite i);
sf::Sprite GetSprite();
void Image(sf::Image i);
protected:
int posx,posy,dirx,diry;
sf::Sprite sprite;
};
void fant::SetX(int i) { posx=i; sprite.SetX(posx); }
void fant::SetY(int i) { posy=i; sprite.SetY(posy); }
void fant::SetDX(int i) { dirx=i; }
void fant::SetDY(int i) { diry=i; }
void fant::Sprite(sf::Sprite i) { sprite=i; }
void fant::Image(sf::Image i) { sprite.SetImage(i); }
sf::Sprite fant::GetSprite() { return sprite; }
int fant::GetX() { return posx; }
int fant::GetY() { return posy; }
int fant::GetDX() { return dirx; }
int fant::GetDY() { return diry; }
and thats a function that create a new object
void addf() {
fa.push_back(fant());
int t=fa.size()-1;
fa[t].SetX(684); fa[t].SetY(18);
fa[t].Image(f1);
fa[t].SetDX(6);
fa[t].SetDY(0);
}
If i use
void fant::Image(sf::Image i) { sprite.SetImage(f1); }
it sets the "f1" image. (a global sf::Image)
class fant {
public:
sf::Sprite GetSprite()
{
return sprite;
}
void LoadImage(std::string path);
protected:
int posx,posy,dirx,diry;
sf::Sprite sprite;
sf::Image image;
};
and
void fant::LoadImage(std::string path)
{
image.LoadFromFile(path.c_str());
sprite.SetImage(image);
}