'MyPlane' object has no attribute 'moveUp'是啥意思?需要改哪里?
本帖最后由 陌黎城 于 2021-8-19 20:41 编辑import pygame
import sys
import traceback
import myplane
from pygame.locals import *
pygame.init()
pygame.mixer.init()
import pygame
class MyPlane(pygame.sprite.Sprite):
def __init__(self,bg_size):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("images/me1.png").convert_alpha()
self.rect = self.image.get_rect()
self.width, self.height = bg_size, bg_size
self.rect.left, self.rect.top = \
(self.width - self.rect.width) // 2, \
self.height - self.rect.height - 68
self.speed = 10
def moveup(self):
if self.ret.top > 0:
self.rect.top -= self.speed
else:
self.rect.top = 0
def movedown(self):
if self.rect.bottom < self.height - 60:
self.rect.top += self.speed
else:
self.rect.bottom = self.height - 60
def moveleft(self):
if self.rect.left > 0:
self.rect.left -= self.speed
else:
self.rect.left = 0
def moveright(self):
if self.rect.right < self.width:
self.rect.left += self.speed
else:
self.rect.right = self.width
bg_size = width, height = 480,700
screen = pygame.display.set_mode(bg_size)
pygame.display.set_caption("飞机大战")
background = pygame.image.load("images/background.png")
#载入游戏音乐
pygame.mixer.music.load("sound/game_music.ogg")
pygame.mixer.music.set_volume(0.2)
bullet_sound = pygame.mixer.Sound("sound/bullet.wav")
bullet_sound.set_volume(0.2)
bomb_sound = pygame.mixer.Sound("sound/use_bomb.wav")
bomb_sound.set_volume(0.2)
supply_sound = pygame.mixer.Sound("sound/supply.wav")
supply_sound.set_volume(0.2)
get_bomb_sound = pygame.mixer.Sound("sound/get_bomb.wav")
get_bomb_sound.set_volume(0.2)
get_bullet_sound = pygame.mixer.Sound("sound/get_bullet.wav")
get_bullet_sound.set_volume(0.2)
upgrade_sound = pygame.mixer.Sound("sound/upgrade.wav")
upgrade_sound.set_volume(0.2)
enemy3_fly_sound = pygame.mixer.Sound("sound/enemy3_flying.wav")
enemy3_fly_sound.set_volume(0.2)
enemy1_down_sound = pygame.mixer.Sound("sound/enemy1_down.wav")
enemy1_down_sound.set_volume(0.2)
enemy2_down_sound = pygame.mixer.Sound("sound/enemy2_down.wav")
enemy2_down_sound.set_volume(0.2)
enemy3_down_sound = pygame.mixer.Sound("sound/enemy3_down.wav")
enemy3_down_sound.set_volume(0.5)
me_down_sound = pygame.mixer.Sound("sound/me_down.wav")
me_down_sound.set_volume(0.2)
def main():
pygame.mixer.music.play(-1)
#生成我方飞机
me = myplane.MyPlane(bg_size)
clock = pygame.time.Clock()
running = True
while running:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
#检测用户键盘操作
key_pressed = pygame.key.get_pressed()
if key_pressed or key_pressed:
me.moveUp()
if key_pressed or key_pressed:
me.moveDown()
if key_pressed or key_pressed:
me.moveLeft()
if key_pressed or key_pressed:
me.moveRight()
screen.blit(background,(0,0))
#绘制我方飞机
screen.blit(me.image, me.rect)
pygame.display.flip()
clock.tick(60)
if __name__=="__main__":
try:
main()
except SystemExit:
pass
except:
traceback.print_exc()
pygame.quit()
input()
'MyPlane' object has no attribute 'moveUp'是啥意思?怎么修正? 我猜需要改第 15 行,然后第 16 行去掉多余的空格
https://github.com/ruby-china/How-To-Ask-Questions-The-Smart-Way/blob/master/README-zh_CN.md 人造人 发表于 2021-8-19 20:42
我猜需要改第 15 行,然后第 16 行去掉多余的空格
大佬可以帮忙运行试一下吗?
myplane
import pygame
class MyPlane(pygame.sprite.Sprite):
def __init__(self,bg_size):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("images/me1.png").convert_alpha()
self.rect = self.image.get_rect()
self.width, self.height = bg_size, bg_size
self.rect.left, self.rect.top = \
(self.width - self.rect.width) // 2, \
self.height - self.rect.height - 68
self.speed = 10
def moveup(self):
if self.ret.top > 0:
self.rect.top -= self.speed
else:
self.rect.top = 0
def movedown(self):
if self.rect.bottom < self.height - 60:
self.rect.top += self.speed
else:
self.rect.bottom = self.height - 60
def moveleft(self):
if self.rect.left > 0:
self.rect.left -= self.speed
else:
self.rect.left = 0
def moveright(self):
if self.rect.right < self.width:
self.rect.left += self.speed
else:
self.rect.right = self.width
陌黎城 发表于 2021-8-19 20:56
大佬可以帮忙运行试一下吗?
main
import pygame
import sys
import traceback
import myplane
from pygame.locals import *
pygame.init()
pygame.mixer.init()
bg_size = width, height = 480,700
screen = pygame.display.set_mode(bg_size)
pygame.display.set_caption("飞机大战")
background = pygame.image.load("images/background.png")
#载入游戏音乐
pygame.mixer.music.load("sound/game_music.ogg")
pygame.mixer.music.set_volume(0.2)
bullet_sound = pygame.mixer.Sound("sound/bullet.wav")
bullet_sound.set_volume(0.2)
bomb_sound = pygame.mixer.Sound("sound/use_bomb.wav")
bomb_sound.set_volume(0.2)
supply_sound = pygame.mixer.Sound("sound/supply.wav")
supply_sound.set_volume(0.2)
get_bomb_sound = pygame.mixer.Sound("sound/get_bomb.wav")
get_bomb_sound.set_volume(0.2)
get_bullet_sound = pygame.mixer.Sound("sound/get_bullet.wav")
get_bullet_sound.set_volume(0.2)
upgrade_sound = pygame.mixer.Sound("sound/upgrade.wav")
upgrade_sound.set_volume(0.2)
enemy3_fly_sound = pygame.mixer.Sound("sound/enemy3_flying.wav")
enemy3_fly_sound.set_volume(0.2)
enemy1_down_sound = pygame.mixer.Sound("sound/enemy1_down.wav")
enemy1_down_sound.set_volume(0.2)
enemy2_down_sound = pygame.mixer.Sound("sound/enemy2_down.wav")
enemy2_down_sound.set_volume(0.2)
enemy3_down_sound = pygame.mixer.Sound("sound/enemy3_down.wav")
enemy3_down_sound.set_volume(0.5)
me_down_sound = pygame.mixer.Sound("sound/me_down.wav")
me_down_sound.set_volume(0.2)
def main():
pygame.mixer.music.play(-1)
#生成我方飞机
me = myplane.MyPlane(bg_size)
clock = pygame.time.Clock()
running = True
while running:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
#检测用户键盘操作
key_pressed = pygame.key.get_pressed()
if key_pressed or key_pressed:
me.moveUp()
if key_pressed or key_pressed:
me.moveDown()
if key_pressed or key_pressed:
me.moveLeft()
if key_pressed or key_pressed:
me.moveRight()
screen.blit(background,(0,0))
#绘制我方飞机
screen.blit(me.image, me.rect)
pygame.display.flip()
clock.tick(60)
if __name__=="__main__":
try:
main()
except SystemExit:
pass
except:
traceback.print_exc()
pygame.quit()
input()
陌黎城 发表于 2021-8-19 20:57
main
def moveup(self): # 这里定义的时候小写
me.moveUp() # 这里使用的时候就大写了?
要认真一点呀
人造人 发表于 2021-8-19 21:01
def moveup(self): # 这里定义的时候小写
me.moveUp() # 这里使用的时候就大写了?
要 ...
懂了懂了,谢谢大佬
页:
[1]