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

Python practice -- creating a game interface with pyGame (3)

編輯:Python

The two questions left before , Because the game interface settings are relatively small , The rocket will appear faster as it moves , It just moves out of the interface , What needs to be improved is the speed of the rocket and the setting of the screen edge , The rocket stops moving when it reaches the edge of the screen .
The rocket needs to be set up in settings Class , Add a rocket speed attribute here :

self.ship_speed = 0.5

Define a new attribute that can store floating-point data , It is used to store the position information and transform the position information according to the speed , Finally, the position information is transmitted to update the position attribute of the control spacecraft .( When it is passed back, the data type is different , The decimal part will be automatically removed , But it's not a big problem , There's no need to study here )

# In spaceship properties center Small value stored in , Transfer location information 
self.centerx = float(self.image_rect.centerx)
self.centery = float(self.image_rect.centery)

Don't let the rocket move out of the screen , Just monitor the coordinates of the outer rectangular edge of the spacecraft , Keep it from touching the screen edge coordinates , adopt if Judge statements to detect , Not only should keyboard events be detected , Also determine whether the rocket position is in contact with the edge of the screen , Only when both are satisfied can the move command be executed , adopt self.centerx,self.centery To change the position , Finally, pass the value back , Update the value of the location .

 def update(self):
''' Respond to the position of the spacecraft according to the moving signs '''
if self.moving_right and self.image_rect.right < self.screen_rect.right:
self.centerx += self.mg_settings.ship_speed
if self.moving_left and self.image_rect.left > 0:
self.centerx -= self.mg_settings.ship_speed
if self.moving_up and self.image_rect.top >0:
self.centery -= self.mg_settings.ship_speed
if self.moving_down and self.image_rect.bottom < self.screen_rect.bottom:
self.centery += self.mg_settings.ship_speed
# according to self.center to update rect object 
self.image_rect.centerx = self.centerx
self.image_rect.centery = self.centery

( The coordinate value in the upper left corner of the screen (0,0))

Set the bullet

We can set the bullet class , When we press a specific key value , Rockets can fire bullets .
Bullets can be used directly pygame.Rect() Or is it pygame Other methods in the package , But when you need to create a specific image , You can import pictures as bullets to shoot , It looks more comfortable . The approximate method is the same as that of the rocket , But the position is at the top center of the rocket :

 self.image_bullet_rect.centerx = ship.image_rect.centerx
self.image_bullet_rect.top = ship.image_rect.top

Create a bullet Class to store bullet settings separately . Don't talk much , Look at the code !
bullet.py

import pygame
from pygame.sprite import Sprite
class Bullet(Sprite):
''' Create a class to manage the spacecraft '''
def __init__(self, mg_settings, screen, ship):
''' Create a bullet object where the ship is located '''
super(Bullet, self).__init__()
self.screen = screen
# Load the missile image and get the circumscribed rectangle 
self.image_bullet = pygame.image.load("C:\python Project documents \ practice 002\image\ missile .bmp")
self.image_bullet_rect = self.image_bullet.get_rect()
self.image_bullet_rect.centerx = ship.image_rect.centerx
self.image_bullet_rect.top = ship.image_rect.top
# Store the position of the bullet in decimal 
self.y = float(self.image_bullet_rect.y)
self.bullet_speed = mg_settings.bullet_speed
def update(self):
''' Move the bullet up '''
# Update the decimal value indicating the moving position of the bullet 
self.y -= self.bullet_speed
# Update indicates bullets rect The location of 
self.image_bullet_rect.y = self.y
def draw_bullet(self):
''' Draw bullets on the screen '''
self.screen.blit(self.image_bullet, self.image_bullet_rect)

Other modules are basically unchanged , The relevant codes changed are as follows :
main_game.py

 # Create a spaceship 
ship = Ship(mg_settings, screen)
# Create a group to store bullets 
bullets = Group()
# Start the main cycle of the game 
while True:
gf.check_enents(mg_settings, screen, ship, bullets)# Detect keyboard events 
ship.update()# Update the screen when a keyboard event is detected 
bullets.update()
gf.update_screen(mg_settings, screen, ship, bullets)
run_game()

game_func.py

def check_enents(mg_settings, screen, ship, bullets):
''' Respond to key and mouse events '''
......
elif event.key == pygame.K_SPACE:
# Create a bullet , And add it to the group bullets in 
new_bullet = Bullet(mg_settings, screen, ship)
bullets.add(new_bullet)
......
def update_screen(mg_settings, screen, ship, bullets):
''' Update image on screen , And switch to the new screen '''
# Redraw the screen every time you cycle 
screen.fill(mg_settings.screen_color)
# Redraw all bullets behind aliens and spaceships 
for bullet in bullets.sprites():
bullet.draw_bullet()
ship.blitme()
# Make the most recently drawn screen visible 
pygame.display.flip()

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
self.ship_speed = 0.3
# Bullet speed setting 
self.bullet_speed = 0.1

Through the above changes , The rocket can move up, down, left and right without moving off the screen , Press the space bar to fire bullets .
The renderings are as follows :

Later, various effects of the game interface will be updated , Add game features , such as ,,,
Ask me what I have , There are only white clouds in the mountains . I can only enjoy myself , I can't bear to give it to you : Part of the reason is beneficial to me , Not necessarily universal . Limited by some viewpoints , It is hard to avoid being biased and subjective . Part of the writing is unequivocal , It does not mean that we firmly believe in its sole correctness , I just don't want to be all inclusive and ambiguous . If you have any mistakes or opinions, please correct them !
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