Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Game Program (Pong Movement Stutter)  (Read 1774 times)

0 Members and 1 Guest are viewing this topic.

Cayle

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Game Program (Pong Movement Stutter)
« on: December 06, 2013, 03:06:06 pm »
Hi, I am makeing a pong game, and for my movement, the paddle stutters when it moves, like when you hold down a letter on the keyboard. Taking that in mind I compensated for it in my code. The same strategy worked in another program but not in this one.

main(pong)
pong.cpp:
Code: [Select]
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include "paddle.hpp"

/* Program Flow
init -> mainMenu -> game -> exit / main
*/

int main(){
paddle cayle;
sf::RenderWindow window(sf::VideoMode(800,600), "Pong -By Cayle");
cayle.createSprite();
window.setVerticalSyncEnabled(true);
while(window.isOpen()){
sf::Event event;

while(window.pollEvent(event)){

if(event.type == sf::Event::Closed){window.close();}
cayle.processEvent(event);
}

window.clear(sf::Color::Black);
window.draw(cayle.sprite);
window.display();
}
return 0;
}

paddle class:

Code: [Select]

#include <string>
#include <SFML/Graphics.hpp>
#include "paddle.hpp"

int paddle::getInt(std::string var){
int data;

if(var == "xPos"){
data = this -> xPos;
}else if(var == "yPos"){
data = this -> yPos;
}else if(var == "length"){
data = this -> length;
}
return data;
}

int paddle::move(int distance){
this -> yPos += distance;
return this -> yPos;
}

void paddle::createSprite(){
this -> texture.loadFromFile("../resources/paddleTexture.png");
this -> sprite.setTexture(texture);
}
void paddle::processEvent(sf::Event event){


bool upPress, downPress, leftPress, rightPress;

if(event.type == sf::Event::KeyPressed){

if(event.key.code == sf::Keyboard::Left){
leftPress = true;
}
if(event.key.code == sf::Keyboard::Right){
rightPress = true;
}
if(event.key.code == sf::Keyboard::Up){
upPress = true;
}
if(event.key.code == sf::Keyboard::Down){
downPress = true;
}
}

if(event.type == sf::Event::KeyReleased){

if(event.key.code == sf::Keyboard::Left){
leftPress = false;
}
if(event.key.code == sf::Keyboard::Right){
rightPress = false;
}
if(event.key.code == sf::Keyboard::Up){
upPress = false;
}
if(event.key.code == sf::Keyboard::Down){
downPress = false;
}
}

if(upPress){this -> yPos -= this -> speed;}
if(downPress){this -> yPos += this -> speed;}
sprite.setPosition(this -> xPos, this -> yPos);

}

paddle class header:

Code: [Select]
#ifndef PADDLE_HPP
#define PADDLE_HPP
#include <string>
#include <SFML/Graphics.hpp>
class paddle{
private:


int speed;
int length;


public:
int xPos;
int yPos;
paddle(){
xPos = 60;
yPos = 300;
speed = 7;
length = 80;
}
sf::Texture texture;
sf::Sprite sprite;
void createSprite();
void processEvent(sf::Event);
int move(int distance);
int getInt(std::string var);
};

#endif

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Game Program (Pong Movement Stutter)
« Reply #1 on: December 06, 2013, 03:10:47 pm »
Separate your movement code from your event handling code. Why would moving the paddle only when an event happens work? (or worse when multiple events happen)
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Njifra

  • Guest
Re: Game Program (Pong Movement Stutter)
« Reply #2 on: December 06, 2013, 05:21:26 pm »
And if you are not checking for key press using events, use:
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
     //move paddle to left
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
     //move paddle to right
}
 

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10989
    • View Profile
    • development blog
    • Email
AW: Game Program (Pong Movement Stutter)
« Reply #3 on: December 06, 2013, 05:37:50 pm »
But outside the event loop. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Njifra

  • Guest
Re: Game Program (Pong Movement Stutter)
« Reply #4 on: December 06, 2013, 06:50:01 pm »
Yes, of course... Outside of event loop, doh...

 

anything