java 上下键_java-同时使用WASD和方向键

我正在开发Processing(在Java上运行)中的两人游戏.一个用户将使用WASD键控制其角色,而另一个用户将使用箭头键控制移动.我遇到的问题是,当按下箭头时,使用keyPressed会否定WASD,反之亦然.我已经很长时间了.有谁知道解决方法或注意到我做错了什么?

//global variables

int wide = 600; //canvas width

int tall = 600; //canvas height

int s = 50; //player size

float speed = 2.5; //player movement speed

//colors

int redColor = #CB4646; //player 1 color

int blueColor = #4652CB; //player 2 color

int backgroundColor = #DBE3B3; //background color

float player1X = 600/3-s; //HOW COME width/3 DOESN'T WORK??????????

float player2X = 600*2/3;

float playerY = 600/2-(s/2);

//players

Player player1 = new Player(player1X, playerY, s, speed, "wasd", redColor); //player 1

Player player2 = new Player(player2X, playerY, s, speed, "arrows", blueColor); //player 2

//setup

void setup(){

background(backgroundColor);

size(wide, tall);

smooth();

println(player2.controls);

}

//draw

void draw(){

background(backgroundColor);

player1.usePlayer();

player2.usePlayer();

}

class Player{

//class variables

float x; // x position

float y; // y position

int s; //size

float speed; //speed

String controls; //controls

int colors; //player color

char keyControls [] = new char [4];

//construct

Player(float tempX, float tempY, int tempS , float tempSpeed, String tempControls, int tempColors){

x = tempX;

y = tempY;

s = tempS;

speed = tempSpeed;

controls = tempControls;

colors = tempColors;

}

void usePlayer(){

// draw player

fill(colors);

rect(x, y, s, s);

//move player

keyPressed();

//wraparound

boundaries();

}

void keyPressed(){

//sets controls for wasd

if(controls == "wasd"){

if(key == 'w' || key == 'W'){

y -= speed; //move forwards

}

if(key == 's' || key == 'S'){

y += speed; //move backwards

}

if(key == 'd' || key == 'D'){

x += speed; //move right

}

if(key == 'a' || key == 'A'){

x -= speed; //move left

}

}

//sets controls for arrows

if(controls == "arrows"){

if(key == CODED){

if(keyCode == UP){

y -= speed; //move forwards

}

if(keyCode == DOWN){

y += speed; //move backwards

}

if(keyCode == RIGHT){

x += speed; //move right

}

if(keyCode == LEFT){

x -= speed; //move left

}

}

}

}

//pacman style wraparound

void boundaries(){

if(x == width) x = 2;

if(y == height) y = 2;

if(x == 0) x = width-s;

if(y == 0) y = height-s;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值