天天看點

射殺外星人python實驗_Python系統學習 - 項目篇(消滅外星人2)

Thanks to 這邊因為公司停電了 哈哈哈哈哈哈哈哈哈哈,趕緊抓緊時間學習一下,完善一下消滅外星人,目前完成了子彈Bullet類以及可以正常的發射子彈如下,然後我準備發完這篇文章後,繼續學習到Mac沒電了,就回家充電去 嘿嘿嘿~

(完善子彈及子彈發射等代碼,對于其他幾個類都有改動,改動的類如下:大家接着上一篇直接覆寫即可:bullet.py、settings.py、alien_invasion.py、game_function.py)

1.bullet.py:

# coding = utf8

import os

os.path.abspath(".")

from pygame.sprite import Sprite

import pygame

class Bullet(Sprite):

"""

Image 代表圖檔本身,隻提供圖檔的資料和基本操作,

Sprite代表一個由多幀組成的閃花,隻負責隻幀數、位置上操作。傳入Image後,Sprite把Image分成多幀操作。

Sprite僅限隻遊戲架構範圍内使用

Image是基本圖檔封裝類型

Sprite可以認為是一個個小圖檔,一種可以在螢幕上移動的圖形對象,并且可以與其他圖形對象互動。精靈圖像可以是使用pygame

繪制函數繪制的圖像,也可以是原來就有的圖像檔案

目前類中使用Sprite類,通過使用精靈,可将遊戲中相關元素編組,進而同時操作編組中的所有元素。

"""

def __init__(self, screen, settings_game, ship):

"""Create a bullet object and contained theirs attributions

Args:

screen (Screen): Deliver into a screen object

settings_game (settings_game): Deliver into a settings_game object

ship (ship_obj): Deliver into a ship object

"""

# 在飛船位置處建立一個子彈對象

super(Bullet, self).__init__()

self.screen = screen

self.settings_game = settings_game

self.ship = ship

# 在(0, 0)位置繪制一個子彈的矩形,再設定後續位置

self.rect = pygame.Rect(0, 0, settings_game.bullet_width, settings_game.bullet_height)

self.rect.centerx = ship.ship_rect.centerx

self.rect.top = ship.ship_rect.top

# 存儲小數子彈縱坐标表示其位置,後續位置移動使用

self.y = float(self.rect.y) # rect.y == rect.top

self.color = settings_game.bullet_color

self.speed_factor = settings_game.bullet_speed_factor

def update(self):

"""

更新子彈位置

"""

self.y -= self.speed_factor

self.rect.y = self.y

def draw_bullet(self):

"""

螢幕上繪制子彈

"""

pygame.draw.rect(self.screen, self.color, self.rect)

# 子彈需要不斷更新位置,當繪制完成後需要更新子彈位置

self.update()

2.settings.py:

# coding = utf8

import os

os.path.abspath(".")

class settings_game():

def __init__(self):

# 螢幕屬性

self.width = 800

self.height = 600

self.bg_color = (230, 230, 230)

self.caption = "Alien Invasion"

# 飛船屬性

# 設定飛船移動速度

self.ship_speed_factor = 10

# 子彈屬性

self.bullet_speed_factor = 3

self.bullet_width = 3

self.bullet_height = 15

self.bullet_color = (60, 60, 60)

self.bullet_allowed = 5

if __name__ == "__main__":

pass

3.alien_invasion.py:

# coding = utf8

import os

import sys

import pygame

from settings import settings_game

from ship import ship_game

from game_function import game_function_game as gf

from pygame.sprite import Group

os.path.abspath(".")

def run_game():

# 初始化遊戲并建立一個螢幕對象

pygame.init()

settings_obj = settings_game()

screen = pygame.display.set_mode((settings_obj.width, settings_obj.height))

bg_color = settings_obj.bg_color

pygame.display.set_caption(settings_obj.caption)

# 建立一艘飛船

ship_obj = ship_game(screen)

# 建立一個用于存儲子彈的編組

bullets = Group()

# 開始遊戲主循環

while True:

# 監聽鍵盤和滑鼠事件

gf.check_events(ship_obj, settings_obj, screen, bullets)

# 更新子彈

gf.update_bullets(bullets)

# 讓最近繪制的螢幕可見

gf.update_screen(settings_obj, screen, ship_obj, bullets)

run_game()

4.game_function.py:

# coding = utf8

import os

os.path.abspath(".")

import sys

import pygame

from bullet import Bullet

class game_function_game():

def check_keydown_events(event, ship_obj, settings_obj, screen, bullets):

"""This is a function to control keydown events

Args:

event (Events): All event to accept for judgement from keyboard

ship_obj (ship_game): A ship_game object

settings_obj (settings_game): Deliver into a settings_game object

screen (Screen): Deliver into a screen object

bullets (Group) : Deliver into a bullet group

"""

if event.key == pygame.K_RIGHT:

# 向右移動飛船

ship_obj.moving_right = True

elif event.key == pygame.K_LEFT:

# 向左移動飛船

ship_obj.moving_left = True

elif event.key == pygame.K_UP:

# 向上移動飛船

ship_obj.moving_top = True

elif event.key == pygame.K_DOWN:

# 向下移動飛船

ship_obj.moving_bottom = True

elif event.key == pygame.K_SPACE:

# 建立一顆子彈并添加到編組bullets中, 同時限制在螢幕中出現的子彈是數量

game_function_game.fire_bullet(settings_obj, ship_obj, screen, bullets)

def check_keyup_events(event, ship_obj):

"""This is a function to control keyup events

Args:

event (Events): All event to accept for judgement from keyboard

ship_obj (ship_game): A ship_game object

"""

if event.key == pygame.K_LEFT:

ship_obj.moving_left = False

elif event.key == pygame.K_RIGHT:

ship_obj.moving_right = False

elif event.key == pygame.K_UP:

ship_obj.moving_top = False

elif event.key == pygame.K_DOWN:

ship_obj.moving_bottom = False

def check_events(ship_obj, settings_obj, screen, bullets):

"""Main events distribution

Args:

ship_obj (ship_game): A ship_game object to response keyboard or other action

settings_obj (settings_game): Deliver into a settings_game object

screen (Screen): Deliver into a screen object

bullets (Group) : Deliver into a bullet group

"""

# 響應按鍵和滑鼠事件

for event in pygame.event.get():

if event.type == pygame.QUIT:

sys.exit()

elif event.type == pygame.KEYDOWN:

game_function_game.check_keydown_events(event, ship_obj, settings_obj, screen, bullets)

elif event.type == pygame.KEYUP:

game_function_game.check_keyup_events(event, ship_obj)

def update_screen(settings_obj, screen, ship_obj, bullets):

"""A function to update screen, refresh the ship or other object's behaviour

Args:

settings_obj ([settings_game]): A settings_game object to gain some attributions

screen (Screen): Current screen's object as the game frame

ship_obj (ship_game): A ship_game object for ship update

bullets (Group) : Deliver into a bullet group

"""

# 更新螢幕圖像,并切換到新螢幕

# 每次循環時重繪螢幕

# 更新飛船位置

screen.fill(settings_obj.bg_color)

# 重繪所有子彈

for bullet in bullets.sprites():

bullet.draw_bullet()

ship_obj.blitme()

ship_obj.update(settings_obj)

# 更新整個待顯示的Surface對象到螢幕上

pygame.display.flip()

def update_bullets(bullets):

# 删除已消失的子彈

bullets.update()

for bullet in bullets.copy():

if bullet.rect.bottom <= 0:

bullets.remove(bullet)

def fire_bullet(settings_obj, ship_obj, screen, bullets):

"""Describe a function to fire bullet

Args:

settings_obj (settings_game): Deliver into a settings_game object

ship_obj (ship_game): A ship_game object for ship update

screen (Screen): Deliver into a screen object

bullets (Group) : Deliver into a bullet group

"""

# 發射子彈函數

if len(bullets) < settings_obj.bullet_allowed:

new_bullet = Bullet(screen, settings_obj, ship_obj)

bullets.add(new_bullet)

好了,那麼我們來看看效果圖:

雖然有點Low,好吧,的确是Low,下一篇文章我們就來建立外星人了,很快,這個項目就要完成, 大家拭目以待把~

關注 + 收藏 + 點贊哦,謝謝啦~