Skip to Content
Java® Programming 24-Hour Trainer
book

Java® Programming 24-Hour Trainer

by Yakov Fain
February 2011
Intermediate to advanced
504 pages
11h 46m
English
Wrox
Content preview from Java® Programming 24-Hour Trainer

Chapter 12

Developing a Ping-Pong Game

The tic-tac-toe game developed in Lesson 11 was a simple one. My goal was to show you the basic layout of UI components and event processing. Now you’ll learn how to draw and move such objects as ovals, rectangles, and lines in a window. You’ll also learn how to process keyboard events. To add a little fun to these subjects, you’ll be learning all these things while creating a ping-pong game. This game will have two players: the user and the computer.

The Strategy

Let’s come up with the rules for our version of the ping-pong game:

1. The game lasts until one of the players (the user or the computer) reaches a score of 21. The computer’s racket is on the left.

2. The user’s racket movements will be controlled by the mouse.

3. The game score will be displayed at the bottom of the window.

4. A new game starts when a player presses the N key on the keyboard; the Q key ends the game; S serves the ball.

5. Only the user can serve the ball; the computer can’t.

6. For one of the players to win a point the ball must go beyond the opponent’s racket’s vertical line when the racket is not blocking the ball.

7. When the computer bounces the ball, the ball can move only horizontally to the right.

8. If the ball contacts the user’s racket in the upper half of the table, the ball should be moving up and left. If the ball was located in the bottom part of the table, it should move down and left.

It sounds like a very complicated task, but let’s split ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Java® Programming 24-Hour Trainer

Java® Programming 24-Hour Trainer

Yakov Fain

Publisher Resources

ISBN: 9780470889640Purchase bookAudio-VideoExamples