- 帖子
- 6
- 精华
- 0
- 积分
- 35
- 阅读权限
- 10
- 注册时间
- 2018-5-2
- 最后登录
- 2018-6-6
|
import pygame
from sys import exit
pygame.init()
screen=pygame.display.set_mode((600,170),0,32)
pygame.display.set_caption('Game in object!')
bg=pygame.image.load('./Figure/back.jpg').convert()
#定义子弹的类
class Bullet:
def __init__(self):
self.x=0
self.y=-1
self.image=pygame.image.load('./Figure/bullet.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 Plane:
def __init__(self):
self.image=pygame.image.load('./Figure/plane.png').convert_alpha()
def move(self):
mouseX, mouseY = pygame.mouse.get_pos()
self.x = mouseX-self.image.get_width()/2
self.y = mouseY-self.image.get_height()/2
#调用飞机的函数
plane=Plane()
#调用子弹的函数
bullet=Bullet()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
#根据图片调整窗口位置并显示背景图片
width=bg.get_width()
height=bg.get_height()
screen_size=(width,height)
screen=pygame.display.set_mode(screen_size, 0, 32)
screen.blit(bg, (0,0))
#调用子弹的Move方法
bullet.move()
#绘制子弹
screen.blit(bullet.image,(bullet.x-22,bullet.y))
screen.blit(bullet.image, (bullet.x+24, bullet.y))
#调用飞机的Move方式
plane.move()
#绘制飞机
screen.blit(plane.image,(plane.x,plane.y))
#更新
pygame.display.update()
修改了一下,封装了子弹跟飞机,双弹齐发,同时跟前面已经发过的朋友又有点小小的区别。
|
|