Reminds me of this one guy who posted a lot of sample code in the Pygame documentation years ago. His function and variable names randomly had shit like "HE_HE_HE" and "WEEE" and "WOW" all over the place. Looks like it's still there in the comments for pygame.draw.circle(). Truly a sight to behold.
> Are there tutorials for these?
Of course!
Just google 'modulename tutorial' for almost any major package and you will probably get numerous results. It is fairly routine for major 3rd party modules and packages to have a 'how to'/'introduction'/'getting started'/'tutorial' in their own documentation. For instance, here it is for PyGame.
But tutorial or no, referencing the documentation and google searches are critical to learning modules and even programming in general.
Also, for the standard library in particular, Python Module of the Week is a cool reference.
From the pygame wiki:
>Pygame is free. Released under the LGPL licence, you can create open source, freeware, shareware, and commercial games with it. See the licence for full details.
And here's the license they link to in the docs: https://www.pygame.org/docs/LGPL.txt
Great video dude. Very well explained and straight to the point. I can see your channel growing as large as codebullet or carykh in no time.
I just checked the project on github and the codes looking really good.
Now you've got nn's and genetics working once, you can easily pass your skills onto another project.
Isn't learning fun :)
Also, if you end up falling in love with pygame like i did. I recommend reading the Newbie Guide, specifically the dirty rect part. It'll allow the simulation to run at 100's of frames per second instead of the 28fps limit on pygame.display.flip()
Keep being epic :)
Lo conozco porque tengo uno sobre la librería en mi lista casi-infinita de cursos de Udemy sacados gracias a cupones. Igual la página de la librería da acceso a algunos tutoriales como para empezar e ir probando: https://www.pygame.org/wiki/tutorials
The rest of the mainline series and one of the spin-offs are available on mobile, and the Android/IOS ports of Apollo Justice onwards are pretty good I hear.
As for similar experiences, I personally recommend Ghost Trick, which was written by the the same guy responsible for the Trilogy (it's only on DS and IOS, though.) The gameplay is different (still good though), but the tone and character writing is virtually identical and the overarching mystery is very compelling, with some sublime twists.
The Layton series has less in common, but it did have a crossover with Ace Attorney once, so there is some overlap in the fanbases. Those games are primarily on DS and 3DS, but a few of the games have recently made their way to mobile, including the original three.
Danganronpa gets suggested a fair bit. In some ways, it's very similar to Ace Attorney, and in others it's polarisingly different. Because of that, it's hard to know whether you'll enjoy it or not until you go ahead and play it. The series is on Steam, PS4, PS Vita, and now mobile. Zero Escape is another popular visual novel franchise, but it's closer to Danganronpa than Ace Attorney, so I'll recommend the former first.
If you want something as close to Ace Attorney as you can get, but are willing to occasionally vary in quality, you can experiment with some fan cases/games. Ace Attorney Online (not to be confused with the AA-themed chatroom, Attorney Online) is a website for making fan cases, and has a featured page you may want to stick to. Meanwhile, PyWright is a dedicated program for making full AA fan-games, with the main two on there being Conflict of Interest and Contempt of Court.
Hope this helps!
> 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.
Your traceback indicates the issue comes from line 42, which includes:
win.blit(textsurface1(250, 250))
After checking pygame docs a bit, I think you might be missing a comma:
win.blit(textsurface1, (250, 250))
The code is interpreting textsurface1(...))
as a function call, but it appears that the pygame.Surface object can't be called that way. Instead, I think you intend to use <code>pygame.Surface.blit()</code> with the first two positional arguments, source
and dest
.
The same appears to be true on lines 43 and 44, where you have textsurface2 (250, 250)
. That space in between leads me to suspect you meant to add commas. :)
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))
I don't know much about pygame, but after 2 minutes of looking at their docs, I think pygame.mixer.music.queue might be something you'll want to play around with.
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.
If you are learning Python, the best option for you at the moment is https://www.pygame.org/
There is no need of jumping to another language or worrying so much about which engine is better. You are learning, so give Pygame a try without the stress of changing to C#, and when you get confident you can check other engines if that interest you.
> Pygame, though being the most popular one, seems pretty much dead as the last release was in 2009
Pygame eventually started getting releases again. According to https://www.pygame.org/news, the last one was in January 2017.
> Kivy, PySide and PyQt look more like a general GUI thing
Kivy basically combines a low level graphics api comparable to pygame's (although the api is quite different - and much more modern, as it's based around modern opengl rather than blitting), and a widget toolkit. If you want to make pygame-level games, you can ignore the widgets without issues, although in practice they're very convenient for managing things like menus even in simple games.
You might also be interested in kivent, a game engine written for Kivy.
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
> Is there a way for me to just write it into a .txt and have it run?
Not quite. But, for a little 2D game Pygame is about as close as you get to something that simple. https://www.pygame.org/
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 think the flask one isn't that bad, minimalistic for sure but not outdated. (Atleast that's what the homepage looks like on mobile)
I especially love the pygame documentation.
Take a look at pygame, the documentation is eye-watering but it covers everything you need to know. There's plenty of tutorials (video and text) out there to help.
The best way to learn is to just get stuck in; if it doesn't work, find out why and learn from it.
Check out PyGame. It's a huge library, and card games require quite a lot of assets (at least 52...), so it might be a case of working on a simpler PyGame project, then returning to your card game. But it's what you're looking for.
Next, I would recommend utilizing classes to represent the player, monsters, objects, etc. After that, learn more advanced libraries like:
or
Happy coding 🤓🎮
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".
If it's just a small hobby game, then I wouldn't worry about it. If you want to add 2D graphics in the future, you could always try Pygame, and adapt your code to use it.
First of all, for the love of Guido, format your code using 4 spaces in front of it (or better yet.. Put longer pieces of code like this in a GitHub Gist or on Pastebin).
Second of all, this is YOUR learning experience. Nobody is going to just write the code for you, especially not if it is for school.
Look up PyGame's documentation on how to draw a picture to the screen.
Edit: Since it very much reminds me of a tutorial on pythonprogramming.net, here is the part of the tutorial that you might need.
You should be able to do with with a LOT of work. Best bet is to use pygame, getting the state of the switches with gpio and python. and mashing the two together. That should be enough for you to get started. Good luck!
Whenever I want to do something like this in pygame I simply start from the docs: https://www.pygame.org/docs/
(Basically a list of every command and how they can be used)
From then onward just break the graphical parts of your problem into steps and learn the commands for them. For instance, in Cellular Automation, there would need to be cells drawn which would be either squares or circles. So I'd get the relevant command for drawing a square (pygame.draw.rect()), then change its appearance based on given criteria as is necessary in Cellular Automation.
Especially while you're learning, having the docs open all the time is incredibly useful. I always do when I'm working with an unfamiliar module.
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
I've mostly used this reference for Pygame. I can't comment on all aspects of it, as I mostly just use the reference. It always seemed up to date for me, though.
As an aside, you should definitely make sure you know Python well before trying to dive into graphical games. Most resources will assume you already know how to program with Python and will only teach you how to do game dev with it.
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.
There's a game engine for it called pywright. You might want to use it instead.
Disclaimer: I haven't used it.
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.
Python is the best all-around language for learning programming.
Learn Python the Hard Way is generally well-regarded. It's a free ebook but they also have an option for buying extra tutorial videos.
I think Khan Academy has some free Python programming stuff too.
Pygame is a framework/library for making games in Python. Haven't used it since a class in college but I really liked using it.
Be forewarned, though, that within software engineering, the game development world is absolutely brutal. 80+ hour weeks during "crunch time" are considered normal. Lots of people who went into gamedev because they loved gaming end up either burned out or without any free time to actually play games.
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:
Sure can. You can use Pygame for making 2D games. Alternatively, you can use the built-in turtle module to make simple 2D games as well. You can learn about how in a tutorial I made for my students found here: Pong in Python 3 for Beginners. Good luck!
Are you a developer? If so, then Unity is easy enough to pick up.
If you are new to coding, then python is pretty easy to pick up, and has a module called pygame for creating games.
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)
> OP, who clearly has never programmed with Python, just wanted some realistic things that are possible to create, that others have created using Python...
You can do pretty much anything in Python that you could do in any other language.
> OP, I don't use Python (nor am I really that experienced in programming),
And yet you felt qualified to criticize my response?
> For example, you wouldn't really pick Python for making a game.
I take it you've never heard of Pygame, then? Or any of the myriad games that embed Python for use as a scripting language?
First off, you should look into a few of the basic Pygame classes like Rect and Sprite to help you manage some of the things you are already doing.
If you start using a Rect to track your player
, it can be used for both rendering and collision detection. Rect.collidepoint() can be used to test if a single point (or snowflake) is inside of your player rect. Adding this simple check in your for i in range(len(snow_list)):
will let you check for the collisions you are looking for.
On a more advanced topic, you can use SpriteGroups to track multiple sprites and test them for collisions with each other.
I don't know about any libraries but you could make your own simple solution.
One way might be to load all the images of an animation into a list and then loop through it to display the frames. Something like:
import pygame
frame_list = [image.load("frame1"), image.load("frame2)...]
The list would just be a bunch of pygame Surfaces - you could probably find a better way to construct the list.
Then in your sprite's class:
current_frame = 0 #the frame we're currently displaying num_frames = 5 # number of frames in the animation
def update(): sprite.image = frame_list[current_frame] #set the current frame
current_frame = (current_frame+1)%num_frames
This code is pretty rough (it's a while since I've used pygame!) but it'll give you an idea.
The last line uses the Modulus operator so that we can can add 1 to get the next frame and if we've reached the end of the list we'll go back to 0 - the first frame.
This is a basic 'circular array' - which allows us to loop through a list of values infinitely.
If you want to do some sort of sprite sheet animation you could have a texture and instead store a list of pygame Rects. Each Rect would be the position of a frame in the animation.
It looks like you're trying to install a version of pygame which only supports Python 2. You need 1.9.2 or above for Python 3.
If you use pip
(a package manager which uses PyPI, the Python Package Index), it should fetch the right version by default.
I don't use Windows, so I can't offer specific advice from personal experience, but this documentation explains how to install modules for Python 3.5 with pip
and is OS-agnostic.
From Python 2.7.9 and 3.4 onwards, <code>pip</code> is included in the standard Python install, so you shouldn't need to install anything extra in order to use it. So watch out for old instructions that assume you have to install pip
first.
So the best frameworks that use Python would probably be pygame or kivent (built on top of the GUI framework: Kivy).
I recently learnt about a cool extension to pygame called pygame-zero which strips some of the boilerplate from generating games with PyGame.
Though I'd only really recommend them for 2D games, even though Kivy has some 3D support.
You're obviously going to get better performance from something like Java, C++, or C#, but Python is still an awesome tool for experimenting with AI and basic game development, though you'll probably be creating a decent amount of the actual game engine yourself, whereas Java, C++, and C#, have frameworks that make getting to a running simple game a lot faster.
There are some great game engines written in other languages that will definitely give you better performance and will have more support, like Unity, or UDK, , but there's nothing wrong with getting started with JavaScript or Python while you're developing simpler 2D games as a start.
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.
From googling "Pygame events":
https://www.pygame.org/docs/ref/event.html
KEYDOWN (unicode, key, mod) KEYUP (key, mod)
Looks like there are KEYUP and KEYDOWN events.
Nice KEYDOWN example, from googling "pygame KEYDOWN example":
http://stackoverflow.com/questions/25494726/how-to-use-pygame-keydown
I've never used pygame, just wanted to show the type of search you could do to find an anwser
https://www.pygame.org/docs/ref/transform.html
Describes multiple methods to manipulte images. One of them is the pygame.transform.rotate() function.
Here is a stackoverflow post that explains how to use it:
http://stackoverflow.com/questions/19316759/rotate-image-using-pygame
Look at accepted answer to the question.
Once you manage to get it rotating you just need to generate a random number to pick one of your values. Take a look at https://docs.python.org/3.4/library/random.html
random.randint(a, b) is what you would probably want to use and then rotate the image by 360/(number of values) times the position where your selected item is.
Sure! The hardware is a Pi2, a cheap Wifi dongle, and a standard Microsoft Lifecam brand USB webcam. It is powered with just a long extension cord and the standard usb cable. (I was lucky that the bird setup shop in a hanging flower basket on my front porch :D)
I started with getting basic webcam functionality using the pygame library.
Once I got that working, I checked out the Raspberry Pi Twitter Monitor on Sparkfun. It is easy to setup and scans the twitterverse for any string you define.
I frankensteined the two parts together into a bit of code that would combine both functionalities.
Here is a photo of the setup so far
Future Enhancements:
*Weatherize the majority of setup in a sealable plastic housing (I hope to do this tonight)
*Customize the posted image to include the person who initialized the photo using that hashtag
*Beef up the resolution
Send any other suggestions or questions my way :)
> This will update the contents of the entire display. If your display mode is using the flags pygame.HWSURFACE and pygame.DOUBLEBUF, this will wait for a vertical retrace and swap the surfaces. If you are using a different type of display mode, it will simply update the entire contents of the surface.
>When using an pygame.OPENGL display mode this will perform a gl buffer swap.
https://www.pygame.org/docs/ref/display.html#pygame.display.flip
I recommend checking out this page. You'll need to make a font, then render a label. It's pretty straightforward. After that, you can blit the label into the area of the rect, and once you've done that, you can do collision with your mouse. All in all something like this should be good.
​
main_font = pygame.font.SysFont('arial', 50) # Creating our font object
button_rect = pygame.Rect(300, 300, 100, 50) # Creating our rect at a position of x: 300, y: 300, and with a width/height of 100, 500.
button_label = main_font.render('Press Me', True, (255, 255, 255)) # Creating our button label
# Main game loop here running = True while running: screen.blit(button_label, (300, 300)) # Blit the button label to the same position as the button rect # Rest of your code here
So whats going on here is pretty simple. We've created our objects and have blitted the button label to the same location as the rect, because rects have a colliderect method that you can use to determine if any collision is happening, and then on the mouseclick, if collision is occuring, then that button is "clicked" hope this helps, ask anything else you might be confused about
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
Hi,
I'm not sure which part of Ebiten approach was attractive to you, but I think these are similar libraries that might have similar concepts to some extent:
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
I don't know anything about PyGame, but since it says something is wrong with the arguments, I would look at PyGame documentation: pygame.Rect
From this page, it says that the function pygame.Rect
is either in the form Rect(left, top, width, height)
or Rect((left, top), (width, height))
Neither of these include a keyword argument center
, so you will just have to bear with using the left and top instead of center.
However, pygame.Rect
provides the center
property that allows you to change the center of the rect, so you can set the left and top values to random values and then set the center.
self.top_rect = pygame.Rect((0, 0), (but_width, but_height)) self.top_rect.center = (width/2, height/2)
You can create games in Python. Checkout Pygame.
If you want to create games, there is a high chance you will use C++.
Learning C is not that hard, I had to learn it in high school. And learning C will help you learn C++.
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
Well, if you enjoy game development you might look into Pygame. You can create some pretty spiffy games with it, all using Python. You won't code the next Doom in it, but making a 2D platformer, board game, or graphical adventure is certainly possible. Here is a decent tutorial that builds up from basic Python to OOP to Pygame.
If you are more into "functional" applications, maybe try to learn the ins and outs of a GUI library like Tkinter, PyQt or PySimpleGUI and build yourself a simple utility, like a lightweight calculator, text editor, icon creator, whatever. Anything in this vein would be a good learning experience, and the projects are not so complex that you will give up after months of development.
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.
MIDI is probably a good way to do this.
Check out my stack overflow answer that has a basic complete example using the midiutil
library. This will help you create midi files.
You can also check out PyGame's MIDI functionality, which is used for adding funky music to games written with PyGame, though, it could be used standalone, I suppose. This would be more useful for things like realtime input and playback.
Depending on how well you think they will manage actually programming something. You might look at some simple Scratch or Alice ideas. Think these offer some pretty simple/practical ways to achieve results as a complete novice. Python's PyGame might be another consideration for your group.
Audience depending of course... this is what comes to mind for something almost anybody could do and feel good within just a few hours. Plus, they can always download/explore on their own afterwards! Which is even better :D
You need to use a WAV or OGG file (MP3 might work, but support is spotty in Pygame) with the mixer module. In its basic form, it looks something like this.
sfx = pygame.mixer.Sound('sound.wav') sfx.play()
Sounds super cool! I followed the instructions in the readme, but got the following error when running the python script
On ubuntu 20, i3-gaps 4.18.2 (2020-07-26), I installed python3-pygame. btw isn't the compile step meant to take the .c and .so and create a prtscn
file? that's not happening when i run the gcc command (but no errors there)
$ ~/code/misc_projects/i3expo-ng/i3expod.py
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "/home/t4/code/misc_projects/i3expo-ng/i3expod.py", line 787, in <module>
read_config()
File "/home/t4/code/misc_projects/i3expo-ng/i3expod.py", line 164, in read_config
if not isset(option):
File "/home/t4/code/misc_projects/i3expo-ng/i3expod.py", line 175, in isset
if defaults[option][0](*option) == "None":
File "/home/t4/code/misc_projects/i3expo-ng/i3expod.py", line 103, in get_color
raw = config.get(section, option)
File "/usr/lib/python3.8/configparser.py", line 781, in get
d = self._unify_values(section, vars)
File "/usr/lib/python3.8/configparser.py", line 1149, in _unify_values
raise NoSectionError(section) from None
configparser.NoSectionError: No section: 'UI'
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
Do both: https://www.pygame.org/wiki/tutorials
You can can also learn godot too, although learning the engine and its practices may hinder progress in python (and vice versa). They’re obviously not mutually exclusive yet I’d favour keeping things simple.
pyGame has Clock.tick() where you can pass in the framerate. This will control how often the frame is updated. I've had good luck with setting it at 30 on a program I wrote years ago to run on an rPi 1.
https://www.pygame.org/docs/ref/time.html
According to the docs there are more accurate ways to do this but I don't think a few milliseconds here or there are going to matter on a Pong game.
Ohhh yeah thats a (fairly) common "bug"
To fix it you will need to pre_init the pygame.mixer:
pygame.mixer.pre_init(frequency=44100, size=-16, channels=2, buffer=512) pygame.mixer.init() pygame.init()
Lowering the buffer should lower the delay. Buffer should be the power of 2 (and if not it will round it to the closest).
Stackoverflow issue about this.
If that does not work then this answer might.
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?
Why not try making a game with PyGame? You can keep learning how to use python at the same time as making a cool game. You won't make Call of Duty right away but you can definitely make something cool.
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
Check out these pages: https://kidscancode.org/blog/2016/08/pygame_1-2_working-with-sprites/ https://www.pygame.org/docs/ref/image.html https://www.pygame.org/docs/ref/sprite.html
Getting the sprite files in to your working directory is a good start. Check out the definitions for the image and sprite classes to understand loading file to in to a usable object.
You want pygame.time.Clock()
and it's .tick()
method.
https://www.pygame.org/docs/ref/time.html#pygame.time.Clock.tick
Let's you pass in a target frame rate and will then use delay, if necessary, to slow down your loop to the target.
There are various strategies for where to call .tick()
in your loop depending on whether you want slightly more responsiveness to actions or a smoother frame rate.
I'm a lot older then you and love to learn new things. Learning one of those in a year is doable, as long as you can commit the time to it. I know with a family and a full-time job, finding time to learn is going to be difficult. Try to find an hour or two every day to spend some time on learning programming.
If you want to write games, try PyGame, a Python library for writing games. Python is easier to get started with and PyGame will give you goals to achieve.
Hey. You might find pygame useful if you'd like to use a graphics library. It's also possible to use only the standard library and Object oriented programming to create a terminal based game.
I found links in your comment that were not hyperlinked:
I did the honors for you.
^delete ^| ^information ^| ^<3
In that case, you don't want to pause the whole program. You just want to do a bit of work each frame. So the "delay" method isn't what you need. Instead, I recommend a new "update" method and two new attributes for the maze class.
One attribute would store the current status of the repaint. For instance, it could be a True/False boolean. The other attribute would store whether the repaint is complete or not. With these two attributes, the maze won't try to start over in the middle of the repaint progress, but will clearly track the start and end of the repaint.
I'll leave it to you to figure out the algorithm in the update method of the maze class for tracking which rect in the maze needs to be repainted each "frame". You'll probably do a better job at making an efficient algorithm than me.
The repaint input will do something like check the repaint boolean and if it isn't currently running, set it. Leave it up to the "update" method in the maze object to determine the status of the repaint process, repaint the next rect, and reset the booleans when it reaches and repaints the last rect in the maze. Just call that update method every frame, allowing it to figure out just exactly how to update the maze.
You could track time with a timer, and use that to update each rect each second, instead of every single frame.
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]: ...
Couple things -
Your code isn't runnable by anyone trying to help you because it doesn't have the images it tries to import. The best way of sharing something like this would be a folder containing all the elements on a file sharing website, or a github repository.
My suggestion would be to make the Chase class inherit sprite like the bones, so that way you can call pygame.sprite.spritecollide to see which sprites in the bones sprite group collide with your dog sprite.
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.
I personally believe making games is the best way to learn. Start learning PyGame and making simple games and expand as you get more experienced.
A good scope of a simple game is: A circle appears on the screen and the player has to click it before it disappears. Every time they click they get a point.
Maybe is my lack of experience but it looks like there is always some kind of issue or additional step on MacOS. Check this example from pygame:
https://www.pygame.org/wiki/GettingStarted
Is nothing major but it gets annoying when it happens many times setting your toolchain.