Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to link this 2 code?
#1
SCREEN_WIDTH = 1500
    SCREEN_HEIGHT = 750
    background = pygame.image.load(r'C:\Users\ga-sa\Downloads\honeycomb.png')
    background = pygame.transform.scale(BACKGROUND, (SCREEN_WIDTH, SCREEN_HEIGHT))

    img1 = pygame.image.load(r"C:\Users\ga-sa\Downloads\As.png")
    img2 = pygame.image.load(r"C:\Users\ga-sa\Downloads\AssetsXOR.png")
    img3 = pygame.image.load(r"C:\Users\ga-sa\Downloads\AssetsNOT.png")
    img4 = pygame.image.load(r"C:\Users\ga-sa\Downloads\AssetsAND.png")
    images = [img1, img2, img3, img4]
    current_image = -1
    img_rects = [images[i].get_rect(topleft=(20 + 40 * i, 20)) for i in range(len(images))]
    img_angles = [0 for _ in range(len(images))]

    LeftButton = 0
    while 1:
        for e in pygame.event.get():
            if e.type == QUIT:
                pygame.quit()
                exit(0)

            if e.type == pygame.MOUSEBUTTONDOWN:
                mouse_rect = pygame.Rect(e.pos, (1, 1))
                current_image = mouse_rect.collidelist(img_rects)

            if e.type == MOUSEMOTION:
                if e.buttons[LeftButton]:
                    rel = e.rel
                    if 0 <= current_image < len(images):
                        img_rects[current_image].x += rel[0]
                        img_rects[current_image].y += rel[1]

        keys = pygame.key.get_pressed()
        if keys[pygame.K_RIGHT]:
            img_angles[current_image] -= 1
        if keys[pygame.K_LEFT]:
            img_angles[current_image] += 1

        screen.blit(background, (0,0))

        for i in range(len(images)):
            rotated_image = pygame.transform.rotate(images[i], img_angles[i])
            rotated_rect = rotated_image.get_rect(center=img_rects[i].center)
            screen.blit(rotated_image, rotated_rect)

        pygame.display.flip()

the second code that i like to put in the first:
   def main()
pygame.init()
pygame.display.set_caption("Mouse Draw")
surface = pygame.display.set_mode((800, 600))
clock = pygame.time.Clock()
rect = surface.get_rect()
fps = 60

line_surface = pygame.Surface(rect.size, pygame.SRCALPHA)
line_surface.fill((0, 0, 0, 0))

mouse_position = None
display_line = None
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.MOUSEMOTION:
            if mouse_position:
                display_line = mouse_position, event.pos
        elif event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:
                if mouse_position:
                    pygame.draw.line(line_surface, pygame.Color("red"), mouse_position, event.pos)
                    mouse_position = None
                    display_line = None
                else:
                    mouse_position = event.pos

    surface.fill(pygame.Color('black'))
    surface.blit(line_surface, (0, 0))

    if display_line:
        pygame.draw.line(surface, pygame.Color('lawngreen'), *display_line)

    pygame.display.update()
    clock.tick(fps)
main ()
Reply
#2
Please elaborate. Don't quite know what's being asked.
Reply


Forum Jump:

User Panel Messages

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