Win a copy of Mastering Corda: Blockchain for Java Developers this week in the Cloud/Virtualization forum!

don krieg

Greenhorn
+ Follow
since Sep 13, 2020
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
8
Given in last 30 days
6
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by don krieg

Sorry for the late reply. Yes i already noticed it and replace it with the code below. And i already finished with the entire code. Thanks



But one more problem. why is it the background  y-axis string / ... g.drawString(i * unit, 0,  i * unit , screenHeight) /
in the most right part do not fit perfectly with the length of the backgroud?
6 days ago
so in the code there is something that set x,y coordinate to (0, 0) right.
I tried to added below code and succesfully erased the red rectangle.
Correct me if my method is wrong.


In the constructor

1 week ago
Oh i get it. So in order to use my older code ,  i just need to add an enumvariable to control all the(up/down/right/left var). Thank you.

How about my second questions for the red rectangle that appear in the top left background. Do you have any idea about it ?


I already fixed it as below. Kindly please check for the red rectangle on the top left.
I dont know why it only appear for 1 second and then vanish. / Kindly refer to my last comment on the below part

1 week ago
Thank for the reply guys. Now i get it .  Based on the comments, i tried to apply it on the existed  code and its WORKED.. Thanks a lot

in the constructor



in the move method()



in the keyListener method




And i discovered i can  make it move with char / boolean ...Thanks a lot


And one last questions, why is it every time i compile the code , there will be a red rectangle in the most top left of the background? and it
will disappear in 2-3 seconds after i compiled it. I set red rectangle as the snake body part but why is it only this one disappear and placed over there.  
I tried to look for the problem in the paint component method and altered the code a bit , but nothing happen.

#Kindly  try to complile my old code in the above to see the problem  

1 week ago
Thank you for the reply. Correct me if im wrong . In order to prevent that i need to delete these from the constructor right?





But in order to move the snake by Timer , i need to put only one of the above value in the constructor right?

And how about the KeyPressed part. I keep try and error countless time but nothing happen.
Please teach me what is wrong with the below part.






1 week ago
Sorry my bad. The frame class is same as as above.
The new panel class is below.




1 week ago
Sorry for the late reply  .Finally i managed to maked the snake moved by timer. I just add new variables [int up, down ,left, right] to move the x x and y coordinate of the head part. Like below(i add it in move method)

1. In the constructor:
     up = -2;
     down = 2;
     left = -2;
     right = 2;


2. In the move method just add the below code

       if(up == -2) {
y[0] = y[0] - unit;
}
else if(down == 2) {
y[0] = y[0] + unit;
}
else if(right == 2) {
x[0] = x[0] + unit;
}
else if(left == -2) {
x[0] = x[0] - unit;
}


//////////////////////////////////////////

And the for the second problem(headpart is yellow) i already solved the riddle, thanks to you guys. i figured that the headpart is actually overlapped with the body part, that is why the initial colour is yellow.


now i tried to move the snake by KeyListener but i failed a few time
even if i refer the youtube link(change the code a bit) like bellow, but nothing happen.
Please tell me what part in the code i need to change/add, because i tried for another routes but still nothing happen.



    if(e.getKeyCode() == KeyEvent.VK_UP) {
  if(up != down) {
  up = -2;
  }
}
if(e.getKeyCode() == KeyEvent.VK_DOWN) {
  if(down != up) {
  down = 2;
  }
}
if(e.getKeyCode() == KeyEvent.VK_RIGHT) {
  if(right != left) {
  right = 2;
  }
}
if(e.getKeyCode() == KeyEvent.VK_LEFT) {
  if(left != right) {
  left = -2;
  }
}
1 week ago
i dont even know what missed
3 weeks ago
the x, y arrays are use to restore  x, y coordinate of the snake.

Actually i dont know how the snake sets its initial position, as i only follow the youtube(above link). Eventhough i follow the flow and changed a bit of the code still stuck in the middle.
3 weeks ago
Hi Java Masters. Right now i am trying to make snake game . But i encounter problem and stuck in the middle of the process. I tried to look it again and again but cannot find where is my mistake .My questions is;

1. Why is my snake is not moving by Timer .
2. Why the headpart is YELLOW , eventhough i set it as GREEN at first.

My source:
https://www.youtube.com/watch?v=bI6e6qjJ8JQ&t=1605s

Thank you in advance.


/////////////////////////////////////////////////////////////////////
Frame class





Panel class



3 weeks ago
hi im new in java. I want to make  objects bounds at the Jpanel border(so it will not excedd JPanel border), but what i did is something like below. I need to make simple calculation by subtracting  the width of the image from The Jpanel width, to make  it not exceed Jpanel border. But still it exceed the JPanel border slightly. and i dont know but i think there is still better method than using the subtraction method.




Before this i learned that i can use below method to make the bounding more precisely(using this.getWidth()), but only if i make the object code within class with extends Jpanel class, so that why i can use ("this") right?




So when we use different class to make the object code(that does extends Jpanel) , please teach me how to put the method (using this.getWidth()), into class with extended JPanel.
Sorry for the difficult explanation. Thanks in Advance.
Below is the full code














3 months ago
i am new in 2d animation, and now learning the basic collision detection and stuck at this part . For the code below there are four objects. Rectangle, triangle, hexagon, circle(only circle is static).  I can make the
rectangle - triangle collide/bounced (horizontally) each other. But when it come to hexagon - triangle (vertically), i cannot make it collide because i am using wrong method right(line 154-166) ?

What i understand from my research (youtube/ google) to get polygon-polygon collide is, we need to change the polygon to rectangle by using the code below right ? ( correct me if im wrong).



my question is
how do i use the above  code to put it in the below code. and what i need to add to make hexagon - triangle collision happen. Appreciated if you can give hint or link to make me easier to understand. Thanks




 
3 months ago
Dave Tolls , thank you so much for your answer. Now i know the reason. Thanks
3 months ago
I make 2 objects(1. triangle (java.awt.polygon) and 2. rectangle(java.awt.rectangle)) and moved these objects by TIMER . My aim is to make these 2 objects collide and bounce back each other. The problem is Why they are not colliding and bounce (they only overlapped each other), when i used the (..if(intersect..) part(refer code below). The compiler(cmd) encounter error saying ["no suitable method found for intersects(Polygon)"].

i feel weird because before this when i create 2 rectangle(use java.awt.Rectangle) and using the (..if(intersects)...) it work perfectly. These 2 rect touch and bounced each other. I stuck at these part and I need a DEEPER answer to this, what does the error mean ? Thanks




3 months ago
Hi guys, for the code below i managed to make the rectangle moved to the left and bounce back at jpanel width.  But how to make the triangle move horizontally (doesnt matter to the left or right) ? Before this i can make the triangle move by pinpoint the triangle coordinate point by using {..triangle.addPoint(x, y)...}  . but when i used to make the triangle using constructor and arrays, how can i make the triangle move by TIMER? What need i do with the x1 and velx1 variable?

Thank you



4 months ago