[Pygame] adjust speed while givng up choppy movement

[Pygame] adjust speed while givng up choppy movement

Postby metulburr » Wed May 08, 2013 1:19 am

OK i abandoned that alchemy mimic for now. Well, actually set it aside for later. game programming is just not clicking for me, so i figured to jsut start at the way beginning.
Something like a space invaders i guess.

Now i saw in other code given of yours @Makire, that you did not have that choppy movement from the speed just being the pixel movement of next frame, which makes sense. The faster you increment it, the more choppy it gets. The way i am leaning is animating it 4 pixels, bliting it each pixel, to remove the choppy feeling. I am just not sure if 1 this is the best way, or 2 even how to do that

Code: Select all
import pygame

class Player:
   def __init__(self):
      self.speed = 4
      self.orig_image = pygame.image.load('/home/metulburr/Pictures/spaceship.png').convert()
      self.orig_image.set_colorkey((255,0,255))
      
      
      self.image = pygame.transform.rotate(self.orig_image, 180)
      self.mask = pygame.mask.from_surface(self.image)
      self.image_rect = self.image.get_rect()
      
   def move(self, x, y):
      self.image_rect[0] += x * self.speed
      self.image_rect[1] += y * self.speed

class Control:
   def __init__(self):
      self.screensize = (600,600)
      self.screen = pygame.display.set_mode(self.screensize)
      self.clock = pygame.time.Clock()
      self.player = Player()
      self.mouse_press = False
      self.mouse_held = False
      
      self.mainloop()
      
   def mainloop(self):
      run = True
      while run:
         for event in pygame.event.get():
            if event.type == pygame.QUIT:
               run = False
         self.screen.fill((0,0,0))
         self.update()
         pygame.display.flip()
         self.clock.tick(60)
         
   def update(self):
      self.keys = pygame.key.get_pressed()
      if self.keys[pygame.K_UP]:
         self.player.move(0,-1)
      if self.keys[pygame.K_DOWN]:
         self.player.move(0,1)
      if self.keys[pygame.K_RIGHT]:
         self.player.move(1,0)
      if self.keys[pygame.K_LEFT]:
         self.player.move(-1,0)
      
      self.screen.blit(self.player.image, self.player.image_rect)
      
      self.mouse_press = False
         
app = Control()
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1500
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: [Pygame] adjust speed while givng up choppy movement

Postby Mekire » Wed May 08, 2013 1:40 am

Seems pretty much fine to me. You have to keep in mind that most displays won't actually show more than 60 frames per second. Things moving really fast will be a bit choppy; but honestly your users won't really notice this. As the coder you are just ultra aware of it (or at least that is how I feel).

Anyway... looking pretty good. Few minor changes; take or leave them.
Code: Select all
import pygame
import os,sys

DIRECTDICT = {pygame.K_LEFT  : (-1, 0),
              pygame.K_RIGHT : ( 1, 0),
              pygame.K_UP    : ( 0,-1),
              pygame.K_DOWN  : ( 0, 1)}

class Player:
   def __init__(self):
      self.speed = 4
##      self.orig_image = pygame.image.load('/home/metulburr/Pictures/spaceship.png').convert()
##      self.image = pygame.transform.rotate(self.orig_image, 180)
      self.image = pygame.Surface((50,50)).convert()
      self.image.fill((255,0,0))
      self.image_rect = self.image.get_rect()
   def move(self, x, y):
      self.image_rect.move_ip(x*self.speed,y*self.speed)

class Control:
   def __init__(self):
      self.screensize = (600,600)
      self.screen = pygame.display.set_mode(self.screensize)
      self.clock = pygame.time.Clock()
      self.player = Player()
      self.mouse_press = False
      self.mouse_held = False

      self.mainloop()

   def mainloop(self):
      run = True
      while run:
         for event in pygame.event.get():
            if event.type == pygame.QUIT:
               run = False
         self.screen.fill((0,0,0))
         self.update()
         pygame.display.flip()
         self.clock.tick(60)

   def update(self):
      self.keys = pygame.key.get_pressed()
      for direction in DIRECTDICT:
        if self.keys[direction]:
           self.player.move(*DIRECTDICT[direction])
      self.screen.blit(self.player.image,self.player.image_rect)

if __name__ == "__main__":
   app = Control()
   pygame.quit();sys.exit()

And your three space tabs still piss me off :mrgreen: conform damn you.
-Mek
User avatar
Mekire
 
Posts: 1011
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: [Pygame] adjust speed while givng up choppy movement

Postby metulburr » Wed May 08, 2013 1:45 am

And your three space tabs still piss me off :mrgreen: conform damn you.

lmao

it should be 4 spaced tabs.
yeah, i really should switch, its just i started off in python doing it for less key presses using 4 spaced tabs. IT seemed at the time easier to hit one tab instead of 4 space bars,. but the habit stuck, and i cant break it.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1500
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: [Pygame] adjust speed while givng up choppy movement

Postby Mekire » Wed May 08, 2013 1:58 am

My tab key adds four spaces, not 1 tab, in my IDE (should be the same for most good ones (or at the very least adjustable)). If I actually had to hit spacebar four times I might reconsider too.

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

Re: [Pygame] adjust speed while givng up choppy movement

Postby metulburr » Wed May 08, 2013 2:00 am

oh yeah i forgot about the clamp_ip().

Ok the image i have is way off the pixels of the actual image, so a somewhat massive background. So i guess the easiest way would be to just simply auto crop the image, and then there is the game way, of using masks? Which i am not entirely sure how to check the mask of the image with the screen. I tried making a screen mask, but the methods of checking i seen are checking if they overlap, in which they always and should always be. But checking if they do not is what i would be looking for.

I dont know, maybe just skip that and go the easy route for now of jsut cropping hte image in GIMP.
Code: Select all
self.screenrect = self.screen.get_rect()
...
      self.player.rect.clamp_ip(self.screenrect)
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1500
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: [Pygame] adjust speed while givng up choppy movement

Postby metulburr » Wed May 08, 2013 2:05 am

in my IDE, tabs make a tab, i never seen a method of the tab inserting 4 spaces upon every tab press. I mean my IDE, there is an option, to convert all tabs to spefied spaces, or spaces to tabs, but not on key press.

I just forget to convert the document from tabs to spaces before sending it out

EDIT:
oh haha, just figured it out. Thats funny that i went that long without even playing around with this IDE
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1500
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: [Pygame] adjust speed while givng up choppy movement

Postby metulburr » Wed May 08, 2013 10:12 pm

i dont know, but maybe i am ultra sensitive to it being the coder, but to me this method looks like the ship is jumping when moving, and becuase of the speed of the bullet, that it jumps back and forth. Sort of a shadow bullet and a current bullet for every single bullet.
The ship i could let slide at 4 pixels, but the bullet at 8 pixels looks horrible to me.

Code: Select all
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())

class Projectile:
    limit = 5
    def __init__(self, loc):
        self.surf = pygame.Surface((5,10)).convert()
        self.surf.fill((255,255,255))
        self.rect = self.surf.get_rect()
        self.rect.center = loc
        self.speed = 8


class Player:
    def __init__(self):
        self.speed = 4
       
        link = 'http://i1297.photobucket.com/albums/ag23/metulburr/spaceship2_zps095c332a.png'
        self.orig_image = pygame.image.load(image_from_url(link)).convert()
        self.orig_image = pygame.transform.scale(self.orig_image, (40,80))
        self.orig_image.set_colorkey((255,0,255))
       
        self.image = pygame.transform.rotate(self.orig_image, 180)
        self.mask = pygame.mask.from_surface(self.image)
        self.rect = self.image.get_rect()
       
        self.bullets = []
       
    def move(self, x, y):
        self.rect[0] += x * self.speed
        self.rect[1] += y * self.speed

       
class Control:
    def __init__(self):
        self.screensize = (800,600)
        self.screen = pygame.display.set_mode(self.screensize)
        self.screenrect = self.screen.get_rect()
        self.clock = pygame.time.Clock()
        self.player = Player()
 
       
        self.mainloop()
       
    def mainloop(self):
        run = True
        while run:
            self.screen.fill((0,0,0))
           
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    run = False
                elif event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_SPACE:
                        if len(self.player.bullets) <= Projectile.limit:
                            self.player.bullets.append(Projectile(self.player.rect.center))
           
            self.update()
            pygame.display.flip()
            self.clock.tick(60)
           
    def update(self):
        print(len(self.player.bullets))
        self.keys = pygame.key.get_pressed()

        if self.player.bullets:
            for obj in self.player.bullets:
                self.screen.blit(obj.surf, obj.rect)
                obj.rect[1] -= obj.speed
                #obj.loc = (obj.rect[0], obj.rect[1] - obj.speed)
 
                if obj.rect[1] < 0:
                    self.player.bullets.remove(obj)
       
        if self.keys[pygame.K_UP]:
            self.player.move(0,-1)
        if self.keys[pygame.K_DOWN]:
            self.player.move(0,1)
        if self.keys[pygame.K_RIGHT]:
            self.player.move(1,0)
        if self.keys[pygame.K_LEFT]:
            self.player.move(-1,0)
       
        self.player.rect.clamp_ip(self.screenrect) #make sure rect does not go outside of screen
        self.screen.blit(self.player.image, self.player.rect)
           
app = Control()
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1500
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: [Pygame] adjust speed while givng up choppy movement

Postby Mekire » Thu May 09, 2013 12:13 am

metulburr wrote:I don't know, but maybe i am ultra sensitive to it being the coder
Well honestly it looks about right to me. It isn't nearly as noticeable as you believe.
Sort of a shadow bullet and a current bullet for every single bullet.
I actually do know exactly what you mean, but I don't think there is anything you can do about this. I think if you stare hard enough at anyone else's games you will probably notice the exact same thing. The thing is, we never do that. The eye is capable of seeing that 60 fps isn't consistent if it strains, but it is much more likely to assume movement has occurred and fill in the gaps [Citation Needed]

Take a look at this guy's Space Invader style game (and you won't find anyone better on Pygame) and you will see the same things I believe.
QANAT

Check out his other projects too if you get a chance. He does some very impressive stuff with what are actually extremely simple graphics:
SubTerrex
Magnetospheres
Fission Seed
Extinguished
Sometimes they take a bit of effort to get running due to dependencies but some very good stuff.

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

Re: [Pygame] adjust speed while givng up choppy movement

Postby metulburr » Thu May 09, 2013 1:15 am

that is true. I did play that before and never even realized that it does that.

Yeah i played all his, he is pretty good
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1500
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY


Return to Game Development

Who is online

Users browsing this forum: No registered users and 1 guest