Informatics 9. Билингвальный учебник | страница 38



3. Change LEFT, RIGHT, UP, DOWN keys to A, D, W, S keys.


Terminology

coordinates - координаттары - координаты

retrieve - шығарып алу - извлекать

position – позициясы - положение

edge – жиегі - край

flaws – кемшіліктер - недостатки

internal – ішкі - внутренний

unresponsive – жауап бермейді - не реагирующий

to handle - орындау - обрабатывать


4.6 PROGRAMMING GAME CONDITIONS

You will:

Define and create classes;

learn to program arcade game with the ready scenario.


What is the most important aspect of video games today: story, graphics, or gameplay?

Introduction to Classes

Classes and objects are very powerful programming tools. They make programming easier. In fact, you are already familiar with the concept of classes and objects. A class is a “classification” of an object. Like “person” or “image.” An object is a particular instance of a class. Like “Mary” is an instance of “Person.”

Objects have attributes, such as a person’s name, height, and age. Objects also have methods. Methods define what an object can do, like run, jump, or sit.


Defining and Creating Simple Classes

A better way to manage multiple data attributes is to define a structure that has all of the information. Then we can give that “grouping” of information a name, like Character. This can be easily done in Python and any other modern language by using a class.


Example 1

# This is a class that represents the main

# character in a game.

class Character():

# This is a method that sets up the variables

# in the object.

def __init__(self):

self.name = “Link”

self.max_hit_points = 50

self.current_hit_points = 50

self.max_speed = 10


The def __init__(self): in a special function called a constructor that is run automatically when the class is created.


Adding Methods to Classes

A method is a function that exists inside of a class. The code below adds a method for a dog barking.

class Dog():

def __init__(self):

self.age = 0

self.name = “”

self.weight = 0

def bark(self):

print(“Woof”)


Method definitions in a class look almost exactly like function definitions. The big difference is the addition of a parameter self. The first parameter of any method in a class must be self. This parameter is required even if the function does not use it.


Example 2

This example code could be used in Python/Pygame to draw a ball. Having all the parameters contained in a class makes data management easier. The diagram for the Ball class is shown in Figure 1.