I used Python to write games. Getting started with pygame (5): Object-oriented game design, pythonpygame
There was a Moving bullet in yesterday's content. Although we only added one bullet, you can see that we need to record the x and y coordinates of the bullet and update its coordinates each time. If we want to have multiple bullets, We need to store multiple coordinates. At that time, the processing was not so simple. Maybe we can use two lists, one storing the x coordinates of each bullet, and the other storing the y coordinates of the bullet. The problem seems to be less complicated, it is easier to write. However, we have not yet joined the enemy plane. If we joined the enemy plane and joined the design of other things, we would need to store a lot of different data. Although a programmer with clear thinking can remember the list in which all coordinates are stored, this is troublesome after all. So what is the solution?
This requires the data abstraction method of the program language, that is, the object-oriented abstraction method, which allows us to better process data.
The key to object-oriented is encapsulation. Let's take a look at how to encapsulate a bullet.
The most important thing to describe a bullet is its image and coordinate position. It requires so much, there is also a piece of code for handling the bullet display.
# Define a Bullet class that encapsulates the data and methods of bullets. class Bullet (object): def _ init _ (self): self. x = 0 self. y =-100 self. speed= 600 self. image = pygame. image. load (bullet_image_filename ). convert_alpha () def move (self, passed_time_second): if self. y <0: mouseX, mouseY = pygame. mouse. get_pos () self. x = mouseX-self. image. get_width ()/2 self. y = mouseY-self. image. get_width ()/2 else: self. y-= self. speed * passed_time_seconds
This code is exactly the same as it was written yesterday, but it is written in an object-oriented way. Then let's change the code from yesterday:
#-*-Coding: utf8-*-background_image_filename = 'background.png 'mouse _ image_filename = 'hero.png' bullet _ image_filename = 'bullet.png '# specify the image file name import pygame # import the pygame library from sys import exit # Borrow from sys module an exit function is used to exit the Program # define a Bullet class, data and methods for encapsulating bullets class Bullet (object): def _ init _ (self): self. x = 0 self. y =-100 self. speed= 600 self. image = pygame. image. load (bullet_image_filename ). convert_alpha () def move (sel F, passed_time_second): if self. y <0: mouseX, mouseY = pygame. mouse. get_pos () self. x = mouseX-self. image. get_width ()/2 self. y = mouseY-self. image. get_width ()/2 else: self. y-= self. speed * passed_time_secondpygame.init () # initialize pygame to prepare for using hardware screen = pygame. display. set_mode (480,650), 0, 32) # create a window pygame. display. set_caption ("PlaneFight! ") # Set the window title pygame. mouse. set_visible (False) background = pygame. image. load (background_image_filename ). convert () mouse_cursor = pygame. image. load (mouse_image_filename ). convert_alpha () # Load and convert the image bullet = Bullet () clock = pygame. time. clock () while True: # main game loop for event in pygame. event. get (): if event. type = pygame. QUIT: # exit the pygame program after receiving the exit event. quit () exit () time_passed = clock. tick (100) time_passed_second = time_passed/1000.0 screen. bground (background, () # Add the background image x, y = pygame. mouse. get_pos () # obtain the mouse position bullet. move (time_passed_second) # move the bullet x-= mouse_cursor.get_width ()/2 y-= mouse_cursor.get_height ()/2 # Calculate the screen position in the upper left corner of the cursor. BITs (bullet. image, (bullet. x, bullet. y) screen. BITs (mouse_cursor, (x, y) # draws the element to pygame. display. update () # refresh the screen
In this way, we have mastered how to create a bullet.
We use the same idea to consider how to create an enemy plane.
The data to be encapsulated by the enemy plane, mainly images and coordinates. And encapsulate a method to handle its coordinate changes.
Then we can define the class of the enemy:
Class Enemy (object): # defines an Enemy class, which encapsulates data and methods of Enemy hosts. def _ init _ (self): self. x = 200 self. y =-50 self. speed= 200 self. image = pygame. image. load (enemy_image_filename ). convert_alpha () def move (self, passed_time_second): if self. y< 650: self. y + = self. speed * passed_time_second else: self. y =-50
Then instantiate and display the image in the same way:
Like a bullet, the enemy plane shows it.
However, there is a very obvious problem here, that is, the enemy plane flew from the middle of the plane to the middle of the bottom every time, a lot different from the real game, how can we make it changeable?
Here we can import another common library, that is, random.
from random import randint
Add this sentence at the beginning and introduce randint (). This method is used to generate a random integer. You can give it two parameters as the lower bound and the upper bound respectively. The lower bound cannot be larger than the upper bound.
Then we rewrite the Enemy class to this:
Class Enemy (object): # defines an Enemy class, which encapsulates data and methods of the Enemy server def restart (self): self. x = randint (-30,400) self. y = randint (-100,-50) self. speed = randint (100,400) def _ init _ (self): self. restart () self. image = pygame. image. load (enemy_image_filename ). convert_alpha () def move (self, passed_time_second): if self. y< 650: self. y + = self. speed * passed_time_second else: self. restart ()
In this way, the speed and location of the enemy server will change, and it will not seem so dull.
The overall code is as follows:
#-*-Coding: utf8-*-encoding = 'background.png 'mouse _ image_filename = 'hero.png' bullet _ image_filename = 'bullet.png 'enemy _ image_filename = Shanghai' # specify the image file name import pygame # import the pygame library from sys import exit # use an exit function from the sys module to exit the program from random import randint # introduce a random number # define a Bullet class, data and methods for encapsulating bullets class Bullet (object): def _ init _ (self): self. x = 0 self. y =-100 self. speed= 600 self. image = py Game. image. load (bullet_image_filename ). convert_alpha () def move (self, passed_time_second): if self. y <0: mouseX, mouseY = pygame. mouse. get_pos () self. x = mouseX-self. image. get_width ()/2 self. y = mouseY-self. image. get_width ()/2 else: self. y-= self. speed * passed_time_secondclass Enemy (object): # define an Enemy class to encapsulate data and methods of the Enemy's machine def restart (self): self. x = randint (-30,400) self. y = randint (-100,-50) self. sp Eed = randint (100,400) def _ init _ (self): self. restart () self. image = pygame. image. load (enemy_image_filename ). convert_alpha () def move (self, passed_time_second): if self. y< 650: self. y + = self. speed * passed_time_second else: self. restart () pygame. init () # initialize pygame to prepare for using hardware screen = pygame. display. set_mode (480,650), 0, 32) # create a window pygame. display. set_caption ("PlaneFight! ") # Set the window title pygame. mouse. set_visible (False) background = pygame. image. load (background_image_filename ). convert () mouse_cursor = pygame. image. load (mouse_image_filename ). convert_alpha () # Load and convert the image bullet = Bullet () enemy = Enemy () clock = pygame. time. clock () while True: # main game loop for event in pygame. event. get (): if event. type = pygame. QUIT: # exit the pygame program after receiving the exit event. quit () exit () time_passed = clock. tick (100) time_passed_second = time_passed/1000.0 screen. bground (background, () # Add the background image x, y = pygame. mouse. get_pos () # obtain the mouse position bullet. move (time_passed_second) # move the bullet enemy. move (time_passed_second) x-= mouse_cursor.get_width ()/2 y-= mouse_cursor.get_height ()/2 # Calculate the screen position in the upper left corner of the cursor. bemy (enemy. image, (enemy. x, enemy. y) screen. BITs (bullet. image, (bullet. x, bullet. y) screen. BITs (mouse_cursor, (x, y) # draws each element to pygame. display. update () # refresh the screen
Today, in addition to changing the Program Method to object-oriented, there is basically nothing to talk about.
At present, there is only one plane, only one bullet, and only one enemy plane. It does not feel the power of object-oriented, but when there are many planes, you will think that object-oriented abstraction is really easy.
Tomorrow I will show you how to use multiple planes.
Because I have many things recently, this update is also very slow, and I only talk a little bit every day. It is too slow for people who are trying to do it every day. But I keep updating every day until I build the game.
A friend who thinks I am talking slowly has two ways: one is to try to check some information, the other is to ignore my updates and check again in a week, then we can see a lot of changes.
Recently, I was too busy to get in one breath. Sorry.