Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add game_popit.py in examples #2798

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

snowfruit
Copy link
Contributor

PopIt

About
A beginner pygame example about clicking on balls.

Features

  • No external resources
    Makes it easy to share and copy/paste.
  • Lots of comments
    The beginner can read every step in detail.
  • Game in a class
    The main game is contained inside a class.
  • Frame independent movement
    Balls moves the same speed at different frame rates.
  • Hit detection
    Click on balls to "destroy" them.
  • Random colors
    The color of the balls are set with random RGB-values.
  • Random movement
    Balls moves at random speed towards a random target.
  • Random placement
    Place ball at random position inside a specific area.
  • Object pooling
    Balls are not repeatedly created and destroyed but reset.

Add file beginner game_popit.py in examples.
@snowfruit snowfruit requested a review from a team as a code owner April 12, 2024 15:07


class Game:
"""The game-class. Object's and logic is in here."""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Grammar: "Objects and logic are in here"

Comment on lines +156 to +157
self.size = (640, 480)
"""Display size."""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why store size if you just use it in one place, in pygame.display.set_mode.

Also these doc strings seem like overly much since the attributes aren't supposed to be used outside of the class. Why not use normal comments?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To help the beginner understand what they put into the "pygame.display.set_mode".

I'm not used to Visual Studio Code but it seems to be the way to make the variableinfo show up when hovering over it?

self.caption = "PopIt"
"""Name of the game."""
self.frame_rate = 30
"""Desiered frame rate for the game."""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: desired

Also I'd put it at least 60, makes the movement smoother.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.

pygame.draw.circle(self.screen, self.color, self.position, self.radius)


class Game:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the point of a class if you're just going to call it like a function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To show the beginner that you can make your game or app as a class. In my mind it makes it easier to use the example in teaching. The teacher can ask the student to use the class as "game engine".


return False

def blit(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typically pygame sprite systems call this draw, imo.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I don't understand. You want every ball to be a sprite/circle and not just color/radius/etc.?

Comment on lines +136 to +139
if self.position.distance_to(position) < self.radius:
return True

return False
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return self.position.distance_to(position) < self.radius ?

Comment on lines +24 to +25
* Object pooling
Balls are not repeatedly created and destroyed but reset.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think is a good feature for an example. Most games need to deal with dynamic amounts of objects.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair. I do believe shmups and "Vampire Survivors"-clones are a thing suited for pygame. On old pi's and laptops things get slow fast and can suck the fun out of it for a beginner.

@Starbuck5
Copy link
Member

A bunch of the examples are games, they don't start with game_. Therefore I don't think this one should either.

@Starbuck5
Copy link
Member

What value does this example provide when we have other "basic game" examples like chimp and aliens?

@snowfruit
Copy link
Contributor Author

A bunch of the examples are games, they don't start with game_. Therefore I don't think this one should either.

Fair. But finding things in the examples are not easy as a young beginner.

"Is arraydemo.py a game? It says demo and on steam a demo is a game. Oh a demo can be a kind of example."

While making "game_popit" I was asked if I knew where the monkey punching game were. The person did not know that chimp is "monkey".

@snowfruit
Copy link
Contributor Author

What value does this example provide when we have other "basic game" examples like chimp and aliens?

I believe that many of the examples that are here now requires you to know more python than a person just discovering pygame might know. If the code is easy to copy/paste and can be built upon with ease maybe more people will find pygame fun at first try. I tried to write a recognizably, beginner and entry level game that you could copy/paste.

I think chimp is a perfect example for pygame. It is clean, works great. But questions I got when showing it was: "What is os.path.split and os.path.join? Why do I need image = image.convert(). I just loaded my image."

@snowfruit
Copy link
Contributor Author

Thank you for all the feedback!

@Starbuck5
Copy link
Member

I think chimp is a perfect example for pygame. It is clean, works great. But questions I got when showing it was: "What is os.path.split and os.path.join? Why do I need image = image.convert(). I just loaded my image."

Those are good questions for a beginning user to be asking, because those are important things to know for writing pygame-ce games.

@Starbuck5 Starbuck5 marked this pull request as draft April 29, 2024 05:57
@snowfruit
Copy link
Contributor Author

I think chimp is a perfect example for pygame. It is clean, works great. But questions I got when showing it was: "What is os.path.split and os.path.join? Why do I need image = image.convert(). I just loaded my image."

Those are good questions for a beginning user to be asking, because those are important things to know for writing pygame-ce games.

I agree. But the questions are being asked the same way a teenager ask why they should make their bed or brush their teeth.

I'm sharing the experience I had using pygame to inspire a young person to learn python. The person was curious but not motivated. Pygame is an excellent way to learn basic python and I tried to sneak in something extra. Such as classes and pooling to see how far I could push it.

Not trying to justify or hype my code, just wanted to share the experience.

Appreciate all the feedback and thanks for taking the time to look at the code. If it don't fit the project, don't hesitate to close/cancel the merge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants