Python Forum
pygame, sprites, and rects
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pygame, sprites, and rects
#1
Taking the following snippet, is it possible to create a pygame.Rect() and add it to a sprite group?
Been looking for awhile and not really found anything. Maybe just tired. lol.

In the following example, I can get func1 to display but, not sure how to add func2 to a sprites group.
Any help would be great.

import pygame
import sys

# Initiate pygame
pygame.init()

# Create some settings
size = (1280, 720)
screen = pygame.display.set_mode(size)
pygame.display.set_caption('Shmup')
clock = pygame.time.Clock()

hud = pygame.sprite.Group()

def func1():
    arect = pygame.Rect(30,10,125,25)
    pygame.draw.rect(screen, 'green', arect)
    
def func2():
    arect = pygame.Rect(30, 35, 125, 25)
    hud.add(arect)

while True:
    event = pygame.event.poll()
    if event.type == pygame.QUIT:
        sys.exit()

    hud.update()
    hud.draw(screen)

    func1()

    pygame.display.update()
    clock.tick(60)

pygame.quit()
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply


Messages In This Thread
pygame, sprites, and rects - by menator01 - Oct-25-2023, 04:25 AM
RE: pygame, sprites, and rects - by deanhystad - Oct-25-2023, 01:34 PM
RE: pygame, sprites, and rects - by Windspar - Oct-25-2023, 08:09 PM
RE: pygame, sprites, and rects - by menator01 - Oct-25-2023, 09:59 PM
RE: pygame, sprites, and rects - by deanhystad - Oct-26-2023, 10:00 PM
RE: pygame, sprites, and rects - by menator01 - Oct-27-2023, 01:40 AM
RE: pygame, sprites, and rects - by menator01 - Oct-28-2023, 03:59 PM
RE: pygame, sprites, and rects - by Windspar - Oct-31-2023, 06:53 AM
RE: pygame, sprites, and rects - by menator01 - Oct-31-2023, 10:45 AM
RE: pygame, sprites, and rects - by Windspar - Nov-01-2023, 03:23 AM
RE: pygame, sprites, and rects - by deanhystad - Nov-01-2023, 09:36 PM
RE: pygame, sprites, and rects - by Windspar - Nov-02-2023, 12:52 AM
RE: pygame, sprites, and rects - by Benixon - Dec-07-2023, 02:37 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGame] Sprites just randomly appear and dissapear in my pygame.sprite.GoupeSingle trueShadoWrr 2 2,048 Feb-13-2023, 09:34 AM
Last Post: Vadanane
  [PyGame] I found a way to generate sprites without .blit. Is it effecient? xBlackHeartx 19 8,654 Dec-07-2019, 01:06 PM
Last Post: metulburr
  [pygame] transparent rects SheeppOSU 2 2,338 Jun-10-2019, 03:41 PM
Last Post: SheeppOSU
  [PyGame] Having 4 players(Sprites) all being able to jump ElijahCastle 5 4,080 May-07-2019, 05:04 PM
Last Post: SheeppOSU
  Sprites and Actor error ajlconsulting 6 9,464 Jan-30-2019, 12:50 AM
Last Post: metulburr
  draw not showing all sprites ethanstrominger 0 2,642 Jan-25-2019, 10:10 PM
Last Post: ethanstrominger
  [PyGame] move randomly sprites reutB 4 8,321 Mar-29-2017, 01:12 PM
Last Post: metulburr
  How can I get rid of the line following the sprites? Houston11 1 3,784 Jan-06-2017, 10:14 PM
Last Post: Mekire

Forum Jump:

User Panel Messages

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