Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9197947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:10:59+00:00 2026-06-17T22:10:59+00:00

i was writing a little snake like game and i was implementing the key

  • 0

i was writing a little snake like game and i was implementing the key event listener, so it listens when the arrow keys are pressed it increment or decrement the position of the oval in the frame. Below is my code, what do you think is happening? i tried googling for possible solution, but i turned out empty.

package snakegame;

import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;

/**
 *
 * @author PlaixHax0r
 */
public class SnakeGame extends JFrame{

    int x, y;

    public class ActionListener extends KeyAdapter{
        public void KeyPressed(KeyEvent a){

            int KeyCode = a.getKeyCode();

            if(KeyCode == KeyEvent.VK_RIGHT){
                x++;
            }

            if(KeyCode == KeyEvent.VK_LEFT){
                x--;
            }

            if(KeyCode == KeyEvent.VK_DOWN){
                y++;
            }

            if(KeyCode == KeyEvent.VK_UP){
                y--;
            }


        }

        public void KeyReleased(KeyEvent a){

        }

    }



    public SnakeGame(){
        addKeyListener(new ActionListener());
        setTitle("Snake 1.0");
        setVisible(true);
        setSize(500, 500);
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        x=150;
        y=150;

    }

    public void paint(Graphics g){
        g.drawString("Welcome to Snake Empire", 180, 50);
        g.fillOval(x, y, 15, 15);

        repaint();
    }


    public static void main(String[] args) {
        new SnakeGame();

    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:11:00+00:00Added an answer on June 17, 2026 at 10:11 pm

    Games are a different stuff (although some use swing to show a frame). These are the problems in your game.

    • You are calling repaint() inside paint(). It schedules another paint event before one actually completes. Calling it so many times would burst of your cpu. If you just need to move the oval, just call the repaint at the end of your keyPressed() method.

    • You had no control on timing. This leads to a condition where your game runs fast on a fast system and slow on slow system. Try searching for game loops

    • Many games use multiple threads. A logic thread which is separate from the EDT so as to prevent the CPU from hogging and let input be detected at the normal rate.

    • Avoid rendering directly onto a frame. It is recommended only if you want active rendering and create a BufferStrategy (though you won’t use the paint method).

    • You aren’t using double buffering. It causes the game to flicker.

    • Never directly change the positions of objects directly in input state. Just change their particular velocities and simple add them to the positions (Also use negative velocities) when updating to ensure that the objects are updated only at regular intervals.

    • And finally try to search for java game development tutorials. There’s a simple framework here (tutorial) which gives a good introduction to the subject. (It’s a snake game example).

    Get rid of the .NET naming convention in java (Starting method names with capitals)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a simple game - snake. I would like to have background and
I'm writing a little tile-based game, for which I'd like to support light sources.
I am writing a little game with the Java 3D API and I use
I'm writing a little Python extension in C/C++, and I've got a function like
I am writing a little plugin like this: (function($){ $.fn.extend({ myplugin: function (callback) {
I'm writing a little card game in which two players have 4 cards each
I'm writing a little chat app, and I have this event handler: void o_Typing(object
I'm writing a little Android game. It should display a random sequence of 4
I'm writing a little command line todo app that has a general git-like interface.
BACKGROUND I'm writing a little game using Java, slick2d and other frameworks. Slick2d does

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.