# Help me with collision detection with screen walls

**URL:** <https://discourse.processing.org/t/help-me-with-collision-detection-with-screen-walls/24248>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [September 30, 2020, 6:46pm UTC](https://discourse.processing.org/t/help-me-with-collision-detection-with-screen-walls/24248 "2020-09-30T18:46:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cal\_codes](https://avatars.discourse-cdn.com/v4/letter/c/ecd19e/32.png) [@Cal\_codes](https://discourse.processing.org/u/Cal_codes)\
**Post date:** [September 30, 2020, 6:46pm UTC](https://discourse.processing.org/t/help-me-with-collision-detection-with-screen-walls/24248/1 "2020-09-30T18:46:16Z")

</div>

//defining classes  
Player player1;

void setup() {  
size(1200, 800);  
player1 = new Player(width/2, 100, 60);  
}

void draw() {  
background(0);  
stroke(255);  
strokeWeight(3);

// draw players  
fill(255);  
noStroke();  
player1.run();  
}

void keyPressed() {  
player1.pressed((key == ‘a’ || key == ‘A’), (key == ‘d’ || key == ‘D’),  
(key == ‘w’ || key == ‘W’), (key == ‘s’ || key == ‘S’));  
}

void keyReleased() {  
player1.released((key == ‘a’ || key == ‘A’), (key == ‘d’ || key == ‘D’),  
(key == ‘w’ || key == ‘W’), (key == ‘s’ || key == ‘S’));  
}

//player variables  
float x, y;  
float dx = 0, dy = 0;  
float speedX = 4;  
float speedY = 4;  
int size;

class Player {  
Player(int newX, int newY, int newSize) {  
x = newX;  
y = newY;  
size = newSize;  
}

void run() {  
display();  
movePlayer();  
}  
void movePlayer() {  
x += dx \* speedX;  
y += dy \* speedY;  
}  
void display() {  
ellipse(x, y, size, size);  
}  
void pressed(boolean left, boolean right, boolean up, boolean down) {  
if (left) dx = -1;  
if (right) dx = 1;  
if (up) dy = -1;  
if (down) dy = 1;  
}  
void released(boolean left, boolean right, boolean up, boolean down) {  
if (left) dx = 0;  
if (right) dx = 0;  
if (up) dy = 0;  
if (down) dy = 0;  
}  
}

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [September 30, 2020, 7:30pm UTC](https://discourse.processing.org/t/help-me-with-collision-detection-with-screen-walls/24248/2 "2020-09-30T19:30:55Z")

</div>

you can check for the screen border with `if(x<size)............` and `if (x>width-size)................`

see [How do I get my shapes to move/animate around the canvas on p5.js?](https://discourse.processing.org/t/how-do-i-get-my-shapes-to-move-animate-around-the-canvas-on-p5-js/24233)
