Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pygame.surface
#10
Programming is a tool set. Can you go in your garage and make a couch. You probably could make a simple bookshelf. It takes time and practice. One simple rule. KISS (keep it simple stupid).

Here my example. This is how I would write it.
import pygame
from math import pi as PI
from random import choice

class Star:
    @classmethod
    def allowed_colors(cls):
        cls.colors = []
        for key in pygame.color.THECOLORS.keys():
            color = pygame.Color(key)
            if key.startswith('gray') or key.startswith('grey'):
                continue
            elif color[0] > 230 and color[1] > 230 and color[2] > 230:
                continue
            else:
                cls.colors.append(color)

    def __init__(self):
        self.color = choice(self.colors)
        self.x = choice(list(range(20, 500, 20)))
        self.y = 20
        self.fall_speed = choice(list(range(17, 37))) / 10
        self.create_image()

    def create_image(self):
        self.image = pygame.Surface((20,20))
        self.image = self.image.convert_alpha()
        self.image.fill((0,0,0,0))

        pygame.draw.polygon(self.image, self.color, [(0,19), (9,0), (19,19), (0,7), (19,7)])

    def draw(self, surface, delta):
        pos = int(self.x), int(self.y)
        surface.blit(self.image, pos)

        self.y += self.fall_speed * delta * 0.0625

class Game:
    def __init__(self):
        self.rect = pygame.Rect(0, 0, 500, 600)
        pygame.display.set_caption('Star Drop Example')
        self.surface = pygame.display.set_mode(self.rect.size)
        self.background = self.create_background(self.rect.size)
        self.clock = pygame.time.Clock()

        Star.allowed_colors()
        self.star = Star()
        self.star2 = Star()

    def create_background(self, size):
        surface = pygame.Surface(size)
        surface.fill(pygame.Color('white'))

        # Draw on the surface a line from (0,50) to (190,150)
        # 5 pixels wide.
        pygame.draw.line(surface, pygame.Color('green'), [0, 150], [500, 150], 5)
        pygame.draw.line(surface, pygame.Color('green'), [0, 260], [500, 260], 5)

        # Draw on the surface several lines from (0,160) to (100,160)
        # 5 pixels wide using a loop

        for y_offset in range(0, 100, 10):
            pygame.draw.line(surface, pygame.Color('purple'), [0, 160 + y_offset], [240 - y_offset, 160 + y_offset], 5)
            pygame.draw.line(surface, pygame.Color('purple'), [262 + y_offset, 160 + y_offset], [500, 160 + y_offset], 5)

        #xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
        # Draw crosses top
            for x_offset in range(95, 370, 10):
                pygame.draw.line(surface,pygame.Color('blue'),[x_offset,15],[x_offset-10,5],2)
                pygame.draw.line(surface,pygame.Color('red'),[x_offset,5],[x_offset-10,15],2)
        # Draw crosses bottom
            for x_offset in range(95, 370, 10):
                pygame.draw.line(surface,pygame.Color('blue'),[x_offset,135],[x_offset-10,125],2)
                pygame.draw.line(surface,pygame.Color('red'),[x_offset,125],[x_offset-10,135],2)
        # Draw crosses left
            for y_offset in range(24, 130, 10):
                pygame.draw.line(surface,pygame.Color('blue'),[95, y_offset],[85,y_offset-10],2)
                pygame.draw.line(surface,pygame.Color('red'),[85,y_offset],[95, y_offset-10],2)
        # Draw crosses right
            for y_offset in range(24, 130, 10):
                pygame.draw.line(surface,pygame.Color('blue'),[365, y_offset],[355,y_offset-10],2)
                pygame.draw.line(surface,pygame.Color('red'),[355,y_offset],[365, y_offset-10],2)
        #xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

        # Draw an ellipse, using a rectangle as the outside boundaries
        pygame.draw.rect(surface, pygame.Color('red'), [100, 20, 250, 100], 4)
        pygame.draw.ellipse(surface, pygame.Color('purple'), [100, 20, 250, 100], 3)

        # Draw an ellipse with nner ellipses
        pygame.draw.ellipse(surface, pygame.Color('blue'), [390, 295, 100, 150], 3)
        pygame.draw.ellipse(surface, pygame.Color('red'), [400, 305, 80, 130], 3)
        pygame.draw.ellipse(surface, pygame.Color('cyan'), [425, 330, 30, 80], 3)

        # Draw an arc as part of an ellipse.
        # Use radians to determine what angle to draw.
        pygame.draw.arc(surface, pygame.Color('purple'), [20, 295, 150, 100], 0, PI / 2, 2)
        pygame.draw.arc(surface, pygame.Color('green'), [20, 295, 150, 100], PI / 2, PI, 2)
        pygame.draw.arc(surface, pygame.Color('blue'), [20, 295, 150, 100], PI, 3 * PI / 2, 2)
        pygame.draw.arc(surface, pygame.Color('red'), [20, 295, 150, 100], 3 * PI / 2, 2 * PI, 2)

        # This draws triangles using the polygon command
        pygame.draw.polygon(surface, pygame.Color('black'), [[250, 150], [145, 255], [355, 255]], 2)
        pygame.draw.polygon(surface, pygame.Color('red'), [[250, 255], [197, 203], [303, 203]], 2)

        # Draw a star using polygon command
        pygame.draw.polygon(surface, pygame.Color('blue'), [[270, 270], [290, 340], [370, 340],[310, 380],[335, 455],[270, 405],[210, 455],[235, 380],[180, 340],[250, 340]], 4)

        # Draw a square with a cross
        pygame.draw.rect(surface, pygame.Color('red'), [20, 450, 100, 100], 4)
        pygame.draw.line(surface, pygame.Color('blue'), (20, 450), (120, 550), 4)
        pygame.draw.line(surface, pygame.Color('blue'), (120, 450), (20, 550), 4)

        # Draw a colored square with a circle
        pygame.draw.line(surface, pygame.Color('blue'), (350, 480), (450, 480), 4)
        pygame.draw.line(surface, pygame.Color('purple'), (450, 480), (450, 580), 4)
        pygame.draw.line(surface, pygame.Color('green'), (450, 580), (350, 580), 4)
        pygame.draw.line(surface, pygame.Color('red'), (350, 580), (350, 480), 4)
        pygame.draw.ellipse(surface, pygame.Color('orange'), [350, 480, 100, 100], 3)

        return surface

    def loop(self):
        self.running = True
        while self.running:
            delta = self.clock.tick(30)
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    self.running = False

            self.surface.blit(self.background, (0,0))

            if self.star.y < self.rect.bottom - 20:
                self.star.draw(self.surface, delta)
            else:
                # create a new star
                self.star = Star()

            if self.star2.y < self.rect.bottom - 20:
                self.star2.draw(self.surface, delta)
            else:
                # create a new star
                self.star2 = Star()

            pygame.display.flip()


if __name__ == '__main__':
    pygame.init()
    game = Game()
    game.loop()
    pygame.quit()
99 percent of computer problems exists between chair and keyboard.
Reply


Messages In This Thread
pygame.surface - by Graham - Nov-28-2018, 12:52 PM
RE: pygame.surface - by Windspar - Nov-28-2018, 01:41 PM
RE: pygame.surface - by Graham - Nov-28-2018, 05:39 PM
RE: pygame.surface - by Windspar - Nov-29-2018, 02:02 AM
RE: pygame.surface - by Graham - Nov-29-2018, 09:13 AM
RE: pygame.surface - by Windspar - Nov-29-2018, 02:33 PM
RE: pygame.surface - by Graham - Nov-29-2018, 03:20 PM
RE: pygame.surface - by Windspar - Nov-29-2018, 03:22 PM
RE: pygame.surface - by Graham - Nov-29-2018, 03:57 PM
RE: pygame.surface - by Windspar - Nov-29-2018, 04:21 PM
RE: pygame.surface - by nilamo - Nov-29-2018, 04:45 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGame] Surface and rectangle in pygame Fabrizio_fg 6 2,626 May-27-2023, 09:15 AM
Last Post: Fabrizio_fg
  [PyGame] Pygame is treating blob_group as a surface, when I need it to treat it as a Sprite. Swagford 1 1,405 Jan-24-2023, 09:58 PM
Last Post: metulburr
  Coloring a surface with transparency Sandor 4 2,445 Jan-02-2022, 08:11 AM
Last Post: Sandor
  Drawn line shift when that surface is copied to another in pygame Leo_Red 4 3,451 Feb-11-2021, 06:33 AM
Last Post: Leo_Red
  [PyGame] pygame.Surface.fill help Shemira 3 6,318 Nov-29-2019, 12:01 AM
Last Post: Shemira
  [PyGame] PLEASE HELP! TypeError: unsupported operand type(s) for +: 'pygame.Surface' and 'int' keyfive 1 5,399 Jun-19-2018, 01:20 PM
Last Post: volcano63

Forum Jump:

User Panel Messages

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