设为首页收藏本站

Crossin的编程教室

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

【Pygame 第6课】 面向对象的游戏设计 报错,求大神指点

[复制链接]

1

主题

0

好友

15

积分

新手上路

Rank: 1

跳转到指定楼层
楼主
发表于 2018-8-14 18:32:09 |只看该作者 |倒序浏览
【Pygame 第6课】 面向对象的游戏设计  https://mp.weixin.qq.com/mp/appmsg/show?__biz=MjM5MDEyMDk4Mw==&appmsgid=10000245&itemidx=1&sign=81dadfd09edf53ad3dbb4bba767f8702

写的代码报错,Python3环境,不知道如何解决,求大神指点
报错如下:
Traceback (most recent call last):
  File "D:/Python Project/Test.py", line 36, in <module>
    bullet.move()
  File "D:/Python Project/Test.py", line 12, in move
    if self.y<0:
AttributeError: 'Zidan' object has no attribute 'y'

源码如下:

import pygame
from sys import exit
class Zidan:
    def _init_(self):
        self.x=0
        self.y=-1
        self.image=pygame.image.load("zidan.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
pygame.init()
screen = pygame.display.set_mode((450, 800), 0, 32)
pygame.display.set_caption("Hello, World!")
background = pygame.image.load('background.png').convert()
plane=pygame.image.load("airplain.png").convert_alpha()
bullet = Zidan()
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
    screen.blit(background, (0,0))
    bullet.move()
    screen.blit(bullet.image, (bullet.x, bullet.y))
    x,y=pygame.mouse.get_pos()
    x-=plane.get_width()/2
    y-=plane.get_height()/2
    screen.blit(plane,(x,y))
    pygame.display.update()





回复

使用道具 举报

174

主题

45

好友

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

沙发
发表于 2018-8-14 18:35:11 |只看该作者
def __init__(self):
这种函数都是前后两个下划线
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

1

主题

0

好友

15

积分

新手上路

Rank: 1

板凳
发表于 2018-8-14 18:36:52 |只看该作者
crossin先生 发表于 2018-8-14 18:35
def __init__(self):
这种函数都是前后两个下划线

我的源码里是前后两个下划线啊
回复

使用道具 举报

1

主题

0

好友

15

积分

新手上路

Rank: 1

地板
发表于 2018-8-14 22:14:59 |只看该作者
没有大神吗?
回复

使用道具 举报

174

主题

45

好友

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

5#
发表于 2018-8-15 11:41:42 |只看该作者
zhuanglg 发表于 2018-8-14 22:14
没有大神吗?

__init__
前后两个下划线,请仔细看

指出了你的问题,你不虚心接受,却在这里反问“没有大神”,你这个学习态度也是可以的。以后谁还回答你问题
想要大神指导,可以买我们的付费课程
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

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

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

GMT+8, 2024-5-4 04:42 , Processed in 0.027747 second(s), 23 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部