I have a feeling most pygame games are not "beautiful" because of the same reason my games are not.......because i want to program and not draw textures and sprites. I am not skilled at drawing, nor do i like it. I know a lot of other programmers have the same problem.
If you are skilled at drawing sprites, hired someone, or know someone to do the work for you, you would not have the same problems.
unity of command is python/pygame http://unityofcommand.net/
as well as frets on fire https://libregamewiki.org/Frets_on_Fire
Wow I just looked at. It's really bad. Each of those columns should be their own page. Compacting information like that makes it harder to absorb. People don't go to the site to hear about pygame news or reddit posts. People go there to find docs, tutorials, and code examples. The person who makes it should rearrange the order in which information is displayed. Maybe they could take some lessons from the Flask or Django websitse http://flask.pocoo.org/ https://www.djangoproject.com/.
> Neither the surface module nor the .blit command mention each other!
ehm... https://www.pygame.org/docs/ref/surface.html#pygame.Surface.blit
​
And do you know what a tutorial is? The documentation isn't supposed to teach you how to program, but to provide a comprehensive list of functions.
From what I see in your code it looks like you instantiate a surface with pygame.transform.scale() but don't assign it to any variable. Check the docs for that function, scale() returns a new surface instead of modifying the original.
Maybe replace
python.transform.scale(cross, (50,50))
with
cross = python.transform.scale(cross, (50,50))
> Looking for artist
The story of our lives. This tends to always be where the shortage lies in every group project of which I have ever been a part. I highly advise you try to move forward using open game art. You can make surprisingly good looking games with some of it, and a good working game may help attract the kind of artists you want.
On finding fellow programmers, your first priority should be setting up a git repo for your project and getting started. If you don't know Git, then please take the time to at least learn the basics. It is 100% necessary if you are going to try to make a successful open source project.
I would also reccomend you try to start the base of your game yourself. If you try to gather people and then try to discuss what kind of game to make together, chances are high you will never start. I have seen group projects have discussions like this for months and months and nothing ever come of it. If you give people something to look at they will be much more likely to join in. Also, without seeing what level you are at, people can't really judge if joining you would be appropriate for their level.
Also, it may be already obvious to you, but start small. Really small. Make a really really small game and then finish it. Title, Menu, Game, High Score; whatever you would expect of a real game.
Good luck to you. Post a git repo link when you've got it up.
The syntax for this function is:
scale(surface, (width, height), DestSurface = None) -> Surface
That means you need to know the size of the surface getting scaled, so you could write it something like this:newsurf = pygame.transform.scale(originalsurf, (int(originalsurf.get_width() * 1.5), int(originalsurf.get_height() * 1.5))
newsurf will contain the scaled surface you'll want. More information on scaling can be found here.
Edit: fixed missing parenthesis in example.
There's some advice on gui's here if it helps.
There is an advantage to doing things yourself because then you know how it all works and can modify it easily. I think there may be some pygame games out there which are open source.
Let me start by saying that the graphical assets are excellent. You should consider submitting them to opengameart.org.
The code, however, is in rough shape. The problem with your naming isn't so much that it doesn't follow PEP8, it's that there's no consistency. Were I to try and expand on what you've done, I'd have to constantly check which naming convention was used for any particular attribute or method. Honestly, I don't know how you kept it all straight in your head as you were writing it. Even if it's just for fun, following convention doesn't take any extra effort and makes your code easier to read and use so, why not? I think your code would get a much better reception if you did.
You never want to use python.time.sleep.
You want to use pygame.time.get_ticks().
import pygame
def main(): interval = 1000 next_tick = interval saying = ["hi", "hello"] count = 0
while True: ticks = pygame.time.get_ticks() if ticks > next_tick: next_tick += interval print(saying[count]) count += 1 if count == len(saying): break
if name == "main": pygame.init() main() pygame.quit()
In the simplest case you would check this condition every frame while your game runs. i.e. within your "main loop" which is fundamental to a pygame program and responsible for actually running your game.
I suggest you look here https://www.pygame.org/wiki/tutorials
The reason it isn't working is because you aren't updating your grenade's rect's position when you move the grenade. Change you grenade update to something like:
def update(self): if self.is_on_ground == False: self.speed_y += self.GRAVITY self.xpos += self.speed_x self.ypos += self.speed_y else: self.xpos += self.speed_x self.ypos += self.speed_y
if self.is_on_ground == True: self.speed_y = 0
self.rect.x = xpos self.rect.y = ypos
​
For future reference it is easy to spot this kind of error if you create 'debug_draw' functions on your sprites that uses pygame's draw.rect() function ( https://www.pygame.org/docs/ref/draw.html#pygame.draw.rect ) to draw an outline rectangle for all the rectangles you are currently using for collisions.
> because that's what coders use.
What? What is he "programming", HTML? You can use whatever platform or IDE you want. I use Notepad++ on Windows and Kate on Linux.
Anyway, the if statement leading to your quit() is the issue.
if event.key == pygame.QUIT:
That event is looking for an actual keyboard key. Here's the full list for future reference. To close the program with the escape key, switch out that line for:
if event.key == pygame.QUIT:
I'm working on adding this to my procedurally-generated sidescroller game, looking pretty good so far :)
(I didn't make the art, most of it is by Denzi)
I'll post more updates when I'm done.
In my opinion the best way to shortcut all the confusion with python libraries/packages is to download the PyCharm Community editor and use that to manage any libraries you use. It will also probably make you into a better python coder by giving you continuous feedback as you code.
https://www.jetbrains.com/pycharm/download/#section=windows
Once you have it you can get pygame by starting a project, navigating to File->settings->Interpreter and, once you have an interpreter (i.e. a version of python) selected for your project, pressing the green plus on the right hand side and searching for pygame in the list of available libraries. With this method it is now easy to add any new libraries you like in exactly the same manner with no faffing about.
I can also tell you that so long as you have a version of python 3 and a recent version of pygame it will make no difference to a beginner course. The only significant difference when starting out is between pygame 2 and pygame 3.
The method you described is definitely fine. If you are interested in something like pixel perfect collision, Here is a good resource to start from. With pixel perfect collision, alpha channels are considered and your pixel sword when colliding with an enemy will only be considered colliding when the non alpha pixels of your sword touch the non alpha pixels of your enemy. Hope that helps.
​
Do note that pixel perfect collision is slightly more expensive.
As for the clock: https://www.pygame.org/docs/ref/time.html#pygame.time.Clock
I just quote from the docs:
>If you pass the optional framerate argument the function will delay to keep the game running slower than the given ticks per second. This can be used to help limit the runtime speed of a game. By calling clock.tick(40) once per frame, the program will never run at more than 40 frames per second.
When you omit clock.tick() or leave out an argument, your computer will just try and run your game loop as fast as it can. This causes lots of problems, so you always want to cap the speed that your program is executed. So, clock.tick() basically says "wait a specific amount of time between game loop iterations".
There is an event for this. The window will receive pygame.ACTIVEEVENT events as the display gains and loses input focus. You can find more info about it in the documentation: https://www.pygame.org/docs/ref/display.html
Boo. Fixed Sys for EVERYTHING!.
But seriously, just look around for some open fonts. I do agree that a lot of times people make things so fancy they aren't really practically useful.
Check around:
http://openfontlibrary.org/en
and:
http://www.dafont.com/
maybe.
-Mek
These are specified on the pygame docs site, https://www.pygame.org/docs/
pygame.time.delay()
Causes the program to pause for a specified amount of time.
Will pause for a given number of milliseconds. This function will use the processor (rather than sleeping) in order to make the delay more accurate than pygame.time.wait().
clock.tick()
This method should be called once per frame. It will compute how many milliseconds have passed since the previous call.
If you pass the optional framerate argument the function will delay to keep the game running slower than the given ticks per second. This can be used to help limit the runtime speed of a game. By calling Clock.tick(40) once per frame, the program will never run at more than 40 frames per second.
Hi, just made this account to help you, but I installed pygame on my m1 yesterday
just follow this stop by step:
https://www.pygame.org/wiki/MacCompile
make sure to install homebrew first (I missed it)
​
if it does't work, feel free to reply back
blit
is a method in the Surface
class that draws the image you pass in (self.image
) at the given position ((self.x, self.y)
).
The image is drawn on top of the surface on which the method is called. In your example, said surface is stored in the window
variable.
So when you do window.blit(self.image, (self.x,self.y))
, you're telling Pygame to draw the image at the given position on top of the window surface.
You can find the documentation here: https://www.pygame.org/docs/ref/surface.html#pygame.Surface.blit
I apologize if I didn't make myself clear. I'm not really well-versed on Pygame.
I'm not sure about raycasting stuff, but cropping an image sounds like a good place for https://www.pygame.org/docs/ref/surface.html#pygame.Surface.subsurface
Nice video to learn the math, but you may want to comment that pygame.math.Vector2 already implements all that math optimized and in C.
see https://www.pygame.org/docs/ref/event.html
roughly something like this
for event in pygame.event.get(): if event.type == pygame.locals.KEYUP: # int print(event.key) # int (also in pygame.locals I think) # handle key up event # ...
Somewhere in your game loop you can use:
events = pygame.event.get() for event in events: if event.type == pygame.KEYDOWN: if event.key == pygame.K_0: num = 0 if event.key == pygame.K_1: num = 1
and you can use num as the input number. Basically the events are a list of input types. You check if the input was a KEYDOWN event and then check the key type using the pygame keyboard enums documented here https://www.pygame.org/docs/ref/key.html
left, top, width, height - in that order:
Top Left x-------- ^ | | | | | Height | | | --------- v <-width->
This is all direct from the pygame.Rect docs:
pygame.locals https://www.pygame.org/docs/ref/locals.html
is basically just a collection of constants (variables that don't change and stand for a certain value).
For example, the constant pygame.K_a has the value 94, which stands for the a key on your keyboard in ASCII code.
So, instead of having to type
if event.key == 94
you can type
if event.key == pygame.K_a
which makes your code MUCH more easy to understand.
This is what you need. Convert a surface to a pixel array, replace the color, convert back to a surface.
Take a look here: https://www.pygame.org/docs/ref/rect.html#pygame.Rect.colliderect
You can get a pygame.Rect from a Surface (image) by calling the get_rect() method.
Ps: for laserbeams, check the collidelist method (just beneath the colliderect method in the documentation)
Nice work so far. A few things I think you should look into (in addition to using a state machine):
Using masks for collisions would be much better for this kind of game
An animation cycle for the meteors. Even just a few frames to animate the tail would make it much more interesting to look at.
I am new to pygame, and wrote a simple missile command game. Use the mouse to aim and fire. There is no sound in this game at present. I posted to sourceforge, which I have just joined, so am not sure if I am linking correctly. The game is here: missile command
Ok I got it figured out.
When I was running
'pip3 install hg+http://bitbucket.org/pygame/pygame'
I was getting an error message: src/scrap.c:27:10: fatal error: 'SDL.h' file not found #include "SDL.h" ^ 1 error generated. error: command '/usr/bin/clang' failed with exit status 1
After trying to understand this error message for a while, I realized that I missed installing the SDL component. Ran brew install sdl and then tried to install pygame again and it imports beautifully. Thanks again for your help.
Godot is a relatively new open-source game engine, but it's great. Here's a list of features. Some things that I'm missing in many Python engines:
Here's a video that shows some features of the next Godot version 3.0.
BTW, GDScript is pretty easy to learn if you already know Python, but watch out for differences.
Godot is a very nice engine and I wish we had something similar for Python. Its own scripting language, GDScript, is pretty similar to Python, but there are differences and it doesn't have a big standard library like Python. The 3D part of the engine is not state-of-the-art (will be improved when Vulkan is more mature), but for 2D games it's really great. Godot unlike Unity and Unreal is completely free and open source.
The nicest engine for Python that I've checked out yet is Cocos2d, but I don't think it's developed anymore and the C++ version, Cocos2d-x, looks more mature.
I also wanted to take a look at KivEnt for Kivy, but couldn't even manage to install it (maybe had the wrong compiler or something with the Kivy installation was wrong).
Of course you can make nice things with Pygame as well, but if you compare it to Godot it feels really rudimentary and you've got to do more things yourself.
https://www.udacity.com/blog/2021/09/create-a-timer-in-python-step-by-step-guide.html
About half way down the page- don’t do anything with sleep or your program won’t be able to progress until the timer elapses. Best is to get a start time, then get the current time and subtract them- when the result is big enough, the cool-down is over.
A small little game where you dodge enemies who become more difficult to dodge as time goes on.
Not much, but it's been a game I've worked on by myself for the last 5-6 months.
I'd like to use something like those reflections to make the sense of depth better in a game jam game I'm finishing up for: https://itch.io/jam/extra-credits-game-jam-4
ok, I did some work on it and yes I think, with your help, I found out the problem (but it wasn't easy).
python -m pip install -U pygame --user
3) opened up Visual Code Studio and had to select the NEW interpreter (this is where is was tricky because of the custom python path). Alot of Cntr:shift:P. Followed the instructions to get to the new path for VCS to find c:\python\python.exe
https://code.visualstudio.com/docs/python/environments
4) restarted VCS
5) cntrl:shift:p to select the correct interpreter (the new path with python 3.8)
6) Now recognizes "import pygame" without issue.
This might be easier if you use the installation path and you could skip steps 3 and 4 BUT, there is a lot of work getting thru the path of USERS/Appdate etc to get to python 3.8
The Avast UI didn't have any obvious log information but I did narrow down what is happening a bit. I disabled the "DeepScreen" feature and the .exe works normally every time. It looks like it's their way of sandboxing unknown applications to test for malicious behaviour.
A quick google search revealed a bunch of other people with this issue. I'm guessing the only resolution is to tell users to whitelist the exe in their antivirus... which unfortunately is a massive usability hit these days.
Examples: https://feedback.avast.com/responses/deepscreen-is-playing-games-with-my-c-net-40-app http://superuser.com/questions/555905/stop-avast-from-attacking-my-own-programs
I used Learn Python the Hard Way and Codecademy to learn the basics of Python, then moved on to the Program Arcade Games book that Mek mentioned.
I've got a decent grasp on the language and basics of Pygame now. From here I've just been learning from other peoples' code, and I have a small game that's slowly coming together.
If you're looking for some help on the paintball game, I'd love to help tackle this project! I've never played HVPB, but I love playing actual paintball, so there's that. Good luck!
This is where I have gotten to so far. It is a bit rough and ready, and not very pretty. Load doesn't work so ignore it. Click the buttons to select which items to view, use left and right arrow keys to move among them, and up and down arrow keys to change colours. Massive thanks to /u/xmzhang for the srcalpha idea!!
https://sourceforge.net/p/sprite-builder-master/code/ci/master/tree/
Okay, I managed to upload it to sourceforge. I'm sure when I signed up there was a tutorial that took you through the process and explained all the git stuff but I can't seem to find it now (its not the one in the help docs for git on there.)
So, I hacked this out originally, with a couple of classes, and got it displaying, then added the event detection, then added the valid move check. Then I decided I wanted to use the state engine stuff from the skeet shoot challenge, and oh my god did it get in a mess. You know that point where nothing works any more and you know it will be ages until you can even just run the program! Felt like pulling my hair out, only I don't have any hair anyway, so couldn't even do that!!
On the plus side, some firsts: First time using the if a < x < b: form for conditional checking, first time separating classes into separate modules and importing them, first time using images in a pygame program, and first time using the state engine stuff, so progress!! (I had to hack the state engine and game class code a bit to get it to work, also had loads of indentation problems with gedit, but I like gedit and don't want to change - I have no problem writing/saving and then running stuff I have written in it, only when I copy other code in there that I get these issues.)
Anyway, it is here: https://sourceforge.net/p/boggler/code/ci/master/tree/
Press the done button when you have a word to add. I left the checking until the end because basically I suck at this game, so even just getting a couple of 3 letter words was a victory lol!
On top of those, "A Dark Room" is the best idle in-browser game I've ever played.
It starts super simple, but it turns into this massive game and it's a blast to play. There's imagery, a captivating story, and rewarding progress. I can't tell you much about the gameplay, you'll have to trust me on this one and give it a try.
Yep, (very) long time user of Linux in general and Ubuntu in particular. But, relatively recently I switched over to Manjaro (Manjaro - enjoy the simplicity), an Arch-based Linux distro and I'm loving it. If you haven't already, you should check it out.
Hmm odd. I don't know of any inherent problems for SDL/Virtualbox. Are you sure it's not your configuration for sound? Also, is the primary OS (not the virtualbox) also linux?
You could try this: uninstall pygame and SDL with apt, then grab it from http://pygame.org/download.shtml, and the SDL lib from http://www.libsdl.org/download-1.2.php. Install them manually and see if that helps matters.
Alternatively you could try PySDL, which has quite recently made the switch from SDL 1.2 (what pygame used) to SDL 2.0. Pygame is really just a (very convenient!) wrapper for SDL, which means you wouldn't have to do a whole lot in true SDL to get something pygame-like.
> Distribution is unwieldy, and unless your audience have Python and pygame installed you'll need to get to grips with complex tools like py2exe, which i personally have totally failed to successfully export any my games with in the past. Also, unlike Flash and Unity there is no web player so users will have to download your game, significantly reducing your potential player numbers.
I've had good success with cx_Freeze for building Windows executables for pygame + Python 3.3. I have to copy over my subfolders for things like graphics and audio, and the ogg lib SDL uses (libvorbis.dll, I think?), but other than that it is pretty simple. Additionally, if you use the font module you might have to copy a font file over into the library.zip file (inside its pygame folder, if I recall) that cx_Freeze will create. This might be true only with the default system font pygame uses--I am unsure because I don't use the bundled font module anymore.
To be honest i dont ever use pip to install anything. So the first one i couldnt help with (but that doesnt mean someone else wont later respond). The second error is you do not have mercurial installed (hg). Install that first. http://tortoisehg.bitbucket.org/ And if you do that you might as well execute the commands i gave above. The third...oh whoops forgot your on windows. Just download the msi one from the link above that corresponds to you python version. For example "pygame-1.9.1.win32-py2.7.msi " is pygame 1.9.1 for 32 bit windows for python 2.7 version. However this is an older version. Cloning the repo via mercurial will get you the latest
That was my best guess! Haha. I know you can test to see if it installed correctly by doing import pygame in the IDLE shell. You should get a response of "pygame 2.0.1 (SDL 2.0.14, Python 3.9.4) Hello from the pygame community. https://www.pygame.org/contribute.html" or something along those lines depending on version
When in doubt. Print it out. Doc
pygame clock returns time it took in milliseconds. I just change it to a float. 16 * 0.001 equals 0.016 .
pygame clock always return time it took. Even if you don't use it. So you just repeating it with time.time().
Hey so I am not at home, so I cant help much but you can change alpha transparency of your circle surface to like 50 or smth. Test it out.
Documentaion: https://www.pygame.org/docs/ref/surface.html#pygame.Surface.set_alpha
Not really. It's just a group of sprites effectively. Have you read the sprite section of the pygame docs website? (https://www.pygame.org/docs/ref/sprite.html)
Setting seconds=5
does nothing since seconds
will be overritten in the very next iteration of the main loop, and for a certain amount of time, it will be set to 0
and thus calling replicate
again.
You either need another flag, change start_ticks
, or trigger the calling of the funciton with a timer (which is an easy way to do something every X seconds).
As you are saying "limit" as something different from "resize" and "clip" I'll guess you want to actively control how many characters you write. In this case use Font.get_rect() to get the size of the rendered text. Use a while loop to keep decrementing the number of characters until the returned size fits the allocated space.
I'm guessing that you need to set an alpha color (with the set_alpha()
method) and then use convert_alpha()
instead of convert()
when you load the player image on line 9.
https://www.pygame.org/docs/ref/surface.html#pygame.Surface.set_alpha
Is there just one grass block? Or many? If there is just one, you can just set rects for each object, update the rects to move them and test for collision using colliderect:
if player.rect.colliderect(grass.rect)
If there are multiple grass blocks then add them to a spritegroup and test for collision between the player and all the blocks using spritecollide:
if not pygame.sprite.spritecollide(player, grassgroup, False): player.move(direction)
Obviously this is dependent on the direction of movement.
see:
https://www.pygame.org/docs/ref/sprite.html#pygame.sprite.spritecollide
You can use surface.set_at((x,y), color) for this.
for x in range(rect.x, rect.x + rect.width): for y in range(rect.y, rect.y + rect.height): surf.set_at((x,y), array[x, y]) # or however you access numpy array!
In your example, the numpy array is bigger than the surface, though?
https://www.pygame.org/docs/ref/surface.html#pygame.Surface.set_at
Use Sprites.
Make a person class, subclassing sprite;
class Person(pygame.sprite.Sprite):
def init(self, img, x, y): pygame.sprite.Sprite.init(self) self.image = img self.rect = self.image.get_rect() self.rect.x = x self.rect.y = y
Then create 2 sets of the same people, one for each player. To display them you would store all the people in a list, and then run through the list setting the rect x and y variables.
player1_people = [] x = 0, y = 100
load image and make person object using x and y values, and add to the list. At the end of each row, reset x to 0 and increment y by the image height.
make a sprite.Group called all_sprites, and in the update and draw methods do:
self.all_sprites.update() #and self.all_sprites.draw(screen)
Have a look at the sprite docs on the pygame website:
https://www.pygame.org/docs/ref/sprite.html
pm me if you need any more help.
Hello,
A common mistake is to update the sprites without using the Layerupdates group (sprite without a layer will be display onto the screen in the order they are received/processed). In your case if your are using a normal sprite group instead of pygame.sprite.LayeredUpdates then your status bar might be buried underneath other sprites.
If you are interested in displaying sprites in top of each others in a specific sequence then LayerUpdates group is a must.
Also when creating your status bar make sure to set the alpha channel to 255 otherwise your sprite might be partially invisible when updating the RGB color using a gradient (only if you are using a 32-bit surface).
How are you planning to create your status bar ? are you using a gradient method (numpy)? or changing the Surface color according to your player life point?
Kind Regards
No worries, just happy i can help
Use this function : pygame.key.get_pressed()
Doc here: https://www.pygame.org/docs/ref/key.html#pygame.key.get_pressed
The difference is, this gives you the key state at any given time (whether it's being pressed or not). From that you can give instructions on movement. Example, "oh, the right key is being pressed, let's move to the right, the upper key isn't being pressed, no need to do anything".
Because atm you're trying to track when the key is being pressed and when it's being unpressed to conclude if you need to move or not.
The difference is kind of subtle. But it'd make the code half smaller and more readable.
Also not sure why you are setting these move variables to True and False?
Just to add on:
for event in pygame.event.get(): if event.type == pygame.KEYUP: if event.key == pygame.K_a: # handle key up event # ...
You can find all the key constants here: https://www.pygame.org/docs/ref/key.html
I think you could use the clamp_ip() function when you move the character's rect.
You can pass:
collide_mask to spritecollide() to specify the collision type. There is also a group collide function that works the same way, but between groups instead.
You can try and have a look at the function mask connected components and other similar ones. Seems to be a way to connect a bunch of masks for connected sprites
As you are using pygame 2, you could ignore dealing with indices by using pygame.key.key_code()
and passing in the name of a key as a string (keys can be found in docs). According to the docs:
pygame.key.key_code('return')
is the same as pygame.K_RETURN
.
In your case:
pressed = pygame.key.get_pressed() key_to_check = pygame.key.key_code('w')
if pressed[key_to_check]: ...
All the things in your second-to-last paragraph are able to be accomplished with pygame:
rotations:
https://www.pygame.org/docs/ref/transform.html
palette swaps:
https://www.pygame.org/docs/ref/surface.html#pygame.Surface.set_palette
So you just missed it in the docs, no worries. As for having other files, you will just need to get used to that. You aren't going to be making animated sprites in hard-coded lists of lists, you'll save .pngs and import them, likely (eventually) in a spritesheet.
Here's my take on this: you figured something out, and learned in the process. Hell yeah! But not you've got a tool and are looking for a use-case. That's not how you should approach it. You should start with what you want to do (your use-case), and then find the best tool.
You could turn your row 1 through 8 into a numpy array and use pygame.surfarray to create a surface for blitting. Then simply scale it up as required.
> As long as the surface contains alpha, and has the flag, at the point I blit it, am I right in thinking it doesn't matter that surfaces it is then blit to dont?
That sounds mostly right yes. The blending will be done at the moment of blitting, so as long as you aren't expecting the surface you blit to to continue having transparency on another surface afterward (if you blitted to a surface and then blitted that surface to another surface).
​
> Also, can you make an existing surface partially transparent, rather than doing a solid colour fill?
There are ways, yes but there are some restrictions in pygame 1 that are no longer present in pygame 2. You can experiment by using :
https://www.pygame.org/docs/ref/surface.html#pygame.Surface.set_alpha
​
In pygame 1 you can use this flag on surfaces without per pixel alpha, and in pygame 2 you can use it on any surface.
Did you even try to put in movements??
You don't even have a position set. This is literally what is required to start the system and doesn't even have all the information filled out. There's no FPS set here, .flip isn't called.
Here's the documentation on keyboard inputs: https://www.pygame.org/docs/ref/key.html
Please read them. it's important.
I think this is a good idea OP.
Another thing is to work on the tooling. So I would want a text_box class which I could call with a function like
tb = text_box(rect, text)
and it would know how to draw the box and write the text in it, how to wrap the lines and how to split it into multiple pages with dots at the end of the line if it's two long for one page etc. This might help.
It might take a while to write the class but you won't regret it when you are making the game and can just easily call it whenever.
Feel free to ask if you need more help.
pygame.quit()
should not be necessary. Most of the time I don't use it in my apps.
https://www.pygame.org/docs/ref/pygame.html#pygame.quit >When the Python interpreter shuts down, this method is called regardless, so your program should not need it, except when it wants to terminate its pygame resources and continue.
Shouldn't your rock (and ball) class declaration look like this:
​
class rock(pygame.sprite.Sprite):
I'd also move the call to the Sprite's __init__ function call to the top of your __init__ function so that it doesn't overwrite any of your elements.
You also need to check for None in the mask check because it returns a point or None rather than True or False. From the docs:
> [sprite_collide_mask] Returns first point on the mask where the masks collided, or None if there was no collision.
e.g.
for i in rocks: if pygame.sprite.collide_rect(i, Player): print(1) if pygame.sprite.collide_mask(i, Player) is not None: print(2)
The other thing to check is that you have a surface loaded in with an alpha channel available. i.e. your rock and ball texture images have an alpha channel (i.e. they are png images) and when you load it in you aren't erasing it with a convert() or something; see.
​
​
Awesome! Just bought a copy, and am looking forward to trying it out later on my windows laptop.
There's a news post on the pygame front page now: https://www.pygame.org/news/2018/8/duga-1-0-the-first-person-shooter-is-out
If you need help packaging it up for other platforms, please let us know. I'd help, and I'm sure other people would too.
No don't worry :) it is absolutly normal! In fact each key is represented by an integer wich is defined in pygame's code, so when you print it you actually print an integer wich represent the key
From pygame.key documentation: "Both events have a key attribute that is a integer ID representing every key on the keyboard." If you want to see the name of the key and not the integer, use : pygame.key.name(<the key>)
Pygame.key documentation : https://www.pygame.org/docs/ref/key.html Hope it helped ! :) Ps: sorry for my english I'm still learning ^^
I think because unit tests are supposed to only test one small thing, you should be able to just learn about the one thing that it is testing. So in some cases, I don't think you'll need to learn too much. The https://www.pygame.org/wiki/Contribute has all the information about getting started with git, github and unit tests.
Feel free to drop into the discord chat (link in the news post above), and come into the "contributing" channel. A few people have been getting started and asked questions already, and we should be able to help you out.
I've not really anything interesting but I thought I'd post it here anyway. It's just paper scissors rock, nothing too exciting, or new. It has a few bugs but nothing that'll affect the gameplay.
Game: https://www.pygame.org/project/3634/5724
I'm having trouble finding a good game idea and sticking to it.
music is played non-blocking in a background thread. if the main thread stops, all threads are closed, including music. put the following in a loop and exit the program when music stops.
https://www.pygame.org/docs/ref/music.html#pygame.mixer.music.get_busy
You can use the <code>get_flags</code> method of your display surface and check which bit is set with the bitwise AND &
.
For example to check if your display surface is a full screen display, you can do this:
if screen.get_flags() & pygame.FULLSCREEN:
Perhaps the docs will help find your answer:
Some platforms require the pygame.mixer pygame module for loading and playing sounds module to be initialized after the display modules have initialized. The top level pygame.init() takes care of this automatically, but cannot pass any arguments to the mixer init. To solve this, mixer has a function pygame.mixer.pre_init() to set the proper defaults before the toplevel init is used.
It is safe to call this more than once, but after the mixer is initialized you cannot change the playback arguments without first calling pygame.mixer.quit().
https://www.pygame.org/docs/ref/mixer.html#pygame.mixer.init
So if you call pygame.mixer.init() after calling pygame.init(), it sounds like you need to initialize a display module before mixer will work.
I think that you want:
self.amide_sprites = pygame.sprite.Sprite()
to be:
self.amide_sprites = pygame.sprite.Group()
Here's the documentation for Group(): https://www.pygame.org/docs/ref/sprite.html#pygame.sprite.Group
Its slow because of all the image loading and converting ("grab_image"). When you are making your tiles, you can just convert the main image, then make subsurfaces from the main one, no need to re convert. You'll want to cache the tiles as well, instead of lading the image and extracting a tile each time. You're also loading each level, which is slow. Just load the one you need at the time.
All of the declarations to place the tiles is not ideal, but it isn't causing the slowdown.
https://www.pygame.org/docs/ref/surface.html#pygame.Surface.subsurface
oh duh, sorry, yeah you linked it. My biggest problem with that one is they do not use object oriented programming (OOP). But you will get some gaming experience by making those, as well as learning pygame a little more. Just be aware that no one structures their programs like that, and as your games gets larger it turns into spaghetti code. (as if you were to expand on one of their games)
to get the link above i just googled http://lmgtfy.com/?q=pygame+sound+example
EDIT: you can use pygame.org docs to find such features out https://www.pygame.org/docs/ref/time.html
To change the song that the mixer is playing you need to stop the current song, load the new song then play it (just like with the first one). You could also make a queue of songs, but that won't work if you need to stop the first song after ten seconds.
There's a sweet tool called Tiled that's free and works wonderfully just for this. It cuts a tile sheet and numbers the tiles. You can then build maps and output a csv file with the tile placement numbers. You'd just have to change your game to read in csv files and index tiles by number instead of letters.
An upgrade to the website would be very nice. For starters, when I first started with pygame and visited the website it looked like some hobbyist project by 1 person that was only capable of making crappy blocky games. That's definitely not true, but the visuals give that impression and a beginner won't know any better.
Hopefully a new look will bring more people in. Take love2d for example. Nice and clean and has featured (good) games on the landing page.
Also an upgrade in the backend would be nice but we'll see how much that breaks stuff!
A Standalone framework, maybe even a fork of PySDL2. Just geared around simplicity.
Take a look at Löve, a 2d game framework for Lua. Look at the code examples. That's their FRONT PAGE, their code is so simple its a FRONT PAGE FEATURE. That's what a 2D gaming framework for Python should look like.
I love Pygame, but it's old, and Kivy, while nice, uses a widget system, its own language in .kv files and is not exactly beginner friendly.
I dunno, it's just an idea really.
If you want to create a 3D game with Python, I'd rather recommend checking out Panda3D (or Godot but it has a different scripting language that's only similar to Python (the Python plugin is in the works)).
Of course you can make 2D games in a 3D engine as well, so it should be possible to mix both 2D and 3D.
Learning OpenGL would be more useful if you want to create your own engine.
[https://hastebin.com/xijisuhugo.py](alien_invasion.py)
[https://hastebin.com/foyevapebo.rb](ship.py)
alright, these should be (unless I fubr'd them copying them over) be updated with working code showing how I fixed the issue by using a copy of the last draw position (see ship.py)
I've read this a number of times and made a continuously scrolling background but not a tracking background. Hoopefully the answer you seek is somewhere in there.
The second argument pygame.draw.polygon
receives is a tuple or Color
, both of which can have an alpha value (transparency). If you add another element to (0, 0, 0)
, that's going to be the alpha value.
See https://www.pygame.org/docs/ref/draw.html#pygame.draw.polygon
pi@raspberrypi:~ $ ./t.py
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
pi@raspberrypi:~ $ sudo -u pi ./t.py
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "/home/pi/./t.py", line 4, in <module>
pygame.mixer.init()
pygame.error: No available audio device
pi@raspberrypi:~ $ cat t.py
#!/usr/bin/python3
import pygame
pygame.mixer.init()
You can create a timer using pygame.time.set_timer()
. It fires an event every x milliseconds that you can catch in the event queue.
Details here: https://www.pygame.org/docs/ref/time.html#pygame.time.set_timer
The Pygame site has a page of tutorials:
https://www.pygame.org/wiki/tutorials
Here's one on YouTube that I thought was good, covers all the basics, animation, displays, text on screen:
I'm not quite ready to release this code yet, but I did release the code for another game I made for my other son a few years back. Check it out if you are interested. It's some pretty old code but it's not terribly done :) Things could have been done differently. There is a google drive link near the bottom of the page https://www.pygame.org/project/3018
What are you trying to do? Unless your trying to burn power, busy loops are almost never the answer.
If you're trying to limit frame rate, https://www.pygame.org/docs/ref/time.html#pygame.time.Clock.tick_busy_loop maybe be useful.
If you're trying to make things happen at the same speed regardless of frame rate, https://www.pygame.org/docs/ref/time.html#pygame.time.Clock.tick is probably what you want.
You can check this game out here: https://play.google.com/store/apps/details?id=org.andyrusso.sbar
Basically its classic Snake game with controls optimization so you can move snake around just with one finger.
Looks really good :)
​
I noticed your itch.io release is for windows. I'm curious what you used to compile the code? Was going to give it a go, unfortunately I'm rocking linux at home these days lol...
Sorry for coming late with this, but I modified the code a little bit to calculate the distance with given intersect point, but now it sees it's back also as something to interact with tho I only want it to be able to intersect it with it's front (still being rotatable) I put the code on https://hastebin.com/mamasatuga.rb
np, do what the others said. also, there is an application that is called sharex. it helps making screenshots of your screen and uploads them automatically to a filehost of your choice. after making a screenshot, you can just press ctrl+v to paste the link to the last screenshot you took.
it also has the ability to upload code snippets to pastebin or other platforms. same as with screenshots, it copies the url to the code to your clipboard so you can paste it with ctrl+v when making a post.
Well hitboxes, as you may know, are represented by rects. You basically create rects for all your game objects, and then check if they are colliding with colliderect()
. Rects also have a bunch of positional attributes which can be directly changed. A full list of them are on the docs, but here are some to give you an idea.
rect.x rect.y rect.width rect.height rect.center rect.right rect.bottom
Most of these are self explanatory. Rect.x represents the x position of the topleft pixel, and width and height represents the size. Rect.right is sometimes useful as well, since it represents the x position of the right side. In other words, rect.right is equivalent to rect.x + rect.width.
Hopefully this answers your question. Just know that creating moving hitboxes is the same as creating idle hitboxes; all you're doing is changing the positional values.
Oh yeah, and calling get_rect()
on a surface will return a rect of the same size. Use this to generate hitboxes for images, which I imagine your notes will be.
A good forum thread that explains the concept of "lerping".
The easiest way to measure time is in frames. If you want the total time to be 1s, that's 60 frames if your clock.tick is set to 60.
So the code would look something like this:
startV = pygame.Vector2(0, 0) finalV = pygame.Vector2(0, 0) counter = 0
while counter < 60: progress = counter / 60 currentV = startV.lerp(finalV, progress) counter += 1
# Vector objects can be used as usual tuples for object positions screen.blit(texture, currentV) pygame.display.update()
you can use get_rel to get how much has the user move the mouse that frame. store it and add it frame after frame, once it is over 10 move the cursor to the new position, meanwhile keep positionting the cursor at the point