Home > other >  For help, fire alien_invasion learned that step
For help, fire alien_invasion learned that step

Time:09-29

This can be moved but not shoot, please forgive me didn't write notes to you


 import sys 
The import pygame
The from pygame. Sprite import Sprite
The from pygame. Sprite import Group
The class Setting () :
Def __init__ (self) :
Self. Screen_width=800
Self. Screen_height=600
Self. Bg_colour=(255, 240, 245)
Self. Bullet_factor=1
Self. Bullet_width=10
Self. Bullet_height=15
Self. Bullet_colour=(0255127)
The class bullet (Sprite) :
Def __init__ (self, screen) :
Super (bullet, self) __init__ ()
The self. The screen=screen
The self. The rect=(0, 0, Setting bullet_width, Setting. Bullet_height)
The self. The rect. Centerx=ship. The rect. Centerx
The self. The rect. Bottom=ship. The rect. Bottom
Self. Y=float (self. The rect. Y)
The self. The color=Setting. Bullet_colour
Self. Speed_factor=Setting. Bullet_factor

Def update (self) :
Self. - y=self. Speed_factor
The self. The rect. Y=self. Y

Def draw_bullet (self) :
Pygame. The draw (). The rect (self) screen, the self. The color, the self. The rect)
The class ship () :
Def __init__ (self, screen) :
The self. The screen=screen
Self. Image=pygame. Image. The load (" twine. BMP ")
The self. The rect=self. Image. Get_rect ()
Self. Screen_rect=screen. Get_rect ()
The self. The rect. Centerx=self. Screen_rect. Centerx
The self. The rect. Bottom=self. Screen_rect. Bottom
Self. Right_move=False
Self. Left_move=False

Def update (self) :
If self. Right_move==True:
If self. Right_move and self. The rect. Right & lt; Self. Screen_rect. Right:
The self. The rect. Centerx +=1
If self. Left_move==True:
If the self. The rect. Left & gt; Self. Screen_rect. Left:
The self. The rect. Centerx +=1
Def blit (self) :
The self. The screen. The blit (self image, self. The rect)



Def run_game () :
Pygame. The init ()
A=Setting ()
Screen=pygame. Display. Set_mode (a.s creen_height, a.s creen_height))
Pygame. Display. Set_caption (" hello ")
Bg_colour=a. g_colour
Ships=ship (screen)

Bullets=Group ()
While True:
For the event in pygame. Event. The get () :
If the event. The type==pygame. QUIT:
Sys. The exit ()

Elif event. Type==pygame. KEYDOWN:
If the event. The key==pygame. K_RIGHT:
Ships. Right_move=True
If the event. The key==pygame. K_LEFT:
Ships. Left_move=True
Elif event. Type==pygame. KEYUP:
Ships. Right_move=False
Ships. Left_move=False


Elif event. Type==pygame. KEYDOWN:
If the event. The key==pygame. K_0:
New_bullet=bullet (Setting, ship, screen, bullets)
Bullets. The add (new_bullet)
For bullet in bullets. Sprites () :
Bullet. Draw_bullet ()

Bullets. The update ()
Pygame. Display. Flip ()
Ships. The update ()
Screen. The fill (bg_colour)
Ships. Blit ()



Run_game ()


  • Related