设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
查看: 8378|回复: 1
打印 上一主题 下一主题

pygame第五课面对对象的游戏设计——问题

[复制链接]

2

主题

0

好友

30

积分

新手上路

Rank: 1

跳转到指定楼层
楼主
发表于 2017-11-1 12:32:09 |只看该作者 |倒序浏览
# -*- 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'

不知道什么原因,就是把原代码粘贴复制上去也会报错



回复

使用道具 举报

2

主题

0

好友

30

积分

新手上路

Rank: 1

沙发
发表于 2017-11-1 13:02:56 |只看该作者
已经解决,谢谢大家!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即加入

QQ|手机版|Archiver|Crossin的编程教室 ( 苏ICP备15063769号  

GMT+8, 2024-4-19 19:18 , Processed in 0.026023 second(s), 25 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部