Zayaan Khan

Ranch Hand
+ Follow
since Oct 30, 2020
Merit badge: grant badges
For More
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
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Zayaan Khan

Yes I believe that the bug lies in the restart() method as my KeyClicked class which extends the KeyAdapter interface is actually working which I have tested through print statements and I am getting feedback in the console (I have attached an image to show this for further understanding) but I am still not too sure if it is a problem of refocusing on components and how I can fix this as although the actual KeyClicked interface is working, on click it does not restart the game. Further help will be appreciated.



1 year ago
By buttons, are you referring to JButton or keypad key buttons? And yes there are several bugs which need to be fixed, I will try to debug them.
1 year ago
When space is pressed, it show the user the screen with the slider so they can choose the number of bad apples and play again. I will attempt to further debug this
1 year ago
Thanks for the reply. I managed to solve this by just drawing different states of the walls and then resizing and placing them onto the window.
1 year ago
My program features other files apart from Panel.java hence you may view the entire project on my github. The link is here: https://github.com/zkhan122/snakeboost
1 year ago
I have said that I need to use KeyListener. Can you provide help based on those guidelines?
1 year ago
I am making a game which develops on top of the original Snake game. The problem is that when the user snake hits a purple apple or the wall or it's own snake body, the game ends. To restart the game, I have initialized the user to press SPACE in order to restart and it is not responding on press. This was working before I made changes to the drawWalls() method but I have tried backtracking to the source of the bug but I have had no luck so far. I know I should be using KeyBindings instead of KeyListener but I need to specifically use KeyListener for this project. Any help will be appreciated. Also if an MCVE needs to be created I would like advice on how to condense this to do so.

I have also provided images of both in game and when the user needs to press SPACE to restart to where it will navigate to. I have taken out some code to attempt to simplify the post.

Code:

1 year ago

Les Morgan wrote:Zayaan,

please take note of Campbell's comments: in object oriented development you should keep your GUI separate from your process as much as possible, they are really very different.  your GUI is just that, the Graphical User Interface, the view of what is happening in your game--it is only a view and not the game play itself.  if done right, it becomes the animation engine that you can drop any amount of diverse graphics objects into it and use it for a base for many different projects.

As far as your transforms for walls, as others have said: why?  many, including myself champion the ease of having a separate image for each wall type you will have in your game, don't over complicate things, keep it simple and the game will play faster and have less resource conflicts.

As far as your image on a label: if it works for you, then go for it.  it's my preference to have 1 image in a JPanel for the GUI and write the portions of the grid that you need to change.  yes, there is some elementary Math there to keep it all strait, but imo, you get a cleaner game out of it.  you have one image you work on and swap into the rendering process as it is ready.  the grid is only defined mathematically across your large image and your graphic objects are easily overlaid onto the image to be rendered.

Les



Ah ok so you suggest having different images for each wall state and placing them correctly on the grid, that makes perfect sense, I think I was making it too complex. On the other hand, if you wouldn't mind, can you elaborate on keeping my GUI seperate? I thought I had done so as I have a Panel class for holding the GUI and game processes, the Main class to call a Panel object to actually render the window and start the game.
1 year ago

Rob Camick wrote:

That sounds an inefficient way to do it...



Not understanding your comment.

The code is using an Image to represent a wall. Not sure what a Rectangle2D has to do with an image.

My suggestion is to create one horizontal image/icon and one vertical image/icon. Then you just paint the Icon in the required location.



How would I tweak my logic for the for loop (drawing the images) to instead go down vertically instead across horizontally as I have already done?
1 year ago

Campbell Ritchie wrote:Of course, you can have long thin rectangles to represent vertical barriers and turned the other way to repesent horizontal barriers, so we get the same conclusion that we have made the rotation redundant.



Thank you for the reply. Yes I can do that but I am wanting to use images rather than painting long rectangles since better graphics factors will make it feel more like a game.
1 year ago

Campbell Ritchie wrote:Please don't play the game in this code, nor in this class. . . .
The Graphics object passed is a subtype of Graphics2D, . . . .



Ok noted. I will just use Graphics to achieve this since it is more straightforward
1 year ago

Rob Camick wrote:Note, in your looping code you do NOT need create a new image/icon for every label you create. . . .rem create a single rotated image of the horizontal image than can then be used as a vertical wall?



Yes, I am wanting to use a single image since the walls are going to be same horizontally and vertically .
1 year ago
Hi. I am creating walls for my game. I am able to place the image inside the JLabel horizontally across the window using a loop however I want to also place the image vertically downwards hence rotate it 90 degrees. However, researching into e.g. AffineTransformation is a complex solution and I was wondering if there was a simple solution to achieve this. I have also seen rotation solutions using Graphics2D.rotate() but is there a way to do this without using Graphics?

I have attempted to add images to show what I am trying to achieve as well as my current attempt at achieving the rotation. Importantly, I would like to make a copy of the initial walls placed horizontally (shown in image attatched) and rotate the copy.

Code:


Edit: I originally posted the question in the wrong forum so I reposted it in this forum.
1 year ago

Rob Camick wrote:

but I just want to have many circles displayed at different (x, y) coordinates



And you were given a complete working example in your last question showing how to use an ArrayList to hold all the objects you want to paint

then the KeyListener stops working and the snake cannot move.



You should NOT be using a KeyListener. A KeyListener only works when a component has focus. When you click on the button your drawing panel loses focus. Instead you should be using Key Bindings. Key Bindings allow you to use the keyboard even when a component doesn't have focus. Read the Swing tutorial on How ot Use Key Bindings for more information.



Is there not a way to reintialize focus for the KeyListener? Also how would I transition from KeyListener to KeyBindings?
1 year ago