Thinking Object Oriented

Hey folks.

So for a school assignment, we had to create some kind of game with at least two self-defined classes while using object-oriented programming. However, I’m finding it quite hard to implement this OOP way of thinking. The game I created is just a complete copy of the Flappy Birds game.

The problem is, that i didn’t really find use for classes since i could just write all the code on the main page. I just got some feedback for the teacher and she said my code was very disorganized and that i needed an extra class to fulfill requirements.

Can anybody give me some tips or help me with how to organize my code and maybe come up with some ideas on what my two classes could be?

I uploadet the code at github above.

1 Like
  • OOP is basically an approach of how to organize code as objects.
  • Each object requires variables to describe its characteristics & current state.
  • Those variables become fields (a.K.a. properties) of the class which describes that object.
  • Then we write functions which deal w/ those field variables.
  • Those functions become methods of that object’s class.
  • Here’s a simple game that got classes for the player (Beaver), the grass animation (Grass), the player’s objective (Stick) and 1 more to save how many sticks the player caught (Score):
    sketchpad
  • And a beginner tutorial about OOP & classes:
    Lesson: Object-Oriented Programming Concepts (The Java™ Tutorials > Learning the Java Language)
4 Likes

I love the title to this discussion, “Thinking Object Oriented” because it says it all.

OO requires you to identify the entities (classes) that make up your application before you start writing source code. To retrofit classes into a procedural program is very messy and will probably involve you re-writing most of the original code.

I assume you have been taught the basics of classes and objects so I am not going to rehash it here but in the case of a Flappy Birds type program there are two obvious entities that could be represented as classes

  1. FlappyBird
  2. Obstacle (the entity to be avoided by the bird)
3 Likes

Object Oriented Programming is a way to organize your code into chunks / sections (classes).

Without it, variables (x,y…) and functions (drawBird())…) are all over the place.

With the classes you can sort it into classes. The class is cookie maker, the objects are the cookies:

  • So the class contains the properties of the cookies (color, position) and its functions (beWarm(), beEaten()).
  • The objects are the individual cookies, with different color, position and speed.

Make a class Bird and a class Obstacle. Not a class GameSetup.

Links

See also https://github.com/Kango/Processing-snippets/wiki/Object-Oriented-Programming

and https://github.com/Kango/Processing-snippets/wiki/Variables,-Arrays-and-object-oriented-programming

and https://www.processing.org/tutorials/objects/

Chrisir

3 Likes
2 Likes