I wanna to make a program that generate a line with random shape of blocks. I did well so far. But I dont want to def every single block. Is there a way to do a def function in for to every time when for happened def something else? If not how can i def 10 objects with not just copy and pasting 10 times and changing names. I wanna to my code be more compact, Thanks for reading! I also added a project if I didnt explain well what I am trying to do
import random
import pygame
pygame.init()
black = 0, 0, 0
white = 255, 255, 255
red = 255, 0, 0
green = 0, 255, 0
blue = 0, 0, 255
screen = pygame.display.set_mode((800, 600))
BlockImg = pygame.image.load('cobblestone.png')
blockX = random.randint(0, 800)
blockY = random.randint(0, 600)
position = random.randint(1, 6)
if position == 1:
block2X = blockX - 16
block2Y = blockY
if position == 2:
block2X = blockX + 16
block2Y = blockY
if position == 3:
block2X = blockX
block2Y = blockY - 16
if position == 4:
block2X = blockX + 16
block2Y = blockY - 16
if position == 5:
block2X = blockX + 16
block2Y = blockY + 16
if position == 6:
block2X = blockX - 16
block2Y = blockY + 16
positionoftwo = random.randint(1, 6)
if positionoftwo == 1:
block3X = blockX - 16
block3Y = blockY
if positionoftwo == 2:
block3X = block2X + 16
block3Y = block2Y
if positionoftwo == 3:
block3X = block2X
block3Y = block2Y - 16
if positionoftwo == 4:
block3X = block2X + 16
block3Y = block2Y - 16
if positionoftwo == 5:
block3X = block2X + 16
block3Y = block2Y + 16
if positionoftwo == 6:
block3X = block2X - 16
block3Y = block2Y + 16
# can I automate that?
def one(x, y):
screen.blit(BlockImg, (x, y))
def two(x, y):
screen.blit(BlockImg, (x, y))
def three(x, y):
screen.blit(BlockImg, (x, y))
def four(x, y):
screen.blit(BlockImg, (x, y))
def five(x, y):
screen.blit(BlockImg, (x, y))
def six(x, y):
screen.blit(BlockImg, (x, y))
def seven(x, y):
screen.blit(BlockImg, (x, y))
running = True
while running:
screen.fill(black)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
one(blockX, blockY)
two(block2X, block2Y)
three(block3X, block3Y)
pygame.display.update()
Aucun commentaire:
Enregistrer un commentaire