程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python practice -- using pyGame to build a game initial interface (1)

編輯:Python

I learned it for a while python, It still needs some practical links to deeply understand , After all, he is a novice Xiaobai , Just do some simple games to give yourself some encouragement , Ha ha ha , Don't say the , Serve .

  • Create a blue background python window
    The first use of pycharm Create a project file , The best way to name it is to choose the one related to the project , See the name and know the meaning .
    First create an empty pygame Window and set its background color ( Background color can be customized , Color RGB Get it through Baidu )

main_color.py

# The import module sys and pygame
import sys
import pygame
def run_game():
pygame.init()# Initialize background settings 
screen = pygame.display.set_mode((800,600))
pygame.display.set_caption('blue shy')
bg_color = (135,206,235)# Set background color , Azure blue 
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:# Detect that the player clicks the game window close button 
sys.exit()# Quit the game 
# Redraw the screen every time you cycle 
screen.fill(bg_color)
# Make the most recently drawn screen visible 
pygame.display.flip()
run_game()

Method pygame.event.get() Is used to detect user events , Determine what the user is doing .
Method screen.fill() Fill the screen with background color .
The code contains the following procedures :

  1. Module import ( The modules we need to use in the following game settings , Here are two , modular pygame Including the functions required to develop the game , modular sys To exit the game )
  2. Initialize the game and create a screen object ( Set screen properties )
  3. Start the game cycle ( Detect user actions , Update screen )


The result is shown in the figure , The size of the window can be set according to your own needs .
If there are many functions to be added later , You can consider creating some classes , Store different functions separately , In this way, it will not appear cluttered when a large number of function changes are required , The code is also more readable . There is no need to implement a large number of functions here , You don't have to create a separate class to hold the code .

  • Place an image in the center of the screen , And set the background color to the same
    Add an image to the screen , Create a ship class , Used to manage image settings , Look at the code .
    ship.py
import pygame
class Ship():
def __init__(self, screen):
''' Initialize the image and set its initial position '''
self.screen = screen
# notes : Define methods __init__().( Contains two formal parameters :self, screen . Method __init__() Accept the values of these parameters , And store them in the instance properties created from this class )
# Load the image and get the bounding rectangle of the image and screen 
self.image = pygame.image.load("C:\python Project documents \ practice 001\images\Bee.bmp")
self.image_rect = self.image.get_rect()
self.screen_rect = screen.get_rect()
# Place the spaceship at the designated position 
self.image_rect.centerx = self.screen_rect.centerx
self.image_rect.centery = self.screen_rect.centery
def blitme(self):
''' Draw the image at the specified position '''
self.screen.blit(self.image, self.image_rect)# Define methods blitme(), according to self.image_rect Draw the image to the screen at the specified position 

notes : Define methods __init__().( Contains two formal parameters :self, screen . Method __init__() Accept the values of these parameters , And store them in the instance properties created from this class ),__ It's two underscores , At first, I only typed one , I have been looking for a long time .

pygame in , origin (0,0) Located in the upper left corner of the screen .
Above , By means of ship Class code settings , The basic position of the image has been set , The next step is just to main_color.py The function can be realized by updating the code in .
Here is the updated code file : The updated location is followed by #***

import sys
import pygame
from ship import Ship#***
def run_game():
pygame.init()
screen = pygame.display.set_mode((400,300))
pygame.display.set_caption('blue shy')
# Create an image 
ship = Ship(screen)#***
bg_color = (135,206,235)# Set background color , Azure blue 
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:# Detect that the player clicks the game window close button 
sys.exit()# Quit the game 
# Redraw the screen every time you cycle 
screen.fill(bg_color)
ship.blitme() #***
# Make the most recently drawn screen visible 
pygame.display.flip()
run_game()

From these two small functional implementations , We use a lot of pygame Method in module , Directly call the methods already set in the module to realize the functions , It does provide a lot of convenience , So when you want to write a complicated game by yourself , It is essential to understand the function of each module and each method , Of course , It's written too much , Of course .
Take a look at the renderings :

Center Image , Background color is out of tune , When selecting a picture, the effect will be better if the background is transparent , The image format should be bitmap ( That is, the suffix is .bmp). Of course , The picture can also be moved up, down, left and right , It's not going to be written here , The blog to be updated later will say , In fact, these blogs have no technical level , Just take it as a note and add some of your own understanding , For future reference , By the way, you can deepen your impression .
Solo , Not as happy as others : Writing is fun , Comment is music , It's fun to see , Music is the most important .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved