pygame making aircraft war 3 - edge detection and bullet firing

1. Edge detection

After the aircraft can move freely, it needs to do edge detection to avoid moving to the coordinates outside the pop-up window. The origin coordinate of pygame is in the upper left corner, and the size of my window is (450550), so it only needs to change the corresponding coordinates into the value corresponding to the edge when it is detected that the aircraft is going to fly out of the four coordinate ranges (0, y), (x, 0), (450, y), (x, 550). The definition function is judge, and the code is as follows:

def judge(potx, poty):
    if potx >= 426:
        potx = 426
    elif potx <= -24:
        potx = -24
    elif poty >= 526:
        poty = 526
    elif poty <= -24:
        poty = -24
    return potx, poty

The reason for the adjustment to 426, 526 and - 24 is that visually, the center of the aircraft is just at the edge of the interface at this time, which is more in line with the actual appearance in the game (of course, some game aircraft can not exist as a half body, which can be adjusted according to preferences)

2. Bullet firing

By splitting the bullet firing steps, we can know that the aircraft will generate a bullet every time, and then the bullet will move directly upward to the edge of the picture. Therefore, three small steps need to be completed here:

(1) Time interval for preparing for bullet launch;

(2) Set the number of steps for each upward movement of the bullet to ensure that it can reach the top of the pop-up window when fired from the aircraft;

(3) Draw the bullet in the second step into the interface.

    if i == 0:
        button_x1 = x1 + 3
        button_y1 = y1 + 60
    elif i == 90:
        i = -10
    i += 10
    button_y1 -= 65

x1+3 and y1+60 are to adjust the firing position of bullets and ensure that they are emitted from the firing port on the aircraft wing. i==90 and i+=10 are to increase 9 times from 0. 9 * code refresh time = the time interval between the firing of two different bullets, and button_y1 -= 65 is the number of steps the bullet moves upward each time. The greater the step value, the faster the bullet speed. The aircraft has two launchers, so another bullet is set (of course, x1 + 27 sets the initial position to another launchers):

    if j == 0:
        button_x2 = x1 + 27
        button_y2 = y1 + 60
    elif j == 90:
        j = -10
    j += 10
    button_y2 -= 65

Bullets move upward directly, and the number and direction of bullets in most aircraft wars can be changed according to the capture of different props. This will be designed later. If you are interested, you can also adjust it directly. For example, eight bullets are fired at different positions at a time, and two bullets are fired at the same position at a time to see the different effects, The complete code of this time is as follows (PS: some function implementations may not use too many function calls. The purpose is to run first and continue to optimize later):

#Parameter initialization
background_image_filename = 'background.png'
mouse_image_filename = 'airplane.png'
button_image_filename = 'button1.png'

#Import function
import pygame
from pygame.locals import *
from sys import exit

#Load and convert images
background = pygame.image.load(background_image_filename)
mouse_cursor = pygame.image.load(mouse_image_filename)
button_picture1 = pygame.image.load(button_image_filename)
button_picture2 = pygame.image.load(button_image_filename)

#Create window and title
screen = pygame.display.set_mode((450, 550), 0, 32)
pygame.display.set_caption("hello world")

#Initialize pygame to prepare for hardware use
pygame.init()

x1 = 225
y1 = 275
#Bullet coordinate initialization
button_x1 = 0
button_y1 = 0
button_x2 = 0
button_y2 = 0
v = 4
i = 0
j = 0

# Edge detection
def judge(potx, poty):
    if potx >= 426:
        potx = 426
    elif potx <= -24:
        potx = -24
    elif poty >= 526:
        poty = 526
    elif poty <= -24:
        poty = -24
    return potx, poty

#Game main loop
while True:
    # Detect the exit event and judge whether to exit
    for event in pygame.event.get():
        if event.type == QUIT:
            exit()

    #Draw the background
    screen.blit(background, (0, 0))

    #Get the key status and complete the movement
    press = pygame.key.get_pressed()

    if press[K_LEFT] == True:
        if press[K_UP] == True:
            x1 = x1 - v
            y1 = y1 - v
        elif press[K_DOWN] == True:
            x1 = x1 - v
            y1 = y1 + v
        else:
            x1 = x1 - v
    elif press[K_RIGHT] == True:
        if press[K_UP] == True:
            x1 = x1 + v
            y1 = y1 - v
        elif press[K_DOWN] == True:
            x1 = x1 + v
            y1 = y1 + v
        else:
            x1 = x1 + v
    elif press[K_UP] == True:
        y1 = y1 - v
    elif press[K_DOWN] == True:
        y1 = y1 + 4
    # Get mouse position
    #x, y = pygame.mouse.get_pos()
    #Calculates the position of the cursor in the upper left corner
    #x -= mouse_cursor.get_width() / 2
    #y -= mouse_cursor.get_height() / 2

    #Bullet movement
    #Bullet 1 moves
    if i == 0:
        button_x1 = x1 + 3
        button_y1 = y1 + 60
    elif i == 90:
        i = -10
    i += 10
    button_y1 -= 65
    #Bullet 2 moves
    if j == 0:
        button_x2 = x1 + 27
        button_y2 = y1 + 60
    elif j == 90:
        j = -10
    j += 10
    button_y2 -= 65

    #Judge edge
    x1, y1 = judge(potx = x1, poty = y1)

    #Random enemies
    pass

    #Bullet hit
    pass

    #Show score
    pass

    #Draw the cursor
    screen.blit(mouse_cursor, (x1, y1))
    screen.blit(button_picture1, (button_x1, button_y1))
    screen.blit(button_picture2, (button_x2, button_y2))
    # Draw in new position
    pygame.display.update()

 

Tags: Python Game Development pygame

Posted by thomasd1 on Wed, 18 May 2022 07:21:36 +0300