Python / Pygame Tutorial 7 - Sprite Class (Part 1/2)

preview_player
Показать описание
The next Python / Pygame tutorial. We look at how to make a simple class that we can use to create a sprite object. We create the __init__ method and we create our own method, the render method.

Download the completed project (from parts 1 and 2):

Code:
import pygame

black = (0,0,0)
white=(255,255,255)

moveX,moveY=0,0

class Sprite:

def __init__(self,x,y):

self.x=x

self.y=y

def render(self):


player=Sprite(100,150)
player2=Sprite(200,300)
gameLoop=True
while gameLoop:

gameLoop=False

moveX = -5

moveX = 5

moveY = -5

moveY = 5

moveX=0

moveX=0

moveY=0

moveY=0

player.x+=moveX

player.y+=moveY

Рекомендации по теме
Комментарии
Автор

And also please up load part 2 asap like in a few howers! Sorry I'm being so demanding bland I under stand if you can't but that anamated players thing would help so much! With a project I have due tomarow

obwonkonobe
Автор

Very nice video to understand classes and practice exercise in a window with keyboard events... congratulations... I wait for more tutorials of yours. Thanks!! ;)

elmandibulas
Автор

good job I'll wait for the next Video

samal
Автор

Possibly but it would be very complicated, I think we'll start with something simpler to begin with.

Animations
Автор

I don´t think it makes sense to summarize the previous video for 4 minutes at the beginning of every video. People can just watch the old video if they didn´t understand something. Anyways, good work so far! 

KingsCrowns
Автор

Yes I've just been very busy recently.

Animations
Автор

Sorry to bug you but can you out the code in the description?

obwonkonobe
Автор

Make sure the name of your class is spelled exactly the same as when you create an instance of it: they must both be 'Sprite'.

Animations
Автор

Are you going to continue your tutorials?

inFAMOUSnews
Автор

can u make a video on making bounderies for pygame

brentbaillie
Автор

Sorry, but I can't upload new videos that quickly. I'll try to make the second part as soon as I can.

Animations
Автор

You need to have spaces in between, are you using python idle version 2.5.7 or V 3?

LazyCreeperz
Автор

whenever i try to load it says:
player=sprite(400, 300)
type error:object() takes no parameters

calumparsons
Автор

when ever I try to run the module it says
player=Sprite(200, 350)
NameError: name 'Sprite' is not defined

TobstabMakesVidsOfficial
Автор

my animation aint working, there's only the second image thats coming on the window, the image aint changing!! i copied and rechecked it 100 times, it is same as yours, i dont know whats wrong!

TheMarusic
Автор

Go to google and search "Pygame Documents" and there is every command, code and script you need RE Pygame

LazyCreeperz
Автор

I got this error:
TypeError: object.__new__() takes no parameters

superpositiveable
Автор

---- == to a new line of code:

Just define as a variable:
screen_width = get.width() ---- and ---- screen_height = get.height() x = screen_width/2 ---- y = screen_height/2 --- # Rather that... Tell me your exact problem and then I can specify. I cannot continue writing due to a lack of characters remaining.

LazyCreeperz
welcome to shbcf.ru