您好,登錄后才能下訂單哦!
pygame城市之戰橫版射擊游戲,按上下左右方向箭頭操作飛機。這是一個橫板射擊小游戲,在黑夜的城市上空,你將要操作一架飛機去射擊敵機,爆炸效果還不錯。
在游戲中定義了滾動的背景類,定義了飛機類Plane,定義了子彈類,敵機類,爆炸類等,是學習Pygame和面向對象編程的好例子。
代碼:
import math import time import pygame from pygame.locals import * from random import choice,randint class ScrolledBackground(pygame.sprite.Sprite): def __init__(self,image,screen): pygame.sprite.Sprite.__init__(self) self.screen = screen self.width = screen.get_width() self.height = screen.get_height() self.image0 = pygame.image.load(image).convert_alpha() self.image1 = pygame.image.load(image).convert_alpha() self.rect0 = self.image0.get_rect() self.rect1 = self.image1.get_rect() self.rect1.left = self.rect0.right self.dx = -10 self.dy = 0 def update(self): pass def draw(self): pass class Plane(pygame.sprite.Sprite): def __init__(self,image,keys,screen): pygame.sprite.Sprite.__init__(self) self.keys = keys # 上下左右按鍵 self.image = pygame.image.load(image).convert_alpha() self.screen = screen self.rect = self.image.get_rect() self.rect.centery = self.screen.get_height()//2 self.xspeed = 0 self.yspeed = 0 self.dead = False # 新增加的屬性 def keys_check(self,all_keys): pass def update(self): self.rect.move_ip(self.xspeed,self.yspeed) def draw(self): self.screen.blit(self.image,self.rect) class Bullet(pygame.sprite.Sprite): def __init__(self,image,plane,group,screen): pygame.sprite.Sprite.__init__(self) self.plane = plane self.image = pygame.image.load(image).convert_alpha() self.rect = self.image.get_rect() self.rect.center = plane.rect.center self.group = group self.group.add(self) self.screen = screen self.screen_width = screen.get_width() self.screen_height = screen.get_height() def update(self): self.rect.move_ip(10,0) if self.rect.left > self.screen_width : self.group.remove(self) def split_images(image,rows,cols): """image是一張圖片,把它切分為若干圖,返回列表""" global explosion_images_list image = pygame.image.load(image) step_width = image.get_width()//cols step_height = image.get_height()//rows pass class Explosion(pygame.sprite.Sprite): """爆炸效果類,顯示一系列幀圖""" def __init__(self,surimages,position,group): pygame.sprite.Sprite.__init__(self) self.surimages = surimages # 爆炸效果用到的surface列表 self.index = 0 self.amounts = len(surimages) self.image = self.surimages[self.index] # 初始造型 self.rect = self.image.get_rect() # 矩形對象 self.rect.center = position # 爆炸位置 self.group = group self.group.add(self) self.interval_time = 0.01 # 造型切換時間 self.begin_time = time.time() # 爆炸起始時間 def update(self): # 換造型 if time.time() - self.begin_time >= self.interval_time: # 超時,則換造型 if self.index < self.amounts: self.image = self.surimages[self.index] self.index = self.index + 1 self.begin_time = time.time() else: self.group.remove(self) # 造型切換完了則從組中移除自己 class Enemy(pygame.sprite.Sprite): def __init__(self,images,group,screen): """images是surface列表""" pygame.sprite.Sprite.__init__(self) self.screen = screen self.screen_width = self.screen.get_width() # 獲取屏幕寬度 self.screen_height = self.screen.get_height() # 獲取屏幕高度 self.image = choice(images) # 隨機選擇一個surface self.rect = self.image.get_rect() self.rect.left = self.screen_width + randint(10,self.screen_width) self.rect.centery = randint(0,self.screen_height) self.group = group self.group.add(self) # 加入到自己的組 def update(self): self.rect.move_ip(-5,0) if self.rect.right <= 0 : self.group.remove(self) class Eullet(pygame.sprite.Sprite): """敵方子彈類""" def __init__(self,image,selfgroup,enemy_group,plane,screen): """參數列表:image,已轉換成surface的對象 selfgroup,所在的組 enemy_group,敵人組. plane,我方飛機 screen,屏幕對象 """ pass def update(self): self.rect.move_ip(-self.dx//30,-self.dy//30) if self.beyond_edge() : self.group.remove(self) def beyond_edge(self): pass def collision_check(): """對游戲中的對象進行碰撞檢測,碰撞檢測有以下幾種: 1、我方飛機碰到敵方飛機,都爆炸。給我方飛機增加dead屬性。 2、我方飛機碰到敵方子彈,我方飛機爆炸,游戲結束。 3、敵方飛機碰到我方子彈,敵方飛機爆炸。 以下引用的是全局變量 """ global enemy_counter "我方飛機和敵方飛機任何一架的碰撞" if not plane1.dead: eny = pygame.sprite.spritecollideany(plane1, group_enemy) if eny!=None : # 碰到了,我方飛機死,游戲結束 Explosion(explosion_images_list,eny.rect.center,group_explosion) # 在敵方飛機處發生爆炸 Explosion(explosion_images_list,plane1.rect.center,group_explosion) # 在我方飛機處發生爆炸 plane1.dead = True group_enemy.remove(eny) # 從敵方組中移除碰撞的敵機 "我方飛機碰到敵方子彈,我方死,游戲結束。" if not plane1.dead: b = pygame.sprite.spritecollideany(plane1, group_enemy_bullet) if b!=None : # 碰到了,我方飛機死,游戲結束 Explosion(explosion_images_list,plane1.rect.center,group_explosion) # 在我方飛機處發生爆炸 plane1.dead = True "敵方飛機碰到我方子彈,我方子彈爆,敵方爆炸" result_dict = pygame.sprite.groupcollide(group_bullet1, group_enemy, True, True) # 一顆子彈可能打到幾架飛機 if result_dict !={}: missle = list(result_dict.keys())[0] # 碰到我方子彈 enemy_list = result_dict[missle] #Explosion(explosion_images_list,missle.rect.center,group_explosion) enemy_counter += len(enemy_list) # 大于一定的數量,如果我方戰機沒死,則成功結束 for e in enemy_list: Explosion(explosion_images_list,e.rect.center,group_explosion) pygame.display.set_caption("風火輪少兒編程_城市之戰射擊游戲,當前打爆敵機數:" + str(enemy_counter) + ",請自行編寫游戲成功結束的代碼!") if __name__ == "__main__": width,height = 480,360 enemy_bullet = "images/bullet2.png" enemy_images = ["images/enemy0.png","images/enemy1.png","images/enemy2.png","images/enemy3.png"] explosion_images = "images/explosion.png" image = "images/night city with street.png" plane_image1 = "images/plane.png" bullet_image1 = "images/bullet.png" pygame.init() screen = pygame.display.set_mode((width,height)) pygame.display.set_caption("城市之戰,按上下左右操作飛機。風火輪少兒編程_www.scratch8.net") enemy_bullet = pygame.image.load(enemy_bullet) enemy_images = [pygame.image.load(img) for img in enemy_images] # 形成敵機的surface列表 explosion_images_list = [] # 爆炸效果surface列表 explosion_images = split_images(explosion_images,2,13) # 按2行13列切分圖形,返回surface列表 keys1 = [K_UP,K_DOWN,K_LEFT,K_RIGHT] plane1 = Plane(plane_image1,keys1,screen) bg = ScrolledBackground(image,screen) running = True clock = pygame.time.Clock() bullet1_shoot_EVENT = USEREVENT + 1 pygame.time.set_timer(bullet1_shoot_EVENT,500) "敵機定時生成事件" enemy_EVENT = USEREVENT + 2 pygame.time.set_timer(enemy_EVENT,1000) "敵機定時發射子彈事件" enemy_shoot_EVENT = USEREVENT + 3 pygame.time.set_timer(enemy_shoot_EVENT,50) group_explosion = pygame.sprite.Group() group_bullet1 = pygame.sprite.Group() group_enemy = pygame.sprite.Group() group_enemy_bullet = pygame.sprite.Group() enemy_amounts = 100 # 大于這個數量則游戲成功結束 enemy_counter = 0 while running: for event in pygame.event.get(): if event.type == QUIT: running = False break if event.type == bullet1_shoot_EVENT: # 我方子彈1發射事件 if not plane1.dead: Bullet(bullet_image1,plane1,group_bullet1,screen) if event.type == enemy_EVENT: # 定時生成一架敵機 Enemy(enemy_images,group_enemy,screen) if event.type == enemy_EVENT: # 定時生成一枚敵機子彈 Eullet(enemy_bullet,group_enemy_bullet,group_enemy,plane1,screen) all_keys = pygame.key.get_pressed() # 所有按鍵檢測 plane1.keys_check(all_keys) # 對plane1進行按鍵檢測 bg.update() # 更新背景坐標 group_bullet1.update() # 我方子彈更新 if not plane1.dead : plane1.update() # 飛機1坐標更新 group_enemy.update() # 敵機坐標 group_enemy_bullet.update() # 敵機子彈更新 group_explosion.update() # 爆炸效果更新 "坐標都更新完后,應該進行碰撞檢測" collision_check() screen.fill((0,0,0)) bg.draw() group_bullet1.draw(screen) if not plane1.dead : plane1.draw() group_enemy.draw(screen) group_enemy_bullet.draw(screen) group_explosion.draw(screen) pygame.display.update() clock.tick(30) pygame.quit()
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。