- 帖子
- 48
- 精华
- 0
- 积分
- 200
- 阅读权限
- 30
- 注册时间
- 2015-12-5
- 最后登录
- 2016-5-12
|
- # -*- coding: utf-8 -*-
- import pygame
- from sys import exit
- class Bullet:
- def __init__(self): #初始化成员函数,可是为什么python第49课例子的直接上‘speed = 0’而不用__init__函数初始化呢?
- self.x = 0
- self.y = -1
- self.image = pygame.image.load('f:\plane\sullet.png').convert_alpha()
- def move(self):
- if self.y < 0:
- mouseX,mouseY = pygame.mouse.get_pos()
- self.x = mouseX - self.image.get_width() / 2
- self.y = mouseY - self.image.get_height() / 2
- else:
- self.y -= 5
-
- class Bullet_l(Bullet): #继承了父类Bullet;这个类是为了确定左边子弹的坐标
- def move(self):
- if self.y < 0:
- mouseX,mouseY = pygame.mouse.get_pos()
- self.x = mouseX - self.image.get_width() / 2
- self.y = mouseY - self.image.get_height() / 2
- self.x = self.x - 3.5 * self.image.get_width()
- else:
- self.y -= 5
-
- class Bullet_r(Bullet): #同样继承父类Bullet;为了确定右边子弹的坐标
- def move(self):
- if self.y < 0:
- mouseX,mouseY = pygame.mouse.get_pos()
- self.x = mouseX - self.image.get_width() / 2
- self.y = mouseY - self.image.get_height() / 2
- self.x = self.x + 3.5 * self.image.get_width()
- else:
- self.y -= 5
- #启动pygame
- pygame.init()
- #创建pygame的窗口,设置了窗口尺寸和窗口标题
- screen = pygame.display.set_mode((450,800),0,32)
- pygame.display.set_caption('Hello World')
- background = pygame.image.load('f:\plane\some.jpg')
- plane = pygame.image.load('f:\plane\plane.png').convert_alpha()
- #创建三个类的对象
- bullet = Bullet()
- bullet_l = Bullet_l()
- bullet_r = Bullet_r()
- while True:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- pygame.quit()
- exit()
- screen.blit(background,(0,0))
- #确定三个图片(子弹)的坐标
- bullet.move()
- bullet_l.move()
- bullet_r.move()
- #因为这3张图片互不干扰,所以可以不分前后顺序
- screen.blit(bullet.image,(bullet.x,bullet.y))
- screen.blit(bullet.image,(bullet_l.x,bullet_l.y))
- screen.blit(bullet.image,(bullet_r.x,bullet_r.y))
- #感觉飞机的图片并不是完全对称的,我把子弹图片的中心点设置为鼠标坐标,然而子弹竟然不是从飞机头正中射出去的,而是稍微偏左了。
- #左右两边的子弹看起来偏离地更离谱。
- x,y = pygame.mouse.get_pos()
- planeX = x - plane.get_width() / 2
- planeY = y - plane.get_height() / 2
- screen.blit(plane,(planeX,planeY))
- pygame.display.update()
复制代码 |
|