Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

how do I make these images scattered over the screen?

$
0
0

I would like to know where I got wrong in typing the codeso here's the code...

from tkinter import*import randomimport timeclass Coords:    def __init__(self, x1=0, y1=0, x2=0, y2=0):        self.x1 = x1        self.y1 = y1        self.x2 = x2        self.y2 = y2    def within_x(co1, co2):        if co1.x1 > co2.x1 and co1.x1 < co2.x1:            return True        elif co1.x2 > co2.x1 and co1.x2 < co2.x2:            return True        elif co2.x1 > co1.x1 and co2.x1 < co1.x1:            return True        elif co2.x2 > co1.x1 and co2.x2 < co1.x1:            return True        else:            return Falsedef within_y(co1, co2):    if (co1.y1 > co2.y1 and co1.y1 < co2.y2) \       or (co1.y2 > co2.y1 and co1.y2 < co2.y2)\       or (co2.y1 > co1.y1 and co2.y1 < co1.y1) \       or (co2.y2 > co1.y1 and co2.y2 < co1.y1):        return True    else:         return Falsedef collided_right(co1, co2):    if within_y(co1, co2):        if co1.x2 >= co2.x1 and co1.x2 <= co2.x2:            return True        return Falsedef collided_top(co1, co2):    if within_x(co1, co2):        if co1.y1 <= co2.y2 and co1.y1 >= co2.y1:            return True    return Falsedef collided_bottom(co1, co2):    if within_x(co1, co2):        y_calc = co1.y2 + y        if y_calc >= co2.y1 and y_calc <= co2.y2:            return True        return Falsedef collided_left(co1, co2):    if within_y(co1, co2):        if co1.x1 <= co2.x2 and co1.x1 >= co2.x1:            return True        return Falseclass Game:    def __init__(self):        self.tk = Tk()        self.tk.title("Mr. Stick Man Races for the Exit")        self.tk.resizable(0, 0)        self.tk.wm_attributes("-topmost", 1)        self.canvas = Canvas(self.tk, width=500, height=500, \                             highlightthickness=0)        self.canvas.pack()        self.tk.update()        self.canvas_height = 500        self.canvas_width = 500        self.bg = PhotoImage(file="backround.gif")        w = self.bg.width()        h = self.bg.height()        for x in range(0, 5):            for y in range(0, 5):                self.canvas.create_image(x * w, y * h, \                                         image=self.bg, anchor='nw')        self.sprites = []        self.Running = True    def mainloop(self):        while 1:            if self.Running == True:                for sprite in self.sprites:                    sprite.move()            self.tk.update_idletasks()            time.sleep(0.01)class Sprite:    def __init__(self, game):        self.game = game        self.endgame = False        self.coordinates = None    def move(self):        pass    def coords(self):        return self.coordinatesclass PlatformSprite(Sprite):    def __init__(self, game, photo_image, x, y, width, height):        Sprite.__init__(self, game)        self.photo_image = photo_image        self.image = game.canvas.create_image(x, y, \                                              image=self.photo_image, anchor='nw')        self.coordinates = Coords(x, y, x + width, y + height)g = Game()platform1 = PlatformSprite(g, PhotoImage(file="Large.gif"), \                           0, 480, 100, 10)platform1 = PlatformSprite(g, PhotoImage(file="Large.gif"), \                           150, 440, 100, 10)platform1 = PlatformSprite(g, PhotoImage(file="Large.gif"), \                           300, 400, 100, 10)g.sprites.append(platform1)g.sprites.append(platform1)g.sprites.append(platform1)g.mainloop()

it should come over the screen like this...enter image description here

but it comes like thisenter image description here


Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>