It was said that pygame Create a window and put a bitmap (.bmp) Put it in the center , Next, we will move the bitmap up, down, left and right , Here for better results , Make some adjustments to the game window screen interface , The picture is also replaced by a picture of a rocket , The code has also made some minor adjustments , as follows :
main_game.py
main_game.py It is mainly used to manage the game process , Initialize the game , Start the main game loop by calling various classes about the game .
import pygame
import sys
from settings import Settings
from ship import Ship
def run_game():
# Initialize the game and create a screen object
pygame.init()
mg_settings = Settings()# Import settings class
screen = pygame.display.set_mode((mg_settings.screen_w, mg_settings.screen_h))
pygame.display.set_caption(" The rockets ")
# Create a spaceship
ship = Ship(screen)
# Start the main cycle of the game
while True:
# Monitor keyboard and mouse events
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
# Redraw the screen every time you cycle
screen.fill(mg_settings.screen_color)
ship.blitme()
# Make the most recently drawn screen visible
pygame.display.flip()
run_game()
settings.py
settings.py Is a class created to manage game settings , Put the settings for all games in it , When modifying game settings, you can modify them directly , Avoid tedious search and a large number of modifications .
# Create a separate class to store the interface settings required for the game
class Settings():
def __init__(self):
self.screen_w = 400# Screen width
self.screen_h = 300# Screen height
self.screen_color = 255,255,255# screen color
ship.py
ship.py Is used to manage added bitmaps , That is, a class of rockets on the screen
import pygame
class Ship():
# Create a ship class , Most of the actions of managing rockets
def __init__(self, screen):
self.screen = screen
# Load the rocket image and get the circumscribed rectangle of the rocket
self.image = pygame.image.load("C:\python Project documents \ practice 002\image\huojian.bmp")
self.image_rect = self.image.get_rect()
self.screen_rect = screen.get_rect()
# Put each rocket in the center of the screen
self.image_rect.centerx = self.screen_rect.centerx
self.image_rect.centery = self.screen_rect.centery
def blitme(self):
# Draw the spacecraft at the designated location
self.screen.blit(self.image, self.image_rect)
The following time effect diagram :( It's much more beautiful than the last one )
Okay , The following is how to realize the up and down movement of the rocket , It also involves code refactoring , More concise after refactoring , Later, in the section on code refactoring, I will not point out the changes one by one ( After all, I am also a vegetable chicken ).
The next step is to press the button to move the plane up, down, left and right , Monitor button and mouse events for In circulation , adopt if Judge statements to judge mouse events , meanwhile , You can also add elif Statement to respond to keyboard events , Put the functions that respond to key and mouse events into a check_events() Function , as follows :
def check_enents(ship):
''' Respond to key and mouse events '''
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:# Determine whether there is a key pressed on the keyboard
if event.key == pygame.K_RIGHT:# Judge the pressed key value
# Move the ship to the right
ship.image_rect.centerx += 1
adopt if event.key == pygame.K_RIGHT The condition judgment statement detects the key value pressed by the key , If it is K_RIGHT Just ship.image_rect.centerx Add one to the value of , That is to add the center of the image x Move the coordinates one bit to the right , If I'm moving to the left , be -1. Again , Move up or down , It's also the vertical coordinate y Add and subtract .
Defining functions in check_enents(ship) when , Used Shape parameter ship, stay main_game.py in , Update call check_events() Code , take ship As an argument Pass it on :
gf.check_enents(ship)
Complete the above , The spaceship can move , But the rocket can only move by tapping the keyboard all the time , And the speed is very slow , What we want is when the key is pressed as long as it is not released , The rocket keeps moving , Until released , Here you can set an event , adopt if sentence , As long as this event is true , Always execute the shift right command , conversely , Do not perform . Moving in other directions is the same as moving to the right .
The code is as follows :
main_game.py
import pygame
from settings import Settings
from ship import Ship
import game_func as gf
def run_game():
# Initialize the game and create a screen object
pygame.init()
mg_settings = Settings()
screen = pygame.display.set_mode((mg_settings.screen_w, mg_settings.screen_h))
pygame.display.set_caption(" The rockets ")
# Create a spaceship
ship = Ship(screen)
# Start the main cycle of the game
while True:
gf.check_enents(ship)# Detect keyboard events
ship.update()# Update the screen when a keyboard event is detected
gf.update_screen(mg_settings, screen, ship)
run_game()
ship.py
import pygame
class Ship():
# Create a ship class , Most of the actions of managing rockets
def __init__(self, screen):
self.screen = screen
# Load the rocket image and get the circumscribed rectangle of the rocket
self.image = pygame.image.load("C:\python Project documents \ practice 002\image\huojian.bmp")
self.image_rect = self.image.get_rect()
self.screen_rect = screen.get_rect()
# Put each rocket in the center of the screen
self.image_rect.centerx = self.screen_rect.centerx
self.image_rect.centery = self.screen_rect.centery
# Mobile logo
self.moving_right = False
self.moving_left = False
self.moving_up = False
self.moving_down = False
def update(self):
''' Respond to the position of the spacecraft according to the moving signs '''
if self.moving_right:
self.image_rect.centerx += 1
if self.moving_left:
self.image_rect.centerx -= 1
if self.moving_up:
self.image_rect.centery -= 1
if self.moving_down:
self.image_rect.centery += 1
def blitme(self):
# Draw the spacecraft at the designated location
self.screen.blit(self.image, self.image_rect)
settings.py
# Create a separate class to store the interface settings required for the game
class Settings():
def __init__(self):
self.screen_w = 400
self.screen_h = 300
self.screen_color = 255,255,255
game_func.py
# Store a large number of functions used to make the game run
import pygame
import sys
def check_enents(ship):
''' Respond to key and mouse events '''
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:# Determine whether there is a key pressed on the keyboard
if event.key == pygame.K_RIGHT:# Judge the pressed key value
# Move the ship to the right
ship.moving_right = True
elif event.key == pygame.K_LEFT:# Judge the pressed key value
# Move the ship to the right
ship.moving_left = True
elif event.key == pygame.K_UP: # Judge the pressed key value
# Move the ship up
ship.moving_up = True
elif event.key == pygame.K_DOWN: # Judge the pressed key value
# Move the ship down
ship.moving_down = True
elif event.type == pygame.KEYUP:# Judge whether the key is released
if event.key == pygame.K_RIGHT:
ship.moving_right = False
elif event.key == pygame.K_LEFT:
ship.moving_left = False
elif event.key == pygame.K_UP:
ship.moving_up = False
elif event.key == pygame.K_DOWN:
ship.moving_down = False
def update_screen(mg_settings, screen, ship):
''' Update image on screen , And switch to the new screen '''
# Redraw the screen every time you cycle
screen.fill(mg_settings.screen_color)
ship.blitme()
# Make the most recently drawn screen visible
pygame.display.flip()
Game interface :
The problem here is that the rocket will move out of the screen and move too fast ( Because my interface settings are relatively small , So it seems to be fast ), These problems will be solved later .
One man is happy but not many , Writing is fun , It's fun to see , Comment is music .