赞
踩
题目:创建一个矩形,它在屏幕右边缘以固定的速度上下移动。然后,在屏幕左边缘创建一艘飞船,玩家可上下移动该飞船,并射击前述矩形目标。添加一 个用于开始游戏的Play按钮,在玩家三次未击中目标时结束游戏,并重新显示Play按钮,让玩家能够通过单击该按钮来重新开始游戏。
完成效果:
Python:飞船射击
优势:跟网上为数不多的解决方案比较,本方案划分为几个功能文件,代码结构和思路更清晰。
心得:函数for循环里面的变量无法递增,可以考虑建一个类(比如Settings),将这个变量作为类的属性(self.fail_times=0),再实例化这个类(比如ai_settings)。最后在函数for循环里调用(ai_settings.fail_times)。
ship的原图:(下载后跟以下源代码放到同一个文件夹内再运行程序)
源代码:
文件1:space_war.py
- import pygame
- from pygame.sprite import Group
- from settings import Settings
- from ship import Ship
- import game_functions as gf
- from game_stats import GameStats
- from button import Button
- from target import Target
-
- def run_game():
- pygame.init()
- ai_settings=Settings()
- screen=pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height))
- pygame.display.set_caption("Space War")
- play_button=Button(ai_settings,screen,"Play")
- stats=GameStats(ai_settings)
- ship=Ship(ai_settings,screen)
- bullets=Group()
- target=Target(ai_settings,screen)
- while True:
- gf.check_events(ai_settings,screen,stats,play_button,ship,target,bullets)
- if stats.game_active:
- ship.update()
- target.update()
- gf.update_bullets(ai_settings,screen,ship,target,bullets)
- gf.update_target(ai_settings,stats,screen,ship,target,bullets)
- gf.check_fail(ai_settings,bullets,stats,target,screen)
- gf.update_screen(ai_settings,screen,stats,ship,target,bullets,play_button)
-
- run_game()
文件2: ship.py
- import pygame
-
- class Ship():
- def __init__(self,ai_settings,screen):
- self.screen=screen
- self.ai_settings=ai_settings
- self.image=pygame.image.load('ship.bmp')
- self.rect=self.image.get_rect()
- self.screen_rect=screen.get_rect()
- self.rect.centery=self.screen_rect.centery
- self.rect.left=self.screen_rect.left
- self.center=float(self.rect.centery)
- self.moving_up=False
- self.moving_down=False
-
- def update(self):
- if self.moving_up and self.rect.top>self.screen.get_rect().top:
- self.center-=self.ai_settings.ship_speed_factor
- if self.moving_down and self.rect.bottom<self.screen.get_rect().bottom:
- self.center+=self.ai_settings.ship_speed_factor
- self.rect.centery=self.center
-
- def blitme(self):
- self.screen.blit(self.image,self.rect)
-
- def center_ship(self):
- self.center=self.screen_rect.centery
文件3: target.py
- import pygame
- from pygame.sprite import Sprite
-
- class Target(Sprite):
- def __init__(self,ai_settings,screen):
- super(Target,self).__init__()
- self.screen=screen
- self.ai_settings=ai_settings
- self.rect=pygame.Rect(0,0,ai_settings.target_width,ai_settings.target_height)
- self.rect.right=self.screen.get_rect().right
- self.rect.centery=self.screen.get_rect().centery
-
- self.y=float(self.rect.y)
- self.color=ai_settings.target_color
- self.speed_factor=ai_settings.target_speed_factor
-
- def check_edges(self):
- screen_rect=self.screen.get_rect()
- if self.rect.top<0:
- return True
- elif self.rect.bottom>screen_rect.bottom:
- return True
-
- def update(self):
- self.y+=(self.speed_factor*self.ai_settings.fleet_direction)
- self.rect.y=self.y
-
- def draw_target(self):
- pygame.draw.rect(self.screen,self.color,self.rect)
文件4: settings.py
- class Settings():
- def __init__(self):
- self.screen_width=1200
- self.screen_height=800
- self.bg_color=(230,230,230)
- self.ship_speed_factor=5
- self.bullet_speed_factor=10
- self.bullet_width=30
- self.bullet_height=15
- self.bullet_color=60,60,60
- self.bullets_allowed=3
- self.target_speed_factor=1
- self.target_direction=1
- self.target_width=300
- self.target_height=100
- self.target_color=180
- self.fleet_direction=1
- self.ship_limit=3
- self.bullets_left=3
- self.fail_times = 0
文件5: game_stats.py
- class GameStats():
-
- def __init__(self,ai_settings):
- self.ai_settings=ai_settings
- self.reset_stats()
- self.game_active=False
-
- def reset_stats(self):
- self.ships_left=self.ai_settings.ship_limit
文件6: bullet.py
- import pygame
- from pygame.sprite import Sprite
-
- class Bullet(Sprite):
- def __init__(self,ai_settings,screen,ship):
- super(Bullet,self).__init__()
- self.screen=screen
- self.rect=pygame.Rect(0,0,ai_settings.bullet_width,ai_settings.bullet_height)
- self.rect.centery=ship.rect.centery
- self.x=float(self.rect.x)
- self.color=ai_settings.bullet_color
- self.speed_factor=ai_settings.bullet_speed_factor
-
- def update(self):
- self.x+=self.speed_factor
- self.rect.x=self.x
-
- def draw_bullet(self):
- pygame.draw.rect(self.screen,self.color,self.rect)
文件7: button.py
- import pygame.font
- class Button():
- def __init__(self,ai_settings,screen,msg):
- self.screen=screen
- self.screen_rect=screen.get_rect()
- self.width,self.height=200,50
- self.button_color = (0, 255, 0)
- self.text_color = (255, 255, 255)
- self.font=pygame.font.SysFont(None,48)
- self.rect=pygame.Rect(0,0,self.width,self.height)
- self.rect.center=self.screen_rect.center
- self.prep_msg(msg)
-
- def prep_msg(self,msg):
- self.msg_image=self.font.render(msg,True,self.text_color,self.button_color)
- self.msg_image_rect=self.msg_image.get_rect()
- self.msg_image_rect.center=self.rect.center
-
- def draw_button(self):
- self.screen.fill(self.button_color,self.rect)
- self.screen.blit(self.msg_image,self.msg_image_rect)
文件8: game_functions.py
- import sys
- import pygame
- from bullet import Bullet
- from target import Target
- from time import sleep
-
- def check_keydown_events(event,ai_settings,screen,ship,bullets):
- if event.key==pygame.K_DOWN:
- ship.moving_down=True
- elif event.key==pygame.K_UP:
- ship.moving_up=True
- elif event.key==pygame.K_SPACE:
- fire_bullet(ai_settings,screen,ship,bullets)
- elif event.key==pygame.K_q:
- sys.exit()
-
- def fire_bullet(ai_settings,screen,ship,bullets):
- if len(bullets)<ai_settings.bullets_allowed:
- new_bullet=Bullet(ai_settings, screen,ship)
- bullets.add(new_bullet)
-
- def check_fail(ai_settings,bullets,stats,target,screen): #定义一个函数,在主函数中被调用
- for bullet in bullets.copy(): #bullets为其他程序其他地方定义的一个编组
- if bullet.rect.right>=screen.get_rect().right: #判断bullet对象是否飞出屏幕右侧
- bullets.remove(bullet) #若bullet飞出右侧,则销毁该bullet
- ai_settings.fail_times+=1 #fail_times自增1
- print(ai_settings.fail_times) #控制台输出fail_times,用于监控不碰撞的次数
- elif pygame.sprite.spritecollideany(target,bullets):
- ai_settings.fail_times=0 #失败清零``
- elif ai_settings.fail_times>2:
- ai_settings.fail_times=0 #失败清零
- stats.game_active=False #游戏终止
- pygame.mouse.set_visible(True) #鼠标可见
-
-
- def check_keyup_events(event,ship):
- if event.key==pygame.K_DOWN:
- ship.moving_down=False
- elif event.key==pygame.K_UP:
- ship.moving_up=False
-
-
- def check_events(ai_settings,screen,stats,play_button,ship,target,bullets):
- for event in pygame.event.get():
- if event.type==pygame.QUIT:
- sys.exit()
- elif event.type==pygame.MOUSEBUTTONDOWN:
- mouse_x,mouse_y=pygame.mouse.get_pos()
- check_play_button(ai_settings,screen,stats,play_button,ship,target,bullets,mouse_x,mouse_y)
- elif event.type==pygame.KEYDOWN:
- check_keydown_events(event,ai_settings,screen,ship,bullets)
- elif event.type==pygame.KEYUP:
- check_keyup_events(event,ship)
-
- def check_play_button(ai_settings,screen,stats,play_button,ship,target,bullets,mouse_x,mouse_y):
- button_clicked=play_button.rect.collidepoint(mouse_x,mouse_y)
- if button_clicked and not stats.game_active:
- pygame.mouse.set_visible(False)
- stats.reset_stats()
- stats.game_active=True
-
- bullets.empty()
- create_fleet(ai_settings,screen,ship,target)
- ship.center_ship()
-
- def update_screen(ai_settings, screen, stats, ship, target,bullets, play_button):
- screen.fill(ai_settings.bg_color)
- for bullet in bullets.sprites():
- bullet.draw_bullet()
- ship.blitme()
- target.draw_target()
- if not stats.game_active:
- play_button.draw_button()
- pygame.display.flip()
-
- def update_bullets(ai_settings, screen, ship, target,bullets):
- bullets.update()
- for bullet in bullets.copy():
- if bullet.rect.right>screen.get_rect().right:
- bullets.remove(bullet)
- check_bullet_alien_collisions(ai_settings,screen,ship,target,bullets)
-
- def check_bullet_alien_collisions(ai_settings,screen,ship,target,bullets):
- collisions=pygame.sprite.spritecollideany(target,bullets,False)
-
- def create_fleet(ai_settings,screen,ship,target):
- target=Target(ai_settings,screen)
-
- def check_fleet_edges(ai_settings,target):
- if target.check_edges():
- change_fleet_direction(ai_settings,target)
-
- def change_fleet_direction(ai_settings,target):
- ai_settings.fleet_direction*=-1
-
- def update_target(ai_settings,stats,screen,ship,target,bullets):
- check_fleet_edges(ai_settings,target)
- target.update()
-
-
-
-
-
-
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。