getting objects to draw on the screen at a random location

getting objects to draw on the screen at a random location

Postby LavaCreeperKing » Tue Apr 26, 2016 9:12 pm

I have a question. So if I create an ObjectClass and use it to define an object, then I create three objects like this.
Code: Select all
class ObjectClass(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self,type,image,xcord)
        self.image = pygame.image.load("image")
        self.type = type
        self.rect = self.image.get_rect()
        self.rect.center = [xcord, 500]


pygame.init()
tree = ObjectClass('tree','tree.png')
rock = ObjectClass('rock','rock.png')
fence = ObjectClass('fence','fence.png')

How can I now get my program to get tree, rock, and fence and put them into a list, choose one, then choose an xcord and draw it on the screen?
LavaCreeperKing
 
Posts: 103
Joined: Wed Sep 02, 2015 11:31 pm

Re: getting objects to draw on the screen at a random locati

Postby micseydel » Tue Apr 26, 2016 9:19 pm

LavaCreeperKing wrote:How can I now get my program to get tree, rock, and fence and put them into a list

Code: Select all
options = [tree, rock, fence]


choose one

Code: Select all
from random import choice
option = choice(options)


then choose an xcord

Code: Select all
xcord = option.rect.center[0]


and draw it on the screen?

That, I'm not so sure about. A Pygame user would probably know.
Due to the reasons discussed here we will be moving to python-forum.io on October 1, 2016.

This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
User avatar
micseydel
 
Posts: 3000
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: getting objects to draw on the screen at a random locati

Postby metulburr » Tue Apr 26, 2016 10:01 pm

Here is a modified version of your code. I commented out the image loading as i do not have the images to load. You can remove the Surface and fill and load the images. As well as the associated color tuple for each.

I assume you mean by choose an xcord you mean random x value?

Drawing an object is just blitting the object to the screen at the position of the rect.

Every time this is ran you will get a random selected object from the list at a random x coordinate. The colors are only to differentiate between each object in the list. As is there is no point whatsoever to have your class inherit pygame.sprite.Sprite.

Code: Select all
import pygame
import random

pygame.init()
screen = pygame.display.set_mode((800,600))
screen_rect = screen.get_rect()
done = False

class ObjectClass:
    def __init__(self,tag,image,color, pos):
        #self.image = pygame.image.load(image)
        self.image = pygame.Surface((50,50)).convert()
        self.image.fill(color)
        self.tag = tag
        self.rect = self.image.get_rect(center=pos)
       
    def draw(self, surf):
        surf.blit(self.image, self.rect)
       
def get_rand_width(screen_rect):
    return random.randint(1,screen_rect.width)

objects = [
    ObjectClass('tree','tree.png', (255,0,0), (get_rand_width(screen_rect),500)),
    ObjectClass('rock','rock.png', (0,255,0), (get_rand_width(screen_rect),500)),
    ObjectClass('fence','fence.png', (0,0,255), (get_rand_width(screen_rect),500)),
]
selected_object = random.choice(objects)

while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
    selected_object.draw(screen)
we will be moving to python-forum.io on October 1 2016
more details here
User avatar
metulburr
 
Posts: 2244
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: getting objects to draw on the screen at a random locati

Postby LavaCreeperKing » Wed Apr 27, 2016 7:30 pm

Metulburr, I tried your code and all I get is a black screen nothing happens. I just copied and pasted it, but when I run it all I am getting is a black screen pygame does not crash so the code is working but nothing is being drawn on the screen.
LavaCreeperKing
 
Posts: 103
Joined: Wed Sep 02, 2015 11:31 pm

Re: getting objects to draw on the screen at a random locati

Postby metulburr » Wed Apr 27, 2016 8:01 pm

oh i missed copying over pygame.display.update()

add
Code: Select all
pygame.display.update()

at the end of the while loop in the same indentation of
Code: Select all
    selected_object.draw(screen)
    pygame.display.update()
we will be moving to python-forum.io on October 1 2016
more details here
User avatar
metulburr
 
Posts: 2244
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: getting objects to draw on the screen at a random locati

Postby LavaCreeperKing » Thu Apr 28, 2016 9:58 pm

ok
LavaCreeperKing
 
Posts: 103
Joined: Wed Sep 02, 2015 11:31 pm

Re: getting objects to draw on the screen at a random locati

Postby LavaCreeperKing » Thu Apr 28, 2016 10:26 pm

Alright, so after using your code it helped me to under stand a little better so I then created this.
Code: Select all
class ObjectClass(pygame.sprite.Sprite):
    def __init__(self,type,img,xcord):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load(img)
        self.type = type
        self.rect = self.image.get_rect()
        self.rect.center = [xcord, 500]

def choose():
    type = random.choice(['tree', 'rock', 'fence'])
    if type =='tree': img = 'skier_tree.png'
    if type =='rock': img = 'skier_rock.png'
    if type =='fence': img = 'skier_fence.png'
    xcord = random.randint(0,600)
    obsticle = ObjectClass(type,img,xcord)
    objects.append(obsticle)

def animate():
    for object in objects:
        screen.blit(object)
    pygame.display.update()

pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode([640,640])
objects = []

while True:
    clock.tick(20)
    choose()
    animate()

Having one little problem with it though. It is not liking 'screen.blit(object)'. So would this work for what I am trying to do? I am trying to recreate the skier game in my own way.
LavaCreeperKing
 
Posts: 103
Joined: Wed Sep 02, 2015 11:31 pm

Re: getting objects to draw on the screen at a random locati

Postby micseydel » Thu Apr 28, 2016 10:36 pm

I recommend you not use "type" and "object" as variable names, since they're built-in stuff, and that you get rid of your global variables (objects especially). The globals especially are going to cause huge problems very quickly.
Due to the reasons discussed here we will be moving to python-forum.io on October 1, 2016.

This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
User avatar
micseydel
 
Posts: 3000
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: getting objects to draw on the screen at a random locati

Postby LavaCreeperKing » Mon May 02, 2016 4:45 pm

So I did some stuff with the code that metulburr gave me to try and figure out the best way to do what I am trying to do but the problem is that the objects only draw once. I need to take the selected objects and add them to a new list that keeps having more added to it. That selected objects was part of it but now that I have a way to choose an abject and location I now need to have it keep creating more and drawing them on the screen. I tried to do this by making this change at the end of his code but it still only draws one of each object. And I added things = [] to pygame.init()
Code: Select all
while not done:
    clock.tick(20)
    selected_object = random.choice(objects)
    things.append(selected_object)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
    for selected_object in things:
        screen.blit(selected_object.image, selected_object.rect)
        pygame.display.update()
LavaCreeperKing
 
Posts: 103
Joined: Wed Sep 02, 2015 11:31 pm


Return to Game Development

Who is online

Users browsing this forum: No registered users and 4 guests