Python Forum
drawing, moving, and collision problems (pygame)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
drawing, moving, and collision problems (pygame)
#5
Don't make new post. For same code that been alter a little.

You going the wrong direction here. Let remove python time and threading modules. That another learning curve.

You need to rewrite code. Do not copy and paste code. Type it out.
Examples of clean code.
Basic pygame setup.
class Game:
    info = None

    def __init__(self, caption, width, height):
        # Basic pygame setup
        pygame.display.set_caption(caption)
        self.surface = pygame.display.set_mode((width, height))
        self.rect = self.surface.get_rect()
        self.clock = pygame.time.Clock()
        self.fps = 30

        # Class global variable
        Game.info = self

    def mainloop(self):
        self.running = True
        while self.running:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    self.running = False
                elif event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE:
                        self.running = False

            self.surface.fill(pygame.Color('Navy'))
            # Draw code here

            pygame.display.flip()
            self.clock.tick(self.fps)

def main():
    pygame.init()
    game = Game("Example", 800, 600)
    game.mainloop()
    pygame.quit()

main()
Button draft. Turning button into an object.
class Button:
    def __init__(self, caption, font, rect, color, text_color):
        self.image = font.render(caption, 1, text_color)
        self.rect = rect
        self.text_rect = self.image.get_rect()
        self.text_rect.center = rect.center
        self.color = color

    def draw(self, surface):
        # button draw code here

    def onbuttonup(self, event):
        # click event
Challange. Let see you add on mouse over method and finish button class.
99 percent of computer problems exists between chair and keyboard.
Reply


Messages In This Thread
RE: Drawing player and enemy (pygame) - by Windspar - Apr-14-2019, 10:25 AM
RE: Drawing enemy but not player (pygame) - by Windspar - Apr-14-2019, 10:47 PM
pygame problems - by SheeppOSU - Apr-18-2019, 11:27 PM
pygame problems - by SheeppOSU - Apr-19-2019, 11:59 PM
changing position errors - by SheeppOSU - Apr-22-2019, 03:09 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question [PyGame] Problem with collision of player and enemy Nekotrooper 1 971 Dec-08-2023, 03:29 PM
Last Post: deanhystad
  pygame installation problems Gheryk 5 8,930 Nov-29-2023, 08:49 PM
Last Post: E_Mohamed
  can't get collision detection to work in platform game chairmanme0wme0w 10 4,317 Aug-19-2022, 03:51 PM
Last Post: deanhystad
  [PyGame] Problems with jump code in pygame Joningstone 4 5,587 Aug-23-2021, 08:23 PM
Last Post: deanhystad
  [PyGame] drawing images onto pygame window djwilson0495 1 3,640 Feb-22-2021, 05:39 PM
Last Post: nilamo
  [PyGame] Collision in not happening onizuka 3 3,587 Sep-07-2020, 11:30 AM
Last Post: metulburr
  [PyGame] No collision detection onizuka 6 3,895 Aug-18-2020, 01:29 PM
Last Post: onizuka
  [PyGame] pygame, help with making a function to detect collision between player and enemy. Kris1996 3 3,496 Mar-07-2020, 12:32 PM
Last Post: Kris1996
  Problem with collision detection... michael1789 4 3,493 Nov-12-2019, 07:49 PM
Last Post: michael1789
  Pygame sprite not moving michael1789 1 2,966 Nov-10-2019, 03:54 AM
Last Post: michael1789

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020