movement program

This is the place to post any code that you want to share with the community. Only completed scripts should be posted here.

movement program

Postby Hissing Cockroach » Thu Oct 17, 2013 11:08 pm

My first png image that moves at my will:

Code: Select all
import pygame, sys
from pygame.locals import *

pygame.init()

FPS = 30
FPSCLOCK = pygame.time.Clock()


DISPLAYSURF = pygame.display.set_mode((900,700),0,32)
pygame.display.set_caption('Freeroam')



WHITE = (255,255,255)
img = pygame.image.load('You'r PNG image here.png')
x = 80
y = 100

while True:
    DISPLAYSURF.fill(WHITE)
    key = pygame.key.get_pressed()
    if key[pygame.K_LEFT]:
        x -= 5
    if key[pygame.K_RIGHT]:
        x += 5
    if key[pygame.K_UP]:
        y -= 5
    if key[pygame.K_DOWN]:
        y += 5

   

    DISPLAYSURF.blit(img, (x,y))

    for event in pygame.event.get():
        mousex,mousey = pygame.mouse.get_pos()
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    pygame.display.update()


since I couldn't attach the image that goes with it, I
just put "You'r PNG image here" where the image goes
in the code.
User avatar
Hissing Cockroach
 
Posts: 108
Joined: Sat Sep 14, 2013 2:35 pm
Location: Earth

Re: movement program

Postby metulburr » Thu Oct 17, 2013 11:28 pm

since I couldn't attach the image that goes with it, I
just put "You'r PNG image here" where the image goes
in the code.

if your going to be doing a lot of pygame, i would suggust to use a github repo or bare minimum use an image from a url to give out your resources. For one image its not bad, but imagine if you have 10+ images you needed us to replace with our own.

here is an example of using mekire's method in your code:
(the try except accounts for both python2.x and python3.x)
Code: Select all
import pygame, sys
from pygame.locals import *

pygame.init()

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())
   
url = 'http://i1297.photobucket.com/albums/ag23/metulburr/spaceship2_zps095c332a.png'

FPS = 30
FPSCLOCK = pygame.time.Clock()


DISPLAYSURF = pygame.display.set_mode((900,700),0,32)
pygame.display.set_caption('Freeroam')



WHITE = (255,255,255)
img = pygame.image.load(image_from_url(url))
x = 80
y = 100

while True:
    DISPLAYSURF.fill(WHITE)
    key = pygame.key.get_pressed()
    if key[pygame.K_LEFT]:
        x -= 5
    if key[pygame.K_RIGHT]:
        x += 5
    if key[pygame.K_UP]:
        y -= 5
    if key[pygame.K_DOWN]:
        y += 5

   

    DISPLAYSURF.blit(img, (x,y))

    for event in pygame.event.get():
        mousex,mousey = pygame.mouse.get_pos()
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    pygame.display.update()


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

Re: movement program

Postby Hissing Cockroach » Fri Oct 18, 2013 2:07 am

Hey, thanks for the feedback!
But what do I use besides the star import?
User avatar
Hissing Cockroach
 
Posts: 108
Joined: Sat Sep 14, 2013 2:35 pm
Location: Earth

Re: movement program

Postby Mekire » Fri Oct 18, 2013 7:33 am

The only line in your program currently that is making use of the star import is this:
Code: Select all
if event.type == QUIT:

Just change that to this:
Code: Select all
if event.type == pygame.QUIT:
and you can get rid of the star import.

Also, you forgot to use your clock at the end of your main loop.

You need to add a:
Code: Select all
FPSCLOCK.tick(FPS)

You also might consider using a pygame.Rect for your sprite location(that's what they are for), and then using pygame.Rect.clamp_ip to keep your character on the screen.

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

Re: movement program

Postby Hissing Cockroach » Fri Oct 18, 2013 3:08 pm

Mekire wrote:You also might consider using a pygame.Rect for your sprite location(that's what they are for), and then using pygame.Rect.clamp_ip to keep your character on the screen.
-Mek


It says in run module:

Code: Select all
Traceback (most recent call last):
  File "C:\Users\Family\Desktop\pys\Hissing Cockroach2.py", line 36, in <module>
    pygame.rect.clamp_ip(img)
AttributeError: 'module' object has no attribute 'clamp_ip'


pygame.Rect.clamp_ip(img) was the second line in my for loop.
Do I have to put it in a different place or something?
User avatar
Hissing Cockroach
 
Posts: 108
Joined: Sat Sep 14, 2013 2:35 pm
Location: Earth

Re: movement program

Postby Mekire » Fri Oct 18, 2013 3:29 pm

Code: Select all
import pygame, sys

pygame.init()

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())

url = 'http://i1297.photobucket.com/albums/ag23/metulburr/spaceship2_zps095c332a.png'

FPS = 60
FPSCLOCK = pygame.time.Clock()


DISPLAYSURF = pygame.display.set_mode((900,700),0,32)
DISPLAY_RECT = DISPLAYSURF.get_rect()
pygame.display.set_caption('Freeroam')



img = pygame.image.load(image_from_url(url))
img.set_colorkey((255,0,255))
rect = img.get_rect(topleft=(80,100))

while True:
    DISPLAYSURF.fill(0)
    key = pygame.key.get_pressed()
    if key[pygame.K_LEFT]:
        rect.x -= 5
    if key[pygame.K_RIGHT]:
        rect.x += 5
    if key[pygame.K_UP]:
        rect.y -= 5
    if key[pygame.K_DOWN]:
        rect.y += 5
    rect.clamp_ip(DISPLAY_RECT)



    DISPLAYSURF.blit(img,rect)

    for event in pygame.event.get():
        mousex,mousey = pygame.mouse.get_pos()
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
    FPSCLOCK.tick(FPS)
    pygame.display.update()

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

Re: movement program

Postby Hissing Cockroach » Fri Oct 18, 2013 7:34 pm

Is it just my computer, or does the pygame window really never respond?
User avatar
Hissing Cockroach
 
Posts: 108
Joined: Sat Sep 14, 2013 2:35 pm
Location: Earth

Re: movement program

Postby metulburr » Fri Oct 18, 2013 7:42 pm

define never respond? I do not have a problem with the pygame windows given the last code.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1469
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: movement program

Postby Hissing Cockroach » Fri Oct 18, 2013 8:24 pm

Maybe it's my computor.
User avatar
Hissing Cockroach
 
Posts: 108
Joined: Sat Sep 14, 2013 2:35 pm
Location: Earth

Re: movement program

Postby Mekire » Sat Oct 19, 2013 3:09 am

Well it is loading an image from the internet. If you have an extremely slow internet connection I suppose the program might freeze up before it loads.

You could always
Hissing Cockroach wrote:just put "You'r PNG image here" where the image goes
in the code
and see if it works.

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

Re: movement program

Postby Hissing Cockroach » Sat Oct 19, 2013 2:00 pm

I copied and pasted it again and it worked. last
time I must have accidentally missed a line or something
User avatar
Hissing Cockroach
 
Posts: 108
Joined: Sat Sep 14, 2013 2:35 pm
Location: Earth


Return to Completed Scripts

Who is online

Users browsing this forum: Bing [Bot] and 2 guests