Crossin的编程教室
标题:
pygame第五课面对对象的游戏设计——问题
[打印本页]
作者:
158791
时间:
2017-11-1 12:32
标题:
pygame第五课面对对象的游戏设计——问题
# -*- 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'
不知道什么原因,就是把原代码粘贴复制上去也会报错
作者:
158791
时间:
2017-11-1 13:02
已经解决,谢谢大家!
欢迎光临 Crossin的编程教室 (https://bbs.crossincode.com/)
Powered by Discuz! X2.5