dimanche 27 octobre 2019

How can I generate random pics with python?

I'm trying to solve this problem, I want to draw an image in random positions, so I've declared a global variable that at the beggining is assigned at a negative value (count = -randint). Then in tick function (which is called by main() every 30ms) I increment the count variable and when it's higher than a established value I create the object Hole (that is the pic that I want to draw) and I reset count to the value count = -randint. The problem I've faced is that the different pics are not generated randomly but they are placed in a specific series that is repeated each time they are generated. I put in the description also the module I've used to run the graphycs. Thanks for your attention. https://github.com/tomamic/fondinfo/tree/master/examples Is called g2d.py Here is the code:

#Title: Rovers and Backgrounds
#Date 07/10/19
#Autor: Giovanni Sgobazzi
#Identification number: 305356

import g2d
from random import randint
from actor import Actor, Arena
sprites = g2d.load_image("moon-patrol.png")
bckd = g2d.load_image("moon-patrol-bg.png")
arena = Arena((1000, 700))
ARENA_W, ARENA_H = arena.size()
count = -randint(200,500)

class Background():

    def __init__(self, arena):
        self._arena_w, self._arena_h = arena.size()
        self._w, self._h = 512, 150
        self._w1, self._h1 = 512, 125
        self._dx = 0
        self._x = 0
        self._dx1 = 0
        self._x1 = 0
        self._dx2 = 0
        self._x2 = 0
        self._ground = self._arena_h * 2/9

    def move(self):
        self._x = self._dx % self._arena_w  ## First Background
        g2d.draw_image_clip(bckd,(0, 100, self._w, self._h),(self._x - self._arena_w, 0, self._arena_w, self._arena_h))
        if self._x < self._arena_w:
            g2d.draw_image_clip(bckd, (0, 100, self._w, self._h), (self._x, 0, self._arena_w, self._arena_h))
        self._dx -= 1

        self._x1 = self._dx1 % self._arena_w   ## Second Background
        g2d.draw_image_clip(bckd,(0, 385, self._w1, self._h1),(self._x1 - self._arena_w, self._arena_h * 5/9 - 40, self._arena_w, self._arena_h * 5/9))             
        if self._x1 < self._arena_w:
            g2d.draw_image_clip(bckd,(0, 385, self._w1, self._h1), (self._x1, self._arena_h * 5/9 - 40, self._arena_w, self._arena_h * 5/9))                                                       
        self._dx1 -= 3

        self._x2 = self._dx2 % self._arena_w    ## Third Background
        g2d.draw_image_clip(bckd,(0, 513, self._w1, self._h1),(self._x2 - self._arena_w, self._arena_h * 7/9, self._arena_w, self._ground))                                                      
        if self._x2 < self._arena_w:
            g2d.draw_image_clip(bckd, (0, 513, self._w1, self._h1), (self._x2, self._arena_h * 7/9, self._arena_w, self._ground))
        self._dx2 -= 5

class Rover(Actor):

    def __init__(self, arena, pos):
        self._arena = arena
        self._dim = 3/2
        self._x , self._y = pos
        self._speed = 10
        self._w, self._h = 35 * self._dim, 25 * self._dim
        self._dx, self._dy = 0, 0
        self._arena_w, self._arena_h = arena.size()
        self._ground = self._arena_h - self._y
        self._arena.add(self)

    def move(self):
        self._y += self._dy
        if self._y < 0:
            self._y = 0
        elif self._y > self._arena_h - self._ground - self._h:
            self._y = self._arena_h - self._ground - self._h
        else:
            self._dy += 0.4

        self._x += self._dx
        if self._x < 0:
            self._x = 0
        elif self._x > self._arena_w - self._w:
            self._x = self._arena_w - self._w

    def go_up(self):
        if self._y >= self._arena_h - self._ground - self._h :
            self._dy = -self._speed

    def go_right(self):
        self._dx = self._speed

    def go_left(self):
        self._dx = -self._speed

    def stay(self):
        self._dy, self._dx = 0, 0

    def symbol(self):
        return 210, 155, 35, 25

    def position(self):
        return self._x, self._y, self._w, self._h

    def collide(self,other):
        if isinstance(other, Hole):
            self._arena.remove(self)


class Hole(Actor):
    def __init__(self, arena):
        self._arena = arena
        self._arena_w, self._arena_h = arena.size()
        self._w, self._h = 30, 30
        self._dx = 0
        self._x = 0
        self._ground = self._arena_h * 2/9
        self._arena.add(self)

    def move(self):
        self._x = self._dx % self._arena_w    
        self._dx -= 5
        if self._x < 0:
            self._arena.remove(self)

    def symbol(self):
        return 130, 165, self._w, self._h

    def position(self):
        return self._x, self._arena_h - self._ground - 1, self._w, self._h

    def collide(self,other):
        pass     

b = Background(arena)
r = Rover(arena, (150, ARENA_H * 7/9 + 3))

def tick():
    global count
    count += 1
    g2d.clear_canvas()    
    arena.move_all()
    b.move()    
    if count >= 100:
        Hole(arena)
        count = -randint(200,500)

    if g2d.key_pressed("Spacebar"):
        r.go_up()

    if g2d.key_pressed("ArrowRight"):
        r.go_right()

    if g2d.key_pressed("ArrowLeft"):
        r.go_left()

    if (g2d.key_released("ArrowRight") or g2d.key_released("ArrowLeft")):
        r.stay()

    for a in arena.actors():
        if isinstance(a,Hole):
            g2d.draw_image_clip(sprites, a.symbol(), a.position())

        if isinstance(a,Rover):
            g2d.draw_image_clip(sprites, a.symbol(), a.position())


def main():    
    g2d.init_canvas(arena.size())
    g2d.main_loop(tick)
main()



Aucun commentaire:

Enregistrer un commentaire