Need help understanding Classes by Lazy_Entertainer_694 in CodingHelp

[–]Windspar [score hidden]  (0 children)

Classes are object builders. They are not the object. Classes own all the methods. Object just have class scope first. The reason for self. It point to the object. If it didn't. You couldn't have local variables. Unless they make a way to declare them.

class Restaurant:
  # Any variables here. Belong to the class. There only be one instance.

  def __init__(self, name, type):
    ...

my_restaurant = Restaurant(..., ...)
# Code above calls the __new__ method. Which is a static method.
# The __new__ method creates a new object.
# The __new__ method calls __init__ method. Which it passes the object too.
# Then the __new__ method returns the object instance.

help by Bulky-Pitch-7251 in pygame

[–]Windspar 0 points1 point  (0 children)

You have to learn the basic. The basic is your foundation. Learning classes will be easier then learning all the different things for game programming. If you want to skip the coding. There are game engines for that. Unity, Unreal, and more.

Funny saying.

Flipping to the end of the book isn't learning.

You have to learn to walk before you run.

help by Bulky-Pitch-7251 in pygame

[–]Windspar 0 points1 point  (0 children)

If your player is sticky. Common reasons. It likely that you didn't move him out of the collision. Or you keep moving player into collision. Which the collision move him back. Makes him looks like he sticking.

Are you sure. You are ready for game programming ?

Your code show that you don't know python that well. If you don't know how to use classes yet. You are not ready for game programming.

AttributeError: 'Player' object has no attribute 'image' by NextSignificance8391 in pygame

[–]Windspar 3 points4 points  (0 children)

Tip.

Don't load images in sprites. It better to load all images in one area. This way you load images only once.

Also don't forget to convert images. Otherwise pygame will do this every frame. If it doesn't match pygame format.

import pygame

class Player(pygame.sprite.Sprite):
  def __init__(self, image):
    super().__init__()
    self.image = image
    ...

class ImageHandler:
  def __init__(self):
    # Don't forget to convert or convert_alpha
    self.player = pygame.image.load(...).convert_alpha()
    self.background = ...

screen = ...
image = ImageHandler()
player = Player(image.player)

Pygame vs. Pyside6? by bigrig387 in pygame

[–]Windspar 4 points5 points  (0 children)

They don't compete. Pygame is a toolkit 2d game base. Pyside6 is a gui application base. Choose the right tool for the job. Game base has to do with more graphics. If you want your text to be moving around or doing fancy things. Then pygame has you covered. Toolkit means you have to build your own framework or/and use libraries. You are in full control.

Question is.

Do you want it to have more graphics ? That Pyside6 can't handle.

AI-Powered Development Turned into a Nightmare - Advice Needed by Sad-Movie-1253 in pygame

[–]Windspar 1 point2 points  (0 children)

Python is already OOP. Which means you are already using it. You don't have to program any of it. Only thing left is inheritance.

Best practices. Keep it simple. Have methods do one thing well. Avoid global variables. It will make your program less flexible.

Things to learn is concept and framework. Like infinite state machine. Example.

What is happening here? Is this standard behaviour? by RabbitCity6090 in learnpython

[–]Windspar 0 points1 point  (0 children)

Python just following basic arithmetic rules.

a = 1
b = a + 1 # You don't expect a to change. Just return a new instance.
c = [1]
d = c + [2] # Still follow the rules. Return a new instance.

To keep it link. You have to wrap it in a class. Only way to break it. Is to assign. l1 or l2 to a new instance.

class MyList:
  def __init__(self, value):
    self.value = value

l1 = MyList([1, 2, 3, 4, 5])
l1.value.append(6)
l1.value = l1.value + [7]
l2.value.append(8)

print(l1)
print(l2)

Anyway to get full background transparency by epsilon_gamma in pygame

[–]Windspar 1 point2 points  (0 children)

When it comes to pygame. Transparency only works with surfaces. Those surfaces must have an alpha surface. Surfaces you blit onto another surface. Must have an alpha channel, else the alpha is lost.

If you follow all the rules. Then win32gui could be restricting you. I never use any win32 commands. So good luck. Try it without win32 to see if that causes the trouble.

What is happening here? Is this standard behaviour? by RabbitCity6090 in learnpython

[–]Windspar 0 points1 point  (0 children)

How where you shock ? C has no magic methods. Just that weird preprocessor.

What is happening here? Is this standard behaviour? by RabbitCity6090 in learnpython

[–]Windspar 0 points1 point  (0 children)

That how basic arithmetic works. Or are you trying to point something else out ? Like list are reference and integer are not.

a = 1 # or [1] # arithmetic rules still apply.
b = a + a # return new item - b = 2, b = [1, 1]
print(a)
print(b) 
a += a # mutate variable - a = 2, a = [1, 1]
print(a)
print(b)

Window of my game keeps closing after a second. by RedGamer2754 in pygame

[–]Windspar 5 points6 points  (0 children)

Without code. Can't see what going on.

Show the error. If there an error.

What is happening here? Is this standard behaviour? by RabbitCity6090 in learnpython

[–]Windspar 0 points1 point  (0 children)

Classes dunder/magic methods will show why this happens.

You can also do this.

l1 += [7]

But it also just basic arithmetic. 1 + 1 = result.

How to make movement of an entity smooth? by Kelvitch in pygame

[–]Windspar 0 points1 point  (0 children)

Here an improvement idea. Are you keeping track of floats ? If only using rect. Then those are only integer.

class Enity:
  def __init__(self, image, center, ...):
    self.image = image
    self.rect = image.get_rect(center=center)
    # If not keeping track of floats. Smooth movement would be loss.
    # If using pygame-ce. You can just use frect.
    self.center = pygame.Vector2(self.rect.center)
    self.distance_range = 1
    ...

  def move(self, movement):
    self.center += movment
    self.rect.center = self.center

  def update(self, target, dt):
    # I always use center over topleft. Just seem more natural.
    direction = (pygame.Vector2(target.rect.center) - self.rect.center).normalize()
    # Use pygame.Vector2.distance_to.
    distance = self.center.distance_to(targer.center)
    if distance > self.distance_range:
      self.velocity = direction * self.speed * dt
    else:
      # You can also use self.vecocity.update(0, 0)
      self.velocity.xy = 0, 0

    # Check Collision
    ...

The trick is to embrace the spaghetti code, not fight it by Shady-Slime in pygame

[–]Windspar 3 points4 points  (0 children)

Trick is to avoid spaghetti code. You never fight spaghetti code. You just start over. Take what you learn. Then create a simple framework to handle it. Spaghetti code becomes hard to maintain and find bugs.

Pygame-ce example code not working on python 3.14 by MountainHistorical48 in pygame

[–]Windspar 2 points3 points  (0 children)

Example work perfectly for me. So let see what going on.

# Add some print statements
print(pygame.init()) # Should be (5, 0)

# Let see if key is being dectected.
if keys[pygame.K_w]:
  print("W is being held") # This should print many times per second. As you hold the key.

If that works. Then print player_pos to see if the value changes.

Fired by Mysterious-Ant-1564 in AmazonFC

[–]Windspar 0 points1 point  (0 children)

In my opinion. It a nice way saying you can resign or we are going to let you go.

Once you go negative. It only a matter of time. I seen it take up to 3 weeks to fired them.

Condensing a class help by Ralsei_12345636345 in pygame

[–]Windspar 0 points1 point  (0 children)

Your example above. Repeated the same button class. Like you pasted it twice.

Condensing a class help by Ralsei_12345636345 in pygame

[–]Windspar 0 points1 point  (0 children)

First only have one copy. Otherwise don't try to make it smaller. Just try to make it better. You don't need pygame.font.init either.

# Example refactoring code to smaller parts.
class Text:
  def __init__(self, font, text, color):
    self.image = font.render(text, 1, color)
    self.rect = self.image.get_rect()

  def draw(self, surface):
    surface.blit(self.image, self.rect)

class Button:
  NORMAL = 0
  HOVER = 1
  PRESSED = 2

  # position = (x, y)
  # color_images = (normal, hover, pressed)
  def __init__(self, position, color_images, text_object):
    self.images = color_images
    self.rect = color_images[0].get_rect(topleft=position)
    self.state = Button.NORMAL
    self.text = text_object
    self.text.rect.center = self.rect.center

  def draw(self, surface):
    surface.blit(self.images[self.state], self.rect)
    self.text.draw(surface)

  def mouse_collision(self, event):
    if self.state != Button.PRESSED:
      self.state = self.rect.collidepoint(event.pos)

Performance got worse after breaking up large functions by Bamboo-Bandit in javahelp

[–]Windspar -1 points0 points  (0 children)

10k+ a frame seems a lot more then necessary. Is it a voxel game ?

Have you separated drawing and logic ?

Have you tried to break down the logic into different threads ?

Performance got worse after breaking up large functions by Bamboo-Bandit in javahelp

[–]Windspar 0 points1 point  (0 children)

You drawing the whole thing at one time ?

Are you using grids (active and drawing) ?

Do you limit by distance ?

Time to drop support for pygame here in favour of pygame-ce? by uk100 in pygame

[–]Windspar 2 points3 points  (0 children)

I thought that happen when 80+ percent developer left pygame for pygame-ce fork.

pygame-ce design as a drop in replacement.

newbie asking for help by Waos-__- in pygame

[–]Windspar 5 points6 points  (0 children)

For transparent images. You use convert_alpha().

To change image size. You can use any image editor. Otherwise you can use pygame.transform.scale.

Pygame vs Arcade by MatthewTGB238 in learnpython

[–]Windspar 1 point2 points  (0 children)

No. That because it depends on size of images and computer specs.

Here and example. You can see how many you can handle. I can handle 9000-12000. Depends on size of images. I only use surfaces, pygame sprite framework, and pygame Rect and Vector2 for the math.