1
General / Wall Tile Collision only works for one tile
« on: September 17, 2017, 08:06:50 am »
Collision only seems to work for the tile in the top left corner. Maybe it is because of how I made the tiles? I yet an amateur in game development and programming in general (btw).
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <math.h>
#include <iostream>
#include "ResourcePath.hpp"
sf::Sprite drawTile(int x1, int y1, sf::Sprite sprite) {
x1 = x1 * 32;
y1 = y1 * 32;
sprite.setPosition(x1, y1);
return sprite;
}
int main(int, char const**)
{
// 20 x 15
const int WIDTH = 640;
const int HEIGHT = 480;
int counter1 = 0;
int counter2;
// Create the main window
sf::RenderWindow window(sf::VideoMode(WIDTH, HEIGHT), "ummmm");
window.setVerticalSyncEnabled(true);
// Set the Icon
sf::Image icon;
if (!icon.loadFromFile(resourcePath() + "pokemon.png")) {
return EXIT_FAILURE;
}
window.setIcon(icon.getSize().x, icon.getSize().y, icon.getPixelsPtr());
// Load player texture and sprite
sf::Texture betty;
if (!betty.loadFromFile(resourcePath() + "betty.png")) {
return EXIT_FAILURE;
}
sf::Sprite player(betty);
player.setTextureRect(sf::IntRect(0, 0, 48 , 48));
// collision box for player
sf::FloatRect playerBox;
// Load sky texture and sprite
sf::Texture tile;
if (!tile.loadFromFile(resourcePath() + "tiles.png")) {
return EXIT_FAILURE;
}
sf::Sprite sky(tile);
sky.setTextureRect(sf::IntRect(0, 0, 32, 32));
sf::Sprite brick(tile);
brick.setTextureRect(sf::IntRect(32, 0, 32, 32));
sf::Sprite grass(tile);
grass.setTextureRect(sf::IntRect(64, 0, 32, 32));
sf::Sprite stone(tile);
stone.setTextureRect(sf::IntRect(96, 0, 32, 32));
// collision for other tiles
sf::FloatRect stoneBox;
// Start the game loop
while (window.isOpen())
{
// Process events
sf::Event event;
while (window.pollEvent(event))
{
// Close window: exit
if (event.type == sf::Event::Closed) {
window.close();
}
// Escape pressed: exit
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape) {
window.close();
}
}
// Player
player.setOrigin(-320, -240);
float movementSpeed = 5.0;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
player.move(0, -movementSpeed);
if (playerBox.intersects(stoneBox)) {
player.move(0, movementSpeed);
}
player.setTextureRect(sf::IntRect(96, 0 + (counter2 * 48), 48 , 48));
counter1++;
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) {
player.move(0, movementSpeed);
if (playerBox.intersects(stoneBox)) {
player.move(0, movementSpeed);
}
player.setTextureRect(sf::IntRect(0, 0 + (counter2 * 48), 48 , 48));
counter1++;
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
player.move(movementSpeed, 0);
if (playerBox.intersects(stoneBox)) {
player.move(movementSpeed, 0);
}
player.setTextureRect(sf::IntRect(144, 0 + (counter2 * 48), 48 , 48));
counter1++;
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
player.move(-movementSpeed, 0);
if (playerBox.intersects(stoneBox)) {
player.move(movementSpeed, 0);
}
player.setTextureRect(sf::IntRect(48, 0 + (counter2 * 48), 48 , 48));
counter1++;
}
// counter system for animation
switch(counter1) {
case 0:
counter2 = 0;
break;
case 7:
counter2 = 1;
break;
case 14:
counter2 = 2;
break;
case 21:
counter2 = 3;
break;
case 28:
counter1 = 0;
break;
}
// Clear screen with white
window.clear(sf::Color::White);
// Draw the sprites
for (int x = 0; x < 20; x++) {
for (int y = 0; y < 15; y++) {
window.draw(drawTile(x, y, brick));
}
}
for (int x = 0; x < 20; x++) {
for (int y = 0; y < 15; y++) {
window.draw(drawTile(0, y, stone));
window.draw(drawTile(19, y, stone));
}
window.draw(drawTile(x, 0, stone));
window.draw(drawTile(x, 14, stone));
}
// collision checking
stoneBox = stone.getGlobalBounds();
playerBox = player.getGlobalBounds();
window.draw(player);
// Update the window
window.display();
}
return EXIT_SUCCESS;
}
#include <SFML/Graphics.hpp>
#include <math.h>
#include <iostream>
#include "ResourcePath.hpp"
sf::Sprite drawTile(int x1, int y1, sf::Sprite sprite) {
x1 = x1 * 32;
y1 = y1 * 32;
sprite.setPosition(x1, y1);
return sprite;
}
int main(int, char const**)
{
// 20 x 15
const int WIDTH = 640;
const int HEIGHT = 480;
int counter1 = 0;
int counter2;
// Create the main window
sf::RenderWindow window(sf::VideoMode(WIDTH, HEIGHT), "ummmm");
window.setVerticalSyncEnabled(true);
// Set the Icon
sf::Image icon;
if (!icon.loadFromFile(resourcePath() + "pokemon.png")) {
return EXIT_FAILURE;
}
window.setIcon(icon.getSize().x, icon.getSize().y, icon.getPixelsPtr());
// Load player texture and sprite
sf::Texture betty;
if (!betty.loadFromFile(resourcePath() + "betty.png")) {
return EXIT_FAILURE;
}
sf::Sprite player(betty);
player.setTextureRect(sf::IntRect(0, 0, 48 , 48));
// collision box for player
sf::FloatRect playerBox;
// Load sky texture and sprite
sf::Texture tile;
if (!tile.loadFromFile(resourcePath() + "tiles.png")) {
return EXIT_FAILURE;
}
sf::Sprite sky(tile);
sky.setTextureRect(sf::IntRect(0, 0, 32, 32));
sf::Sprite brick(tile);
brick.setTextureRect(sf::IntRect(32, 0, 32, 32));
sf::Sprite grass(tile);
grass.setTextureRect(sf::IntRect(64, 0, 32, 32));
sf::Sprite stone(tile);
stone.setTextureRect(sf::IntRect(96, 0, 32, 32));
// collision for other tiles
sf::FloatRect stoneBox;
// Start the game loop
while (window.isOpen())
{
// Process events
sf::Event event;
while (window.pollEvent(event))
{
// Close window: exit
if (event.type == sf::Event::Closed) {
window.close();
}
// Escape pressed: exit
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape) {
window.close();
}
}
// Player
player.setOrigin(-320, -240);
float movementSpeed = 5.0;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
player.move(0, -movementSpeed);
if (playerBox.intersects(stoneBox)) {
player.move(0, movementSpeed);
}
player.setTextureRect(sf::IntRect(96, 0 + (counter2 * 48), 48 , 48));
counter1++;
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) {
player.move(0, movementSpeed);
if (playerBox.intersects(stoneBox)) {
player.move(0, movementSpeed);
}
player.setTextureRect(sf::IntRect(0, 0 + (counter2 * 48), 48 , 48));
counter1++;
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
player.move(movementSpeed, 0);
if (playerBox.intersects(stoneBox)) {
player.move(movementSpeed, 0);
}
player.setTextureRect(sf::IntRect(144, 0 + (counter2 * 48), 48 , 48));
counter1++;
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
player.move(-movementSpeed, 0);
if (playerBox.intersects(stoneBox)) {
player.move(movementSpeed, 0);
}
player.setTextureRect(sf::IntRect(48, 0 + (counter2 * 48), 48 , 48));
counter1++;
}
// counter system for animation
switch(counter1) {
case 0:
counter2 = 0;
break;
case 7:
counter2 = 1;
break;
case 14:
counter2 = 2;
break;
case 21:
counter2 = 3;
break;
case 28:
counter1 = 0;
break;
}
// Clear screen with white
window.clear(sf::Color::White);
// Draw the sprites
for (int x = 0; x < 20; x++) {
for (int y = 0; y < 15; y++) {
window.draw(drawTile(x, y, brick));
}
}
for (int x = 0; x < 20; x++) {
for (int y = 0; y < 15; y++) {
window.draw(drawTile(0, y, stone));
window.draw(drawTile(19, y, stone));
}
window.draw(drawTile(x, 0, stone));
window.draw(drawTile(x, 14, stone));
}
// collision checking
stoneBox = stone.getGlobalBounds();
playerBox = player.getGlobalBounds();
window.draw(player);
// Update the window
window.display();
}
return EXIT_SUCCESS;
}