week07_01_arras01_background_stroke_ellipse_rect
要來製作一個名叫arras的遊戲砲管砲身,應該類似砲塔的遊戲。
void setup() {
size(500, 400);
}
float x=250, y=200;
void draw() {
background(215, 184, 200);
fill(167, 167, 175);
stroke(110, 110, 113);
strokeWeight(4);
rect(240, 160, 20, 20);
fill(204, 102, 156);
stroke(125, 84, 105);
strokeWeight(3);
ellipse(x, y, 55, 55);
line(x, y, mouseX, mouseY);
}week07_02_arras02_atan_cos_sin現在我們要讓砲管可以隨著滑鼠移動的方向移動,讓我們可以瞄準敵人。利用atan cos sin 等數學概念作出角度。void setup() {
size(500, 400);
}
float x=250, y=200;
void draw() {
background(215, 184, 200);
float dx= mouseX - x, dy= mouseY - y;
float a = atan2(dy, dx);
strokeWeight(20);
line(x, y, x+cos(a)*40, y+sin(a)*40);
fill(167, 167, 175);
stroke(110, 110, 113);
strokeWeight(4);
rect(240, 160, 20, 20);
fill(204, 102, 156);
stroke(125, 84, 105);
strokeWeight(3);
ellipse(x, y, 55, 55);
//line(x, y, mouseX, mouseY);
}
week07_03_arras03_atan_angle_rotate_translate_myTank現在要來讓真正的砲管做角度移動,不用cos tan sin atan。利用大二下電腦圖學教得,translate(), rotate()。void setup() {
size(500, 400);
}
float x=250, y=200;
void draw() {
background(215, 184, 200);
float dx= mouseX - x, dy= mouseY - y;
float a = atan2(dy, dx);
line(x, y, mouseX, mouseY);
myTank(x,y,a);
}
void myTank(float x, float y, float a){
translate(x ,y);
rotate(a);
fill(167, 167, 175);
stroke(110, 110, 113);
strokeWeight(4);
rect(20, -10, 20, 20);
fill(204, 102, 156);
stroke(125, 84, 105);
strokeWeight(3);
ellipse(0, 0, 55, 55);
}
week07_04_arras04_background_for_line_line_void_keyPressed_keyCode準備按照arras來製作,改變背景顏色,砲塔移動,利用keyCode()。void setup() {
size(500, 400);
}
void keyPressed(){
if(keyCode==RIGHT) x+= 1;
if(keyCode==LEFT) x-= 1;
if(keyCode==UP) y-= 1;
if(keyCode==DOWN) y+= 1;
}
float x=250, y=200;
void draw() {
//background(215, 184, 200);
background(219);
stroke(210);
strokeWeight(1);
for(int i=0; i<30; i++){
line(0, i*30, 500, i*30);
line(i*30, 0, i*30, 500);
}
float dx= mouseX - x, dy= mouseY - y;
float a = atan2(dy, dx);
line(x, y, mouseX, mouseY);
myTank(x,y,a);
}
void myTank(float x, float y, float a){
translate(x, y);
rotate(a);
fill(167, 167, 175);
stroke(110, 110, 113);
strokeWeight(4);
rect(20, -10, 20, 20);
fill(204, 102, 156);
stroke(125, 84, 105);
strokeWeight(3);
ellipse(0, 0, 55, 55);
}
week07_05_arras05_correct_move_translate_translate_mx_my我們顛覆移動的定義,讓坦克主角永遠都可以在畫面的中間,所以移動的時候只有背景在移動利用translate(width/2, height/2)把(0,0)移畫面中心。void setup() {
size(500, 400);
}
void keyPressed(){
if(keyCode==RIGHT) x+= 1;
if(keyCode==LEFT) x-= 1;
if(keyCode==UP) y-= 1;
if(keyCode==DOWN) y+= 1;
}
float x=250, y=200;
void draw() {
background(219);
translate(-x, -y);
translate(width/2, height/2);
float mx = mouseX+x-width/2, my= mouseY+y-height/2;
ellipse(mx, my, 8, 8);
stroke(210);
strokeWeight(1);
for(int i=0; i<30; i++){
line(0, i*30, 500, i*30);
line(i*30, 0, i*30, 500);
}
float dx= mx - x, dy= my - y;
float a = atan2(dy, dx);
line(x, y, mouseX, mouseY);
myTank(x,y,a);
}
void myTank(float x, float y, float a){
translate(x, y);
rotate(a);
fill(167, 167, 175);
stroke(110, 110, 113);
strokeWeight(4);
rect(20, -10, 20, 20);
fill(204, 102, 156);
stroke(125, 84, 105);
strokeWeight(3);
ellipse(0, 0, 55, 55);
}
week07_06_arras06_vx_vy_void_keyPressed_void_keyReleased_void_draw
發現移動的速度不夠順暢,因為keyPressed每秒速度不夠快。
void setup() {
size(500, 400);
}
void keyPressed(){//按下去會有速度,放開後速度要變成0
if(keyCode==RIGHT) vx = 1;//x+= 1;
if(keyCode==LEFT) vx =-1; //x-= 1;
if(keyCode==UP) vy = -1;//y-= 1;
if(keyCode==DOWN) vy = 1;//y+= 1;
}
void keyReleased(){
if(keyCode== LEFT || keyCode==RIGHT) vx = 0;
if(keyCode== UP || keyCode==DOWN) vy = 0;
}
float x=250, y=200, vx=0, vy=0;
void draw() {
x += vx;
y += vy;
background(219);
translate(-x, -y);
translate(width/2, height/2);
float mx = mouseX+x-width/2, my= mouseY+y-height/2;
ellipse(mx, my, 8, 8);
stroke(210);
strokeWeight(1);
for(int i=0; i<30; i++){
line(0, i*30, 500, i*30);
line(i*30, 0, i*30, 500);
}
float dx= mx - x, dy= my - y;
float a = atan2(dy, dx);
line(x, y, mouseX, mouseY);
myTank(x,y,a);
}
void myTank(float x, float y, float a){
translate(x, y);
rotate(a);
fill(167, 167, 175);
stroke(110, 110, 113);
strokeWeight(4);
rect(20, -10, 20, 20);
fill(204, 102, 156);
stroke(125, 84, 105);
strokeWeight(3);
ellipse(0, 0, 55, 55);
}
week07_07_arras07_angle_bulletX_bulletY_bulletVX_bulletVY現在我們要做出砲彈當作攻擊手段,根據滑鼠的方向角度來發射。void setup() {
size(500, 400);
}
void keyPressed(){//按下去會有速度,放開後速度要變成0
if(keyCode==RIGHT) vx = 1;//x+= 1;
if(keyCode==LEFT) vx =-1; //x-= 1;
if(keyCode==UP) vy = -1;//y-= 1;
if(keyCode==DOWN) vy = 1;//y+= 1;
}
void keyReleased(){
if(keyCode== LEFT || keyCode==RIGHT) vx = 0;
if(keyCode== UP || keyCode==DOWN) vy = 0;
}
float x=250, y=200, vx=0, vy=0;
void draw() {
x += vx;
y += vy;
background(219);
translate(-x, -y);
translate(width/2, height/2);
float mx = mouseX+x-width/2, my= mouseY+y-height/2;
ellipse(mx, my, 8, 8);
stroke(210);
strokeWeight(1);
for(int i=0; i<30; i++){
line(0, i*30, 500, i*30);
line(i*30, 0, i*30, 500);
}
float dx= mx - x, dy= my - y;
float a = atan2(dy, dx);
angle = a;
line(x, y, mx, my);
pushMatrix();
myTank(x,y,a);
popMatrix();
if(bulletVX!=0 && bulletVY!=0){
bulletX += bulletVX;
bulletY += bulletVY;
ellipse(bulletX, bulletY, 20, 20);
}
}
float angle, bulletX=0, bulletY=0, bulletVX=0, bulletVY=0;
void mousePressed(){
bulletX = x;
bulletY = y;
bulletVX = cos(angle);
bulletVY = sin(angle);
}
void myTank(float x, float y, float a){
translate(x, y);
rotate(a);
fill(167, 167, 175);
stroke(110, 110, 113);
strokeWeight(4);
rect(20, -10, 20, 20);
fill(204, 102, 156);
stroke(125, 84, 105);
strokeWeight(3);
ellipse(0, 0, 55, 55);
}
week07_08_arras08_array_many_bullets現在要來解決子彈只有1顆的問題,我們就利用陣列宣告的方式。但是,超過100顆子彈就會當機,未完待續......void setup() {
size(500, 400);
}
void keyPressed(){//按下去會有速度,放開後速度要變成0
if(keyCode==RIGHT) vx = 1;//x+= 1;
if(keyCode==LEFT) vx =-1; //x-= 1;
if(keyCode==UP) vy = -1;//y-= 1;
if(keyCode==DOWN) vy = 1;//y+= 1;
}
void keyReleased(){
if(keyCode== LEFT || keyCode==RIGHT) vx = 0;
if(keyCode== UP || keyCode==DOWN) vy = 0;
}
float x=250, y=200, vx=0, vy=0;
void draw() {
x += vx;
y += vy;
background(219);
translate(-x, -y);
translate(width/2, height/2);
float mx = mouseX+x-width/2, my= mouseY+y-height/2;
ellipse(mx, my, 8, 8);
stroke(210);
strokeWeight(1);
for(int i=0; i<30; i++){
line(0, i*30, 500, i*30);
line(i*30, 0, i*30, 500);
}
float dx= mx - x, dy= my - y;
float a = atan2(dy, dx);
angle = a;
line(x, y, mx, my);
pushMatrix();
myTank(x,y,a);
popMatrix();
for(int i=0; i<bulletN; i++){
bulletX[i]+= bulletVX[i];
bulletY[i]+= bulletVY[i];
ellipse(bulletX[i], bulletY[i], 20, 20);
}
}
float angle;
int bulletN = 0;
float []bulletX= new float[100];
float []bulletY= new float[100];
float []bulletVX= new float[100];
float []bulletVY= new float[100];
void mousePressed(){
int i = bulletN;
bulletX[i] = x;
bulletY[i]= y;
bulletVX[i]= cos(angle);
bulletVY[i]= sin(angle);
bulletN++;
}
void myTank(float x, float y, float a){
translate(x, y);
rotate(a);
fill(167, 167, 175);
stroke(110, 110, 113);
strokeWeight(4);
rect(20, -10, 20, 20);
fill(204, 102, 156);
stroke(125, 84, 105);
strokeWeight(3);
ellipse(0, 0, 55, 55);
}
week07_09_arras09_recycle_bullet_move_right_to_left要來解決子彈數超過的問題,打到99時就在從50就重新裝填。void setup() {
size(500, 400);
}
void keyPressed(){//按下去會有速度,放開後速度要變成0
if(keyCode==RIGHT) vx = 1;//x+= 1;
if(keyCode==LEFT) vx =-1; //x-= 1;
if(keyCode==UP) vy = -1;//y-= 1;
if(keyCode==DOWN) vy = 1;//y+= 1;
}
void keyReleased(){
if(keyCode== LEFT || keyCode==RIGHT) vx = 0;
if(keyCode== UP || keyCode==DOWN) vy = 0;
}
float x=250, y=200, vx=0, vy=0;
void draw() {
x += vx;
y += vy;
background(219);
translate(-x, -y);
translate(width/2, height/2);
float mx = mouseX+x-width/2, my= mouseY+y-height/2;
ellipse(mx, my, 8, 8);
stroke(210);
strokeWeight(1);
for(int i=0; i<30; i++){
line(0, i*30, 500, i*30);
line(i*30, 0, i*30, 500);
}
float dx= mx - x, dy= my - y;
float a = atan2(dy, dx);
angle = a;
line(x, y, mx, my);
pushMatrix();
myTank(x,y,a);
popMatrix();
for(int i=0; i<bulletN; i++){
bulletX[i]+= bulletVX[i];
bulletY[i]+= bulletVY[i];
ellipse(bulletX[i], bulletY[i], 20, 20);
}
}
float angle;
int bulletN = 0;
float []bulletX= new float[100];
float []bulletY= new float[100];
float []bulletVX= new float[100];
float []bulletVY= new float[100];
void mousePressed(){
int i = bulletN;
bulletX[i] = x;
bulletY[i]= y;
bulletVX[i]= cos(angle);
bulletVY[i]= sin(angle);
bulletN++;
if(bulletN==100){
for(int k=0; k<50; k++){
bulletX[k]= bulletX[k+50];
bulletY[k]= bulletY[k+50];
bulletVX[k]= bulletVX[k+50];
bulletVY[k]= bulletVY[k+50];
}
bulletN = 50;
}
println(bulletN); //印子彈數
}
void myTank(float x, float y, float a){
translate(x, y);
rotate(a);
fill(167, 167, 175);
stroke(110, 110, 113);
strokeWeight(4);
rect(20, -10, 20, 20);
fill(204, 102, 156);
stroke(125, 84, 105);
strokeWeight(3);
ellipse(0, 0, 55, 55);
}
week07_10_arras10_bulletT_for_timeout
解決子彈填充的方式,把右邊移到左邊,設定每個子彈的壽命。
void setup() {
size(500, 400);
}
void keyPressed(){//按下去會有速度,放開後速度要變成0
if(keyCode==RIGHT) vx = 1;//x+= 1;
if(keyCode==LEFT) vx =-1; //x-= 1;
if(keyCode==UP) vy = -1;//y-= 1;
if(keyCode==DOWN) vy = 1;//y+= 1;
}
void keyReleased(){
if(keyCode== LEFT || keyCode==RIGHT) vx = 0;
if(keyCode== UP || keyCode==DOWN) vy = 0;
}
float x=250, y=200, vx=0, vy=0;
void draw() {
x += vx;
y += vy;
background(219);
translate(-x, -y);
translate(width/2, height/2);
float mx = mouseX+x-width/2, my= mouseY+y-height/2;
ellipse(mx, my, 8, 8);
stroke(210);
strokeWeight(1);
for(int i=0; i<30; i++){
line(0, i*30, 500, i*30);
line(i*30, 0, i*30, 500);
}
float dx= mx - x, dy= my - y;
float a = atan2(dy, dx);
angle = a;
line(x, y, mx, my);
pushMatrix();
myTank(x,y,a);
popMatrix();
for(int i=0; i<bulletN; i++){
bulletX[i]+= bulletVX[i];
bulletY[i]+= bulletVY[i];
ellipse(bulletX[i], bulletY[i], 20, 20);
bulletT[i]--;
if(bulletT[i]==0){//消滅i,就拿右邊的來補
for(int k=i+1; k<bulletN; k++){
bulletX[k-1] = bulletX[k];
bulletY[k-1] = bulletY[k];
bulletVX[k-1] = bulletVX[k];
bulletVY[k-1] = bulletVY[k];
bulletT[k-1] = bulletT[k];
}
bulletN --; //少1顆子彈
}
}
println(bulletN);
}
float angle;
int bulletN = 0;
float []bulletX= new float[100];
float []bulletY= new float[100];
float []bulletVX= new float[100];
float []bulletVY= new float[100];
int [] bulletT = new int[100];
void mousePressed(){
int i = bulletN;
bulletX[i] = x;
bulletY[i]= y;
bulletVX[i]= cos(angle);
bulletVY[i]= sin(angle);
bulletT[i] = 600;//每個子彈只有10秒壽命
bulletN++;
if(bulletN==100){
for(int k=0; k<50; k++){
bulletX[k]= bulletX[k+50];
bulletY[k]= bulletY[k+50];
bulletVX[k]= bulletVX[k+50];
bulletVY[k]= bulletVY[k+50];
}
bulletN = 50;
}
}
void myTank(float x, float y, float a){
translate(x, y);
rotate(a);
fill(167, 167, 175);
stroke(110, 110, 113);
strokeWeight(4);
rect(20, -10, 20, 20);
fill(204, 102, 156);
stroke(125, 84, 105);
strokeWeight(3);
ellipse(0, 0, 55, 55);
}
沒有留言:
張貼留言