萌新找不出问题,求大佬
# -*- coding: utf-8 -*-import pygame#导入pygame库
from sys import exit#向sys模块借一个exit函数用来退出程序
class Bullet:
def _init_(self):
self.x = 0
self.y = -1
self.image = pygame.image.load(r"D:\PY\lx\plane\images\bullet1.png").convert()
def move (self):
if self.y < 0:
mouseX,mouseY = pygame.mouse.get_pos()
self.x = mouseX - pygame.image,get_width()/2
elf.y = mouseY - pygame.image,get_heigh()/2
else:
self.y -= 5
pygame.init()#初始化pygame,为使用硬件做准备
screen = pygame.display.set_mode((400, 600), 0, 32)#创建了一个窗口,窗口大小和背景图片大小一样
pygame.display.set_caption("Plane World!")#设置窗口标题
background = pygame.image.load(r"D:\PY\lx\plane\images\background.png").convert()#加载并转换图像
palne = pygame.image.load(r"D:\PY\lx\plane\images\me1.png").convert()
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()
screen.blit(bullet.image , (bullet.x,bullet.y))
a,b = pygame.mouse.get_pos()
x -= plane.get.width()/2
y -= plane.get.height()/2
screen.blit(plane,(x,y))
pygame.display.update()#刷新一下画面
——————————————————————————————————————————
Traceback (most recent call last):
File "D:\PY\lx\1.py", line 28, in <module>
bullet.move()
File "D:\PY\lx\1.py", line 10, in move
if self.y < 0:
AttributeError: 'Bullet' object has no attribute 'y' Bullet那里,是双下划线的__init__,不是_init_ 魔法方法都是下划线包围的
所以在定义Bullet类的__init__时
不要写成_init_
修改后代码:
# -*- coding: utf-8 -*-
import pygame#导入pygame库
from sys import exit#向sys模块借一个exit函数用来退出程序
class Bullet:
def __init__(self):
self.x = 0
self.y = -1
self.image = pygame.image.load(r"D:\PY\lx\plane\images\bullet1.png").convert()
def move (self):
if self.y < 0:
mouseX,mouseY = pygame.mouse.get_pos()
self.x = mouseX - pygame.image,get_width()/2
elf.y = mouseY - pygame.image,get_heigh()/2
else:
self.y -= 5
pygame.init()#初始化pygame,为使用硬件做准备
screen = pygame.display.set_mode((400, 600), 0, 32)#创建了一个窗口,窗口大小和背景图片大小一样
pygame.display.set_caption("Plane World!")#设置窗口标题
background = pygame.image.load(r"D:\PY\lx\plane\images\background.png").convert()#加载并转换图像
palne = pygame.image.load(r"D:\PY\lx\plane\images\me1.png").convert()
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()
screen.blit(bullet.image , (bullet.x,bullet.y))
a,b = pygame.mouse.get_pos()
x -= plane.get.width()/2
y -= plane.get.height()/2
screen.blit(plane,(x,y))
pygame.display.update()#刷新一下画面 qiuyouzhi 发表于 2020-5-15 15:14
Bullet那里,是双下划线的__init__,不是_init_
代码:
import pygame#导入pygame库
from sys import exit#向sys模块借一个exit函数用来退出程序
class Bullet:
def __init__(self):
self.x = 0
self.y = -1
self.image = pygame.image.load(r"D:\PY\lx\plane\images\bullet1.png").convert()
def move (self):
if self.y < 0:
mouseX,mouseY = pygame.mouse.get_pos()
self.x = mouseX - pygame.image,get_width()/2
elf.y = mouseY - pygame.image,get_heigh()/2
else:
self.y -= 5
pygame.init()#初始化pygame,为使用硬件做准备
screen = pygame.display.set_mode((400, 600), 0, 32)#创建了一个窗口,窗口大小和背景图片大小一样
pygame.display.set_caption("Plane World!")#设置窗口标题
background = pygame.image.load(r"D:\PY\lx\plane\images\background.png").convert()#加载并转换图像
palne = pygame.image.load(r"D:\PY\lx\plane\images\me1.png").convert()
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()
screen.blit(bullet.image , (bullet.x,bullet.y))
a,b = pygame.mouse.get_pos()
x -= plane.get.width()/2
y -= plane.get.height()/2
screen.blit(plane,(x,y))
pygame.display.update()#刷新一下画面
qiuyouzhi 发表于 2020-5-15 15:14
Bullet那里,是双下划线的__init__,不是_init_
感谢大佬指点~~{:9_227:}
页:
[1]