0

I have drawn a snowman. But I need to make 3 of them, and not by changing their coordinates 1 by 1. I thought maybe I define 1 value in ever location and then it may be able to move it towards x axis. Current:

i= 0
import pygame, sys 
pygame.init() # Käivitame Pygame'i
ekraan = pygame.display.set_mode([800, 800]) 
pygame.display.set_caption("Snowmen") 

ekraan.fill([0, 0, 250])
def snowman():
    pygame.draw.circle(ekraan, [255, 250, 250], [400, 200], 60, 0)
    pygame.draw.circle(ekraan, [255, 250, 250], [400, 300], 75, 0)
    pygame.draw.circle(ekraan, [255, 250, 250], [400, 450], 110, 0)
    pygame.draw.circle(ekraan, [0, 0, 0], [380, 175], 12, 0)
    pygame.draw.circle(ekraan, [0, 0, 0], [420, 175], 12, 0)
    pygame.draw.rect(ekraan, [78, 71, 71], [350, 110, 100, 50], 0)
    pygame.draw.rect(ekraan, [78, 71, 71], [330, 155, 140, 5], 0)
    pygame.draw.circle(ekraan, [0,0,0], [400, 440], 9, 0)
    pygame.draw.circle(ekraan, [0,0,0], [400, 480], 9, 0)
    pygame.draw.circle(ekraan, [0,0,0], [400, 400], 9, 0)
    pygame.draw.circle(ekraan, [0,0,0], [400, 360], 8, 0)
    pygame.draw.circle(ekraan, [0,0,0], [400, 320], 8, 0)
    pygame.draw.circle(ekraan, [0,0,0], [400, 280], 8, 0)
    pygame.draw.polygon(ekraan, [255,165,0], [[400, 205], [400, 195],[450, 200]], 0)
    pygame.draw.line(ekraan, [139, 69, 19], [450, 300], [505, 260], 8)
    pygame.draw.line(ekraan, [139, 69, 19], [500, 190], [500, 550], 3)
    pygame.draw.line(ekraan, [255, 250, 250], [498, 190], [480, 138], 2)
    pygame.draw.line(ekraan, [255, 250, 250], [498, 190], [485, 138], 2)
    pygame.draw.line(ekraan, [255, 250, 250], [498, 190], [490, 138], 2)
    pygame.draw.line(ekraan, [255, 250, 250], [500, 190], [495, 140], 2)
    pygame.draw.line(ekraan, [255, 250, 250], [500, 190], [500, 140], 2)
    pygame.draw.line(ekraan, [255, 250, 250], [500, 190], [505, 140], 2)
    pygame.draw.line(ekraan, [255, 250, 250], [502, 190], [510, 142], 2)
    pygame.draw.line(ekraan, [255, 250, 250], [502, 190], [515, 142], 2)
    pygame.draw.line(ekraan, [255, 250, 250], [502, 190], [520, 142], 2)

while i < 3:
    i+=1
    snowman(k*40)  

pygame.display.flip() # Näitame akent

running = True # Muutuja "running" väärtustamine
while running: #Tsükli käivitamine kui muutuja on tõene
    for i in pygame.event.get(): # Kuulame programmi tööd
        if i.type == pygame.QUIT: # Kui vajutame akna sulgemise nuppu
            running = False # Muudame muutuja väärtuse vääraks
pygame.quit() # Sulgeme programmi akna
1
  • 2
    Do you know how to pass a value to a function? You need to add a parameter to the function definition, for example def snowman(x):, then you'll have the k*40 value from the while loop (btw k is not defined anywhere) available inside the function and can add it to the x-positions. Commented Jan 18, 2018 at 11:45

1 Answer 1

1

You have to create function with argument - ie. offset - and use this value with all x parameters

def snowman(offset):
    pygame.draw.circle(ekraan, [255, 250, 250], [400+offset, 200], 60, 0)
    pygame.draw.circle(ekraan, [255, 250, 250], [400+offset, 300], 75, 0)
    pygame.draw.circle(ekraan, [255, 250, 250], [400+offset, 450], 110, 0)
    pygame.draw.circle(ekraan, [0, 0, 0], [380+offset, 175], 12, 0)
    pygame.draw.circle(ekraan, [0, 0, 0], [420+offset, 175], 12, 0)
    pygame.draw.rect(ekraan, [78, 71, 71], [350+offset, 110, 100, 50], 0)
    pygame.draw.rect(ekraan, [78, 71, 71], [330+offset, 155, 140, 5], 0)
    pygame.draw.circle(ekraan, [0,0,0], [400+offset, 440], 9, 0)
    pygame.draw.circle(ekraan, [0,0,0], [400+offset, 480], 9, 0)
    pygame.draw.circle(ekraan, [0,0,0], [400+offset, 400], 9, 0)
    pygame.draw.circle(ekraan, [0,0,0], [400+offset, 360], 8, 0)
    pygame.draw.circle(ekraan, [0,0,0], [400+offset, 320], 8, 0)
    pygame.draw.circle(ekraan, [0,0,0], [400+offset, 280], 8, 0)
    pygame.draw.polygon(ekraan, [255,165,0], [[400+offset, 205], [400+offset, 195],[450+offset, 200]], 0)
    pygame.draw.line(ekraan, [139, 69, 19], [450+offset, 300], [505+offset, 260], 8)
    pygame.draw.line(ekraan, [139, 69, 19], [500+offset, 190], [500+offset, 550], 3)
    pygame.draw.line(ekraan, [255, 250, 250], [498+offset, 190], [480+offset, 138], 2)
    pygame.draw.line(ekraan, [255, 250, 250], [498+offset, 190], [485+offset, 138], 2)
    pygame.draw.line(ekraan, [255, 250, 250], [498+offset, 190], [490+offset, 138], 2)
    pygame.draw.line(ekraan, [255, 250, 250], [500+offset, 190], [495+offset, 140], 2)
    pygame.draw.line(ekraan, [255, 250, 250], [500+offset, 190], [500+offset, 140], 2)
    pygame.draw.line(ekraan, [255, 250, 250], [500+offset, 190], [505+offset, 140], 2)
    pygame.draw.line(ekraan, [255, 250, 250], [502+offset, 190], [510+offset, 142], 2)
    pygame.draw.line(ekraan, [255, 250, 250], [502+offset, 190], [515+offset, 142], 2)
    pygame.draw.line(ekraan, [255, 250, 250], [502+offset, 190], [520+offset, 142], 2)

And then you can run it with different values

for k in range(-1, 2):
    snowman(k*250)  

Full working code

# all imports at the top !!!
import pygame
import sys 

# --- functions ---

def snowman(offset):
    pygame.draw.circle(ekraan, [255, 250, 250], [400+offset, 200], 60, 0)
    pygame.draw.circle(ekraan, [255, 250, 250], [400+offset, 300], 75, 0)
    pygame.draw.circle(ekraan, [255, 250, 250], [400+offset, 450], 110, 0)
    pygame.draw.circle(ekraan, [0, 0, 0], [380+offset, 175], 12, 0)
    pygame.draw.circle(ekraan, [0, 0, 0], [420+offset, 175], 12, 0)
    pygame.draw.rect(ekraan, [78, 71, 71], [350+offset, 110, 100, 50], 0)
    pygame.draw.rect(ekraan, [78, 71, 71], [330+offset, 155, 140, 5], 0)
    pygame.draw.circle(ekraan, [0,0,0], [400+offset, 440], 9, 0)
    pygame.draw.circle(ekraan, [0,0,0], [400+offset, 480], 9, 0)
    pygame.draw.circle(ekraan, [0,0,0], [400+offset, 400], 9, 0)
    pygame.draw.circle(ekraan, [0,0,0], [400+offset, 360], 8, 0)
    pygame.draw.circle(ekraan, [0,0,0], [400+offset, 320], 8, 0)
    pygame.draw.circle(ekraan, [0,0,0], [400+offset, 280], 8, 0)
    pygame.draw.polygon(ekraan, [255,165,0], [[400+offset, 205], [400+offset, 195],[450+offset, 200]], 0)
    pygame.draw.line(ekraan, [139, 69, 19], [450+offset, 300], [505+offset, 260], 8)
    pygame.draw.line(ekraan, [139, 69, 19], [500+offset, 190], [500+offset, 550], 3)
    pygame.draw.line(ekraan, [255, 250, 250], [498+offset, 190], [480+offset, 138], 2)
    pygame.draw.line(ekraan, [255, 250, 250], [498+offset, 190], [485+offset, 138], 2)
    pygame.draw.line(ekraan, [255, 250, 250], [498+offset, 190], [490+offset, 138], 2)
    pygame.draw.line(ekraan, [255, 250, 250], [500+offset, 190], [495+offset, 140], 2)
    pygame.draw.line(ekraan, [255, 250, 250], [500+offset, 190], [500+offset, 140], 2)
    pygame.draw.line(ekraan, [255, 250, 250], [500+offset, 190], [505+offset, 140], 2)
    pygame.draw.line(ekraan, [255, 250, 250], [502+offset, 190], [510+offset, 142], 2)
    pygame.draw.line(ekraan, [255, 250, 250], [502+offset, 190], [515+offset, 142], 2)
    pygame.draw.line(ekraan, [255, 250, 250], [502+offset, 190], [520+offset, 142], 2)

# --- main ---

# - init -

pygame.init()
ekraan = pygame.display.set_mode([800, 800]) 
pygame.display.set_caption("Snowmen") 

# - draw -

ekraan.fill([0, 0, 250])

for k in range(-1, 2):
    snowman(k*250)  

pygame.display.flip()

# - mainloop -

clock = pygame.time.Clock()
running = True
while running:

    for i in pygame.event.get():
        if i.type == pygame.QUIT:
            running = False

    clock.tick(5) # slow down to 5 FPS so it will use less CPU power.

# - end -

pygame.quit()

enter image description here


There is also other method.

You create Surface and draw on it and later you blit this surface in three different places.

# all import at the top !!!
import pygame
import sys 

# --- functions ---

def create_snowman():
    # create surface
    image = pygame.Surface((300, 600)).convert_alpha()

    # fill with transparent background [x,x,x,0]
    image.fill([0, 0, 0, 0])

    # draw on surface
    pygame.draw.circle(image, [255, 250, 250], [150, 200], 60, 0)
    pygame.draw.circle(image, [255, 250, 250], [150, 300], 75, 0)
    pygame.draw.circle(image, [255, 250, 250], [150, 450], 110, 0)
    pygame.draw.circle(image, [0, 0, 0], [130, 175], 12, 0)
    pygame.draw.circle(image, [0, 0, 0], [170, 175], 12, 0)
    pygame.draw.rect(image, [78, 71, 71], [100, 110, 100, 50], 0)
    pygame.draw.rect(image, [78, 71, 71], [100, 155, 140, 5], 0)
    pygame.draw.circle(image, [0,0,0], [150, 440], 9, 0)
    pygame.draw.circle(image, [0,0,0], [150, 480], 9, 0)
    pygame.draw.circle(image, [0,0,0], [150, 400], 9, 0)
    pygame.draw.circle(image, [0,0,0], [150, 360], 8, 0)
    pygame.draw.circle(image, [0,0,0], [150, 320], 8, 0)
    pygame.draw.circle(image, [0,0,0], [150, 280], 8, 0)
    pygame.draw.polygon(image, [255,165,0], [[150, 205], [150, 195],[200, 200]], 0)
    pygame.draw.line(image, [139, 69, 19], [200, 300], [255, 260], 8)
    pygame.draw.line(image, [139, 69, 19], [250, 190], [250, 550], 3)
    pygame.draw.line(image, [255, 250, 250], [248, 190], [230, 138], 2)
    pygame.draw.line(image, [255, 250, 250], [248, 190], [235, 138], 2)
    pygame.draw.line(image, [255, 250, 250], [248, 190], [240, 138], 2)
    pygame.draw.line(image, [255, 250, 250], [250, 190], [245, 140], 2)
    pygame.draw.line(image, [255, 250, 250], [250, 190], [250, 140], 2)
    pygame.draw.line(image, [255, 250, 250], [250, 190], [255, 140], 2)
    pygame.draw.line(image, [255, 250, 250], [252, 190], [260, 142], 2)
    pygame.draw.line(image, [255, 250, 250], [252, 190], [265, 142], 2)
    pygame.draw.line(image, [255, 250, 250], [252, 190], [270, 142], 2)

    return image

# --- main ---

# - init -

pygame.init()
ekraan = pygame.display.set_mode([800, 800]) 
pygame.display.set_caption("Snowmen") 

# - draw -

# get image and rect
snowman_image = create_snowman()
snowman_rect = snowman_image.get_rect()

ekraan.fill([0, 0, 250])

# blit image on screen
for k in range(0, 3):
    snowman_rect.x = k*250
    ekraan.blit(snowman_image, snowman_rect)

pygame.display.flip()

# - mainloop -

clock = pygame.time.Clock()
running = True
while running:

    for i in pygame.event.get():
        if i.type == pygame.QUIT:
            running = False

    clock.tick(5) # slow down to 5 FPS so it will use less CPU power.

# - end -

pygame.quit()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.