Python Forum
Ho can I make it so the dirt goes up instad of diagonal
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Ho can I make it so the dirt goes up instad of diagonal
#1
I finished this code so in line 53-65 display the numbers of the code of images, but when I try to run it nothing goes as planned.
how can I fix it?
import pygame

pygame.init()

screen_width = 1001
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))

# screen name
pygame.display.set_caption("HANDSOME ADVENTURE")

# variables
tile_size = 50

# screen icon
icon = pygame.image.load('handsome icon.png')
pygame.display.set_icon(icon)

# load images
bg_img = pygame.image.load('bgp.png')


def draw_grid():
    for line in range(0, 100):
        pygame.draw.line(screen, (255, 255, 255), (0, line * tile_size), (screen_width, line * tile_size))
        pygame.draw.line(screen, (255, 255, 255), (line * tile_size, 0), (line * tile_size, screen_height))


class World():
    def __init__(self, data):
        self.tile_list = []

        dirtimg = pygame.image.load('handsomedirt.png')

        row_count = 0
        for row in data:
            col_count = 0
            for tile in row:
                if tile == 1:
                    img = pygame.transform.scale(dirtimg, (tile_size, tile_size))
                    img_rect = img.get_rect()
                    img_rect.x = col_count * tile_size
                    img_rect.y = col_count * tile_size
                    tile = (img, img_rect)
                    self.tile_list.append(tile)
                col_count += 1
            row_count += 1

    def draw(self):
        for tile in self.tile_list:
            screen.blit(tile[0], tile[1])

world_data = [

[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
]


world = World(world_data)

# gameloop
run = True
while run:

    screen.blit(bg_img, (0, 0))

    world.draw()

    draw_grid()

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

    pygame.display.update()

pygame.quit()
Reply
#2
"Nothing goes as planned" is not descriptive. What do you expect to happen? How does this differ from what actually happens
Reply
#3
There are three mistakes in the for loop in the World class.
The first is tha you change img_rect.y to col_count instead of row_count.
The second is using putting img_rect where you real need a tuple.
The third is that you use tile as the variable in the for loop and the reassign it in line 44.
Try this and see if it works for you.

class World():
    def __init__(self, data):
        self.tile_list = []
 
        dirtimg = pygame.image.load('handsomedirt.png')
        img = pygame.transform.scale(dirtimg, (tile_size, tile_size))
        img_rect = img.get_rect()

        row_count = 0
        for row in data:
            col_count = 0
            for tile in row:
                if tile == 1:
                    img_rect.x = col_count * tile_size
                    img_rect.y = row_count * tile_size
                    dirt_tile = (img, tuple (img_rect))
                    self.tile_list.append(dirt_tile)
                col_count += 1
            row_count += 1
 
    def draw(self):
        for tile in self.tile_list:
            screen.blit(tile[0], tile[1])
 
world_data = [
 
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
]
Also you are one row short in your world_data.
izmamonke likes this post
Reply
#4
(Jul-12-2021, 01:25 AM)BashBedlam Wrote: There are three mistakes in the for loop in the World class.
The first is tha you change img_rect.y to col_count instead of row_count.
The second is using putting img_rect where you real need a tuple.
The third is that you use tile as the variable in the for loop and the reassign it in line 44.
Try this and see if it works for you.

class World():
    def __init__(self, data):
        self.tile_list = []
 
        dirtimg = pygame.image.load('handsomedirt.png')
        img = pygame.transform.scale(dirtimg, (tile_size, tile_size))
        img_rect = img.get_rect()

        row_count = 0
        for row in data:
            col_count = 0
            for tile in row:
                if tile == 1:
                    img_rect.x = col_count * tile_size
                    img_rect.y = row_count * tile_size
                    dirt_tile = (img, tuple (img_rect))
                    self.tile_list.append(dirt_tile)
                col_count += 1
            row_count += 1
 
    def draw(self):
        for tile in self.tile_list:
            screen.blit(tile[0], tile[1])
 
world_data = [
 
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
]
Also you are one row short in your world_data.
Thank you so much!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  4D array with only elements on one side of the diagonal schniefen 0 1,682 Dec-24-2020, 11:32 AM
Last Post: schniefen
  diagonal matix kraljorah 1 1,869 Dec-20-2020, 05:11 AM
Last Post: ndc85430

Forum Jump:

User Panel Messages

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