1
\$\begingroup\$

Relevant information

I'm in a grade 11 computer science class that uses Python & Pygame. We're aloud to use sprites and images though we're never taught it, just drawing graphics like squares and circles. We were taught arrays, lists, functions but not classes and objects. We also relatively do things without using functions like the init main one I see a lot on line, we also don't use self or super.

If it's anything relevant, I'm making a top down (bird's eye view) 2D shooter game similar to a 2D version of dead ops arcade in Call Of Duty: Black Ops.

What I'm trying to do

I've been programming with Java & LibGDX before hand. So I want to use sprites in images in Pygame the same way I've been using it in LibGDX. Getting images, obtaining image rectangles for collision, rotating/transforming images, changing the images of the sprite.

Though every tutorial I've seen for sprites and images in Pygame that I can search up uses objects, classes and function calls that I'm not familiar with.

What I've done

I've loaded a PNG into my program and drew it to the screen. Simple and straight forward. Though the Pygame documentation doesn't give me a lot of options in terms of manipulating these images other than moving their location.

What I'm asking for

A simple run down syntax of how I would use a sprite in Pygame, without objects/classes or any weird function terminology. In terms of functions I can make a function, pass information through parameters and return values. I'm not aware of global variables, self or super.

The simplest of simple where I can resize an image, change the image of a sprite, transform/rotate it, and get a rectangle that I can use to overlap with another one.

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

Use a spritemap/atlas

You won't need to use global variables (and should be avoided for numerous not listed here), as well self and super you don't need to fully understand yet until you start extending your program and using inheritance.

You will need your sprite drawn into evenly spaced squares/rectangles on a sprite sheet. I will refer to this as an example. You can think of this png as 6 frames of a walking animation.

Loading that png into your program as you are similarly now, you can then divide it into equal frames (you can also make use of an array here by storing each frame array[0] ... array[5]). Then in your update loop if the character is walking you simply draw the relevant frame.

Please note my examples are more C# like syntax as I am not familiar with python. The same fundamental principles apply though.

// load up other parts, such as animation[] an array of pngs  
int frame = 0;
png[] animation;
// begin loop  

DrawCharacter(animation[frame]);  
frame++;  
if (frame == 6) frame = 0; // prevent frame going outside of 0-5.  
// back to start of loop.

What this will do is draw the first frame of the sprite, then frame increments by 1 and when draw is called again it will show the next frame of the sprite. It will happen quickly and give the appearance of the walking animation. Depending on your game engine setup and FPS this may be extremely fast so you may want to only increment frame every 15 steps or so.

If you can't figure out splitting the png into frames in code, you could manually save each frame of the png yourself and just load each part in. (Remember arrays are 0 based).

animation[0] = "frame1.png"
animation[1] = "frame2.png"
animation[2] = "frame3.png"
animation[3] = "frame4.png"
animation[4] = "frame5.png"
animation[5] = "frame6.png"
\$\endgroup\$
4
  • \$\begingroup\$ Ok thanks a lot, I understand what you're doing here for animations. I also understand how I would translate this to python. \$\endgroup\$
    – Kyle
    Commented Dec 5, 2016 at 17:33
  • \$\begingroup\$ Though I wouldn't consider this an answer because I was just looking for syntax and on how to do certain sprite related things with pygame. Such as rotate it, change the actual pictures, get the rectangles, etc. The 'DrawCharacter(animation[frame])' is what im looking for, the pygame version. Since people who do tutorials on it uses extended classes and complex stuff like that. \$\endgroup\$
    – Kyle
    Commented Dec 5, 2016 at 17:34
  • \$\begingroup\$ @Kyle you say you can already draw a PNG to the screen, it would be the same thing but the current frame png no? \$\endgroup\$
    – lozzajp
    Commented Dec 5, 2016 at 17:35
  • \$\begingroup\$ @Kyle actually sprite sheets is very common technique, and of course you can do everything without classes, many people do. You can also use Python lists with surfaces. Probably you should formulate a more concrete question then, currently it is too broad. \$\endgroup\$
    – Mikhail V
    Commented Dec 13, 2016 at 23:47
0
\$\begingroup\$

If a tutorial is using functions or classes you aren't familiar with you should look them up. If a tutorial is using syntax you don't know you should look up a beginner tutorial. If you don't understand a function/class/syntax despite looking it up you could always ask about it specifically.

Considering you want to make a full game (and come from Java which is strictly object-orientated) you'd probably want to learn basics such as creating classes and single-inheritance first. You should especially learn naming scopes (such as global/local variables). You can make a game without making classes, but since pygame use objects like Surface you'd probably want some understanding about them.

To answer your question however, the pygame.transform module do offer functions to manipulate (resize, flip, rotate and more) images.

  • pygame.transform.rotate(image, angle) will return the image rotated by the angle.

  • pygame.transform.flip(image, x, y) will return the image flipped in the horizontal axis (if you set x=True) and in the vertical axis (if you set y=True).

  • pygame.transform.scale(image, (width, height)) will return the image scaled to the width and height you specify.

Just to name a few.

To get a rectangle the same size of an image it's as simple as rect = image.get_rect(). Remember though that a rectangle is an Rect object so you need to know how to assign and accessing attributes from it in order to use it effectively.

\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.