Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added arrow key functionality #51

Merged
merged 1 commit into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ You can use Gitpod in the cloud [![Gitpod Ready-to-Code](https://img.shields.io/


## Controls
- Move mouse to control the paddle
- Move mouse to control the paddle, or use 'A'and the left arrow key or 'D' and the right arrow key for left and right movement respectively.


Created with <img src="https://user-images.githubusercontent.com/32013268/193729561-194dea3a-0255-406e-9329-ad5000f1f361.png" height="50px">
Expand Down
8 changes: 4 additions & 4 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,19 +244,19 @@ function mouseMoveHandler(e) {
}
}

document.addEventListener("keypress", keyDownHandler);
document.addEventListener("keydown", keyDownHandler);

function keyDownHandler(e) {
// "D" for right and "A" for left movement
if(e.code == "KeyD") {
// "D" or right arrow key for right and "A" or left arrow key for left movement
if(e.code == "KeyD" || e.code == "ArrowRight") {

let relativeX = paddleX + 10;
if(relativeX < canvas.width - 100) {
paddleX = relativeX + 10;
}
}

if(e.code == "KeyA") {
if(e.code == "KeyA" || e.code == "ArrowLeft") {

let relativeX = paddleX - 10;
if(relativeX > 0 ) {
Expand Down