I used Python to write a game. Getting started with pygame (7): Collision Detection and pythonpygame

Source: Internet
Author: User

I used Python to write a game. Getting started with pygame (7): Collision Detection and pythonpygame

We have already completed most of the aircraft wars, but there is still no way to officially launch the game, because the bullets cannot be knocked out of the plane. Only after this is done can the game be basically formed.

Today's content is very simple, that is, the collision detection, and control the disappearance of bullets and planes. The sprite module in pygame contains the collision detection part. However, we can implement a collision detection function by ourselves here.

 

The collision detection method is very simple, that is, the position of the bullet is inside the plane image. Because bullets and planes are faster, they do not need to be accurately identified.

We think that if the coordinates of the bullet (B. x, B. y) within the image range of the plane, that is, (e. x, e, y) to (e. x + e. width, e. y + e. height. (Here we need to recall that the coordinates of our bullets are the center of the bullet image, and the coordinates of the enemy plane are the coordinates in the upper left corner of the image)

Therefore, collision detection is:

E. x <B. x <e. x + e. width

E. y <B. y <B. y + B. height

If the bullet is determined to have reached the enemy plane, the only thing to do is to reset the bullet and the plane.

The code implementation is such a function:

def checkHit(enemy,bullet):    if enemy.x < bullet.x < enemy.x + enemy.image.get_width() \       and enemy.y < bullet.y < enemy.y + enemy.image.get_height():        enemy.active = False        bullet.active = False        return True    else:        return False

This function can be used to detect collisions. Then, we change the code segment in the original main loop to the following:

For B in bullet: if B. active: B. move (time_passed_second) # move the bullet for e in enemy: if e. active: checkHit (e, B) screen. BITs (B. image, (B. x, B. y) # Show bullets

Then run your code and you will see it. The bullet can take off the plane. Of course, you have all found out. This effect is abrupt. After that, I will add an animated display on it, and the entire image will be less abrupt.

 

Now the bullet can be used to knock down the enemy plane, but the enemy plane cannot hit its own plane. We can write another collision detection between the enemy plane and its own plane.

Here, we use another method for Collision Detection:

After the distance between two points is smaller than a certain value, we think it is a collision. The distance formula is used to calculate the distance between two points. Do not review it here.

def checkCrash(enemy):    plane_x, plane_y = pygame.mouse.get_pos()    enemy_x, enemy_y = enemy.x, enemy.y    enemy_x += enemy.image.get_width()/2    enemy_y += enemy.image.get_height()/2    distance = sqrt(float(plane_x - enemy_x)**2 + float(plane_y - enemy_y)**2)    if distance < 75:        return True    else:        return False

Here sqrt () is used to open the square, so we need to add a sentence from math import sqrt

This method is quite useful for collision of objects with a circular shape, but it is still slightly poor for our aircraft shape. You can consider the specific method.

 

Most of the things we have done now can be captured or destroyed by planes. Then we should add some content to make it look richer. For example, for a scoring module, for example, the display ends after the game ends, and the module is selected again.

I will not talk about these things for the moment. You can implement them by yourself. I will give you a rough framework tomorrow.

Because the program has been relatively long, it will not be sent. Wait until you finish writing the function.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.