- 帖子
- 4
- 精华
- 0
- 积分
- 30
- 阅读权限
- 10
- 注册时间
- 2017-10-30
- 最后登录
- 2019-9-30
|
# -*- coding: utf-8 -*-
import pygame
from sys import exit
#定义一个Bullet类,封装子弹相关的数据和方法
class Bullet(object):
def __init__(self):
self.x = 0
self.y = -1
self.images = pygame.image.load('bu.jpg').convert_alpha()
self.images= pygame.transform.scale(self.images,(52,52))
#初始化成员量,x,y,image
def move(self):
#处理子弹的运动
if self.y < 0:
mousex,mousey = pygame.mouse.get_pos()
self.x = mousex - self.images.get_width()/2
self.y = mousey - self.images.get_heigh()/2
else:
self.y -= 5
pygame.init()
screen = pygame.display.set_mode((900,700),0,32)
pygame.display.set_caption('look! what is this')
background = pygame.image.load('bga.jpg').convert()
plane = pygame.image.load('plane.jpg').convert_alpha()
bullet = Bullet()
#创建一个Bullet实例
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
screen.blit(background,(0,0))
bullet.move()
#调用move方法,处理子弹运动
screen.blit(bullet.image,(bullet.x,bullet.y))
#绘制子弹,数据来自其成员变量
x,y = pygame.mouse.get_pos()
x-= plane.get_width()/2
y-= plane.get_heigh()/2
screen.blit(plane,(x,y))
pygame.display.update()
#在python3.5上面运行出现如下错误:
Traceback (most recent call last):
File "same.py", line 35, in <module>
bullet.move()
File "same.py", line 19, in move
self.y = mousey - self.images.get_heigh()/2
AttributeError: 'pygame.Surface' object has no attribute 'get_heigh'
不知道什么原因,就是把原代码粘贴复制上去也会报错
|
|