python:实例1:打飞机游戏:02 ship.py

Posted wssking

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python:实例1:打飞机游戏:02 ship.py相关的知识,希望对你有一定的参考价值。

 ship.py 
1
import pygame 2 3 #Ship类,主要用于生成飞船的各项属性信息,以及控制上下左右的移动速度和范围 4 5 class Ship: 6 #定义生成飞船的属性值 7 def __init__(self, screen): 8 self.screen = screen 9 self.image = pygame.image.load(rimagesship.png) 10 self.rect = self.image.get_rect() 11 self.screen_rect = screen.get_rect() 12 13 #ship在主窗口中的位置,把窗口的中心值,赋给ship的中心(把ship的x坐标和窗口中心对齐) 14 self.rect.centerx = self.screen_rect.centerx 15 16 #把窗口底的值,赋值给ship的底(把ship的y坐标和窗口的底以齐),此两项即把ship放置到窗口底部中央位置 17 self.rect.bottom = self.screen_rect.bottom 18 19 #定义两个中心变量,用于存放ship的位置值 20 self.center = float(self.rect.centerx) 21 self.bottom = float(self.rect.bottom) 22 23 #定义移动速度系数 24 self.speed_factor = 3 25 26 #把上下左右移动预设为0 27 self.moving_left = False 28 self.moving_right = False 29 self.moving_up = False 30 self.moving_down = False 31 32 33 #此函数,主要用于控制ship的上下左右移动时,x,y坐标的位置,以及保证移动时不超出窗口范围 34 def update(self): 35 if self.moving_left and self.center > 0+(self.rect.width/2): 36 self.center -= self.speed_factor 37 if self.moving_right and self.center < self.screen_rect.right-(self.rect.width/2): 38 self.center += self.speed_factor 39 if self.moving_up and self.rect.top > self.screen_rect.top: 40 self.bottom -= self.speed_factor 41 if self.moving_down and self.bottom < self.screen_rect.bottom: 42 self.bottom += self.speed_factor 43 44 45 #把移动后的ship的中心和底的位置值,再返回给ship,作为当前位置 46 self.rect.centerx = self.center 47 self.rect.bottom = self.bottom 48 49 50 #此函数,两个参数 ship图像信息,矩形的信息(包括矩形当前的位置信息),主要用于把图像绘制出来 51 def blitme(self): 52 self.screen.blit(self.image, self.rect)

 

以上是关于python:实例1:打飞机游戏:02 ship.py的主要内容,如果未能解决你的问题,请参考以下文章

python实战===如何优雅的打飞机

Android_(游戏)打飞机02:游戏背景滚动

打飞机小游戏 python+pygame

Android_(游戏)打飞机06:后续

Android_(游戏)打飞机03:控制玩家飞机

python pygame模块 打飞机游戏