(The plane is the mini Yasuo, and the alien is the mini Noorhand. Due to the time and difficult to find the picture material, we only made a simple version without pasting the background picture. Due to space reasons, the explanation of the function is relatively brief, you can search for the use of the corresponding function)
The main use of PyGame built-in library for graphical processing
The main game interface is as follows:
Basic operation: Import pyGame module
-
Create a PyGame window with screen=pygame.display.set_mode() and set it to (width, height).
-
Then use Pygame.display.set_Caption () with the caption string as the name of the game you want, for example: “Airplane Fight”
-
Find the image material and draw it to the screen, using the blit() function to draw it to the window, for example: If the previous window is Screen, screen.blit(image,rect), where image is the image to be drawn on the screen and rect is the rectangle of the position to be drawn, you need to create rect by pygame. rect and set the position of rect.
-
Font = PyGame.font.Font(NONE,48) font= PyGame.font (NONE,48) font= pyGame.font. Render (text,TRUE,font_color,bg_color), where text is the content of the text, also a string; TRUE refers to the serrated switch of the text; Font_color is the RGB value of the text color, bg_color is the RGB value of the background color at the drawing point, and then draw the image through blit
-
WhileTRUE: in order to maintain the interface, the game must be kept in the whileTRUE: main loop
-
About the explosion effect of the production, any look the picture of the continuous actually is a ZhenZhen rapid display images, because the human eye vision seems to continuous, so for the explosion effect, only need when bullets collision with aliens, in the position of the aliens, to a certain speed drawing pictures is ok the burst; Images of the explosion are as follows:
This is the basic operation
The main file code is as follows:
import pygame
import sys
from time import sleep
from random import randint
from settings import Settings
from ship_mod_blit import Ship
from bullet import Bullet
from alien import Alien
from button import Button
from bullet import Skill
from game_information import Information
from bomp import Bomp
game_state=0
menu=0
game=1
over=2
score=0
randnum=1
class Alienwar_game:
def __init__(self):
pygame.init()
self.settings=Settings()
self.screen = pygame.display.set_mode((self.settings.screen_width,self.settings.screen_height))
self.ship=Ship(self)
self.button=Button(self,"start play")
self.bullets=pygame.sprite.Group()
self.aliens=pygame.sprite.Group()
self.skills=pygame.sprite.Group()
pygame.display.set_caption('诺克萨斯入侵')
self.creat_alien()
self.move=list()
self.lives=self.settings.ship_lives
self.information=Information(self)
self.bomp=Bomp(self)
self.clock=pygame.time.Clock()
self.time=60
self.speed_x=self.settings.alien_speed
self.speed_y=self.settings.alien_speed_y
def creat_alien(self):
available_space_x=self.settings.screen_width-2*40
avaliable_space_y=self.settings.screen_height-2*40
number_alien_x=available_space_x//80
for alien_number in range(number_alien_x):
new_alien=Alien(self)
new_alien.rect.x=randint(0,1000)
new_alien.rect.y=randint(0,200)
self.aliens.add(new_alien)
def fire(self):
if len(self.bullets)<self.settings.bullet_maxnum:
new_bullet=Bullet(self)
self.bullets.add(new_bullet)
def fire_skill(self):
newskill=Skill(self)
self.skills.add(newskill)
def event_check(self):
global score
global game_state
for event in pygame.event.get():
if(event.type==pygame.QUIT):
sys.exit()
if(event.type==pygame.KEYDOWN):
if(event.key==pygame.K_a or event.key==pygame.K_LEFT ):
self.ship.lstate=1
if(event.key==pygame.K_d or event.key ==pygame.K_RIGHT):
self.ship.rstate=1
if event.key==pygame.K_j:
self.fire()
if event.key==pygame.K_k:
self.fire_skill()
if(event.type==pygame.KEYUP):
if(event.key==pygame.K_a or event.key==pygame.K_LEFT):
self.ship.lstate=0
if(event.key==pygame.K_d or event.key==pygame.K_RIGHT):
self.ship.rstate=0
if(event.type==pygame.MOUSEBUTTONDOWN):
mouse_pos=pygame.mouse.get_pos()
if game_state==2:
if self.screen.get_rect().collidepoint(mouse_pos):
game_state=menu
score=0
elif game_state==0:
if self.button.rect.collidepoint(mouse_pos):
game_state = game
self.lives=self.settings.ship_lives
self.speed_y=self.settings.alien_speed_y
self.speed_x=self.settings.alien_speed
def run_game(self):
global game_state
global score
global randnum
count=0
count1=0
count2=0
while True:
count2+=1
self.clock.tick(self.time)
self.screen.fill(self.settings.bg_color)
self.event_check()
if game_state==menu:
self.screen.blit(self.button.msg_image,self.button.rect)
elif game_state==game:
for number in range(self.lives):
self.screen.blit(self.ship.image,pygame.Rect(number*40,0,40,40))
self.button.label1()
i=0
if self.ship.rect.x>=self.settings.ship_speed:
if self.ship.lstate == 1:
self.ship.rect.x -= self.settings.ship_speed
if self.ship.rect.x<=self.settings.screen_width-self.settings.ship_speed-40:
if self.ship.rstate==1:
self.ship.rect.x+=self.settings.ship_speed
self.bullets.update()
self.ship.blitme()
for bullet in self.bullets.sprites():
bullet.drawit()
if bullet.y<=0:
self.bullets.remove(bullet)
for skill in self.skills.sprites():
skill.skill_update()
skill.blitit()
if skill.rect.y<=0:
self.skills.remove(skill)
for num in range(len(self.aliens)):
self.move.append(1)
for alien in self.aliens.sprites():
if alien.rect.left==self.screen.get_rect().left or alien.rect.right==self.screen.get_rect().right:
self.move[i]=-self.move[i]
if alien.rect.right<self.screen.get_rect().right and alien.rect.left>self.screen.get_rect().left:
if count2%50==0:
randnum=randint(0,1)
print(f"{randnum}")
if randnum==0:
self.move[i] = -self.move[i]
alien.rect.x+=self.speed_x*self.move[i]
alien.rect.y+=self.speed_y
i+=1
if alien.rect.left<self.screen.get_rect().left:
alien.rect.left=self.screen.get_rect().left
if alien.rect.right>self.screen.get_rect().right:
alien.rect.right=self.screen.get_rect().right
if alien.rect.centery>=self.screen.get_rect().bottom:
print("诺手打进你家了!!!")
sleep(1.5)
self.bullets.empty()
self.aliens.empty()
self.creat_alien()
self.lives-=1
break
for alien_self in self.aliens.sprites():
for bullet_self in self.bullets.sprites():
if pygame.sprite.collide_rect(alien_self,bullet_self):
x=alien_self.rect.x
y=alien_self.rect.y
if pygame.sprite.collide_rect(alien_self,bullet_self) or count!=0 and count<=100:
self.bomp.draw_image(x,y,count1%5)
count+=1
if(count%20==0):
count1+=1
if count>100:
count=0
collision=pygame.sprite.groupcollide(self.bullets,self.aliens,True,True)
collision2=pygame.sprite.groupcollide(self.skills,self.aliens,False,True)
if collision:
score+=self.settings.alien_score
if collision2:
score+=self.settings.alien_score*len(collision2.values())
self.information.draw_score('score: '+str(score))
self.screen.blit(self.information.score_image,self.information.score_image_rect)
if not self.aliens:
self.bullets.empty()
self.skills.empty()
self.creat_alien()
self.speed_x+=0.5
if(score!=0 and score%10000==0):
self.speed_y+=0.01
if pygame.sprite.spritecollideany(self.ship,self.aliens):
print("亚索被诺手打死了 !!!")
sleep(1.5)
self.ship.rect.midbottom=self.screen.get_rect().midbottom
self.bullets.empty()
self.aliens.empty()
self.creat_alien()
self.lives-=1
if self.lives==0:
game_state=over
count2=0
self.aliens.draw(self.screen)
elif game_state == over:
self.button.label2()
self.button.label3(str(score))
pygame.display.flip()
if __name__=='__main__':
ai_game=Alienwar_game()
ai_game.run_game()
Copy the code
This is just a master file code, due to the function code into other documents, so I can’t run into, the concrete operation can be through code, since the code in multiple files, all inconvenience to copy and paste here, lest cause code should not be run, the complete code and pictures materials are stored in a lot or gitee cloud, If you want to see it, you can go to my website:
PS: If you need Python learning materials, please click on the link below to obtain them
Free Python learning materials and group communication solutions click to join