Help gradually growing image

This is the place for queries that don't fit in any of the other categories.

Help gradually growing image

Postby Hissing Cockroach » Tue Dec 31, 2013 12:40 am

I am trying to create a projectile, growing to simulate it getting
closer. this is one of my attempts:

Code: Select all
import random
from random import randint
import sys
import time
import pygame

def image_from_url(url):
    try:
        from urllib2 import urlopen
        from cStringIO import StringIO as inout
    except ImportError:
        from urllib.request import urlopen
        from io import BytesIO as inout
    myurl = urlopen(url)
    return inout(myurl.read())

Asteroid_URL = ('http://i1315.photobucket.com/albums/t600/11roadkills/f3252a21-352d-49ca-a7ae-eac85c18b4b7_zpsd97af755.png?t=1387696943')
Explosion_URL = ('http://i1315.photobucket.com/albums/t600/11roadkills/explosion_zpsbbc7bb55.png')


BLACK = (0,0,0)
RED = (255,0,0)
GREEN = (0,255,0)

pygame.init()
screen = pygame.display.set_mode((900,700))
pygame.display.set_caption('Astroid feild')
size = 5

def meteor():
    setplace = randint(0,900)
    asteroidimg = pygame.image.load(image_from_url(Asteroid_URL))
    asteroid_img = pygame.transform.scale(asteroidimg, (size, size))
    asteroid = asteroid_img.get_rect(topleft=(setplace))
    screen.blit(asteroid_img, asteroid)

while True:
    screen.fill(BLACK)

    size += 5
    meteor()

    for event in pygame.event.get():
        if evet.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    pygame.display.update()
           
   



It returns this error:

Code: Select all
Traceback (most recent call last):
  File "C:/Users/Hissing Cockroach/Desktop/asteroid field 2.py", line 40, in <module>
    meteor()
  File "C:/Users/Hissing Cockroach/Desktop/asteroid field 2.py", line 34, in meteor
    asteroid = asteroid_img.get_rect(topleft=(setplace))
TypeError: invalid rect assignment


Can anyone please help with this?
User avatar
Hissing Cockroach
 
Posts: 108
Joined: Sat Sep 14, 2013 2:35 pm
Location: Earth

Re: Help gradually growing image

Postby metulburr » Tue Dec 31, 2013 1:21 am

well this error is due to the fact the Rect.topright is not an int, it is a tuple with 2 indexes of ints. the x and y coordinates. You only have one.

1) What exactly are you trying to do? One asteriod at a random spot or many asteroids at random spots? Regardless you should not be loading the image each loop. It should be done once. Especially since its loading from the web each time.

2) first letter capitalized in python set aside for class names
Code: Select all
Asteroid_URL
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1563
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Help gradually growing image

Postby Hissing Cockroach » Tue Dec 31, 2013 7:45 pm

metulburr wrote:well this error is due to the fact the Rect.topright is not an int, it is a tuple with 2 indexes of ints. the x and y coordinates. You only have one.


what would I do instead?
User avatar
Hissing Cockroach
 
Posts: 108
Joined: Sat Sep 14, 2013 2:35 pm
Location: Earth

Re: Help gradually growing image

Postby metulburr » Tue Dec 31, 2013 8:36 pm

Hissing Cockroach wrote:
metulburr wrote:well this error is due to the fact the Rect.topright is not an int, it is a tuple with 2 indexes of ints. the x and y coordinates. You only have one.


what would I do instead?

I thought i already stated the problem and the fix. I dont know what else to tell you except to explicitly post the code.

Code: Select all
asteroid = asteroid_img.get_rect(topleft=(setplace))

setplace is currently a random integer. topleft needs to be coordinates...a tuple of 2 integers. such as (800,600)
Code: Select all
setplace = randint(0,900)

setplace is only being assinged an int

you would need to do something like:
Code: Select all
setplace =(randint(0,900), randint(0,700))

but that just fixes the error. The end result is something i dont think you meant it to be though.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1563
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Help gradually growing image

Postby metulburr » Tue Dec 31, 2013 8:48 pm

i think you meant to do something like this though:
Code: Select all
import random
from random import randint
import sys
import time
import pygame

def image_from_url(url):
    try:
        from urllib2 import urlopen
        from cStringIO import StringIO as inout
    except ImportError:
        from urllib.request import urlopen
        from io import BytesIO as inout
    myurl = urlopen(url)
    return inout(myurl.read())

Asteroid_URL = ('http://i1315.photobucket.com/albums/t600/11roadkills/f3252a21-352d-49ca-a7ae-eac85c18b4b7_zpsd97af755.png?t=1387696943')
Explosion_URL = ('http://i1315.photobucket.com/albums/t600/11roadkills/explosion_zpsbbc7bb55.png')


BLACK = (0,0,0)
RED = (255,0,0)
GREEN = (0,255,0)

pygame.init()
screen = pygame.display.set_mode((900,700))
pygame.display.set_caption('Astroid feild')
size = 5
setplace = (randint(0,450), randint(0,350))
asteroidimg = pygame.image.load(image_from_url(Asteroid_URL))

def meteor(setplace):
    asteroid_img = pygame.transform.scale(asteroidimg, (size, size))
    asteroid = asteroid_img.get_rect(topleft=(setplace))
    screen.blit(asteroid_img, asteroid)

while True:
    screen.fill(BLACK)

    size += 1
    meteor(setplace)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    pygame.display.update()
           
   


New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1563
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Help gradually growing image

Postby Hissing Cockroach » Tue Dec 31, 2013 9:03 pm

Thanks!, but would you have any idea on how make multiple asteroids
coming at one time? Would I just create multiple functions or something?
User avatar
Hissing Cockroach
 
Posts: 108
Joined: Sat Sep 14, 2013 2:35 pm
Location: Earth

Re: Help gradually growing image

Postby metulburr » Tue Dec 31, 2013 11:36 pm

you would just make a loop when creating the objects and append the objects to a list along with their rects, then loop that list for updating and rendering.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1563
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Help gradually growing image

Postby Hissing Cockroach » Wed Jan 01, 2014 2:10 am

Could you please explain that? I'm not quite sure what you meant.
User avatar
Hissing Cockroach
 
Posts: 108
Joined: Sat Sep 14, 2013 2:35 pm
Location: Earth

Re: Help gradually growing image

Postby Mekire » Wed Jan 01, 2014 10:14 am

I've said this many times before. Your short responses and complete lack of updated attempts don't inspire people to want to help you.

This is the way this should work:
You state your problem; include your code; and describe your error (including the traceback if applicable).
You did this step. You received help (quite good help actually).

Now you should go back and try to apply what he said and research things that you didn't understand. Next time you post we should see a completely updated attempt. Instead you give a one line response with the feel of, "Gimmeh t3h codez."

Here is some further advice. Learn about classes (do it now for @#$% sake). Then write a very simple asteroid class. Create multiple instances of them that update every frame. You want to create multiple objects that all use the same logic but have different specific values. This is EXACTLY what classes are for.

-Mek
User avatar
Mekire
 
Posts: 1142
Joined: Thu Feb 07, 2013 11:33 pm
Location: Asakusa, Japan

Re: Help gradually growing image

Postby metulburr » Wed Jan 01, 2014 3:01 pm

I've said this many times before. Your short responses and complete lack of updated attempts don't inspire people to want to help you.

This is the way this should work:
You state your problem; include your code; and describe your error (including the traceback if applicable).
You did this step. You received help (quite good help actually).

Now you should go back and try to apply what he said and research things that you didn't understand. Next time you post we should see a completely updated attempt. Instead you give a one line response with the feel of, "Gimmeh t3h codez."

Here is some further advice. Learn about classes (do it now for @#$% sake). Then write a very simple asteroid class. Create multiple instances of them that update every frame. You want to create multiple objects that all use the same logic but have different specific values. This is EXACTLY what classes are for.

+1

you really really really need to learn classes
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1563
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY


Return to General Coding Help

Who is online

Users browsing this forum: knightrider179 and 6 guests