Форум сайта python.su
0
[Добрый день от новичка
Начал изучать pygame, и на первой недели застрял на загрузки изображения
Версия pathon 2.7.9
Как я понял из литературы, файл должен находиться в одной директорий, или же сделать на него ссылку
Пробовал 2 способами, но python выдаёт ошибку
Traceback (most recent call last):
File “C:\Users\balabas\Desktop\Python\3+.py”, line 20, in <module>
hero = Sprite(0, 0, ‘h.png’)
TypeError: this constructor takes no arguments
Сам код
[code python]
# -*- coding: cp1251 -*-
import random,pygame
#Создаём окно программы
window = pygame.display.set_mode((400,400))
#Задаём имя окна
pygame.display.set_caption("hello world")
#создаём рабочую область
screen = pygame.Surface((400,400))
#создаём Sprite
class Sprite:
def _init_(self,xpos,ypos,filename):
self.x = xpos
self.y = ypos
self.bitmap = pygame.image.load(filename)
self.bitmap.set_colorkey((0,0,0))
def render(self):
screen.blit(self.bitmap,(self.x,self.y))
hero = Sprite(0, 0, 'h.png')
zet =Sprite(0,0,"z.png")
# Создаём цикл
done= True
while done:
# цикл работает до тех пор, пока переменная E не станет False И будет
# присвоена к QUIT, далее сбрасывает все значения,с помощью команды
# event.get()
for e in pygame.event.get():
if e.type == pygame.QUIT:
done = False
screen.fill((255,250,255))
hero.render()
zet.render()
window.blit(screen,(0,0))
pygame.display.flip()[/code]
Отредактировано balabas (Фев. 21, 2015 20:23:49)
Офлайн
61
Common Problems
Currently there is one main problem that catches new users. When you derive your new sprite class with the Sprite base, you must call the Sprite.__init__() method from your own class __init__() method. If you forget to call the Sprite.__init__() method, you get a cryptic error, like this:
AttributeError: ‘mysprite’ instance has no attribute ‘_Sprite__g’.
Офлайн
0
ZerGMany thanks, It works!!
Офлайн
61
дык нима зо шо
Офлайн