Position of rect

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

31 Replies - 1142 Views - Last Post: 07 February 2014 - 12:57 PM Rate Topic: -----

#1 project21124  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 40
  • Joined: 02-February 14

Position of rect

Posted 03 February 2014 - 01:54 PM

black = (0,0,0)
white = (255,255,255)
import pygame
pygame.init()
Screen = pygame.display.set_mode((1900, 1020))
pygame.display.set_caption("Man_Control/Collision")


class Hero_Sprite(pygame.sprite.Sprite):
	def __init__(self,x,y):
		pygame.sprite.Sprite.__init__(self)
		self.hero = pygame.image.load('/root/Pictures/hero.png').convert_alpha()
		self.x = x
		self.y = y
		self.rect = self.hero.get_rect()
		self.rect.move_ip(0,0)
	def drawHero(self):
		Screen.blit(self.hero, (self.x, self.y))
		if hero_sprite.x <= 0:
			hero_sprite.x += 2
		if hero_sprite.y <= 0:
			hero_sprite.y += 2
		if collide(hero_sprite, object_c):
			print "COLLIDE"
			Screen.blit(self.hero, (self.x, self.y))
		else: 
			print "NOT_COLLIDING"
			Screen.blit(self.hero, (self.x, self.y))
			

class Object(pygame.sprite.Sprite):
	def __init__(self, x, y):
		pygame.sprite.Sprite.__init__(self)
		self.object_c = pygame.image.load('/root/Pictures/grass.png')
		self.x = x
		self.y = y
		self.rect = self.object_c.get_rect()
		self.rect.move_ip(0,0)
	def drawObject(self):
			Screen.blit(self.object_c, (self.x, self.y))

def collide(Object1, Object2):
	if pygame.sprite.collide_rect(Object1, Object2):
		return True
	else:
		return False

hero_sprite = Hero_Sprite(200, 200)
object_c = Object(100, 100)
x=True
while x:
	for x in pygame.event.get():
		if x.type == pygame.QUIT:
			x=False
	if x.type == pygame.KEYDOWN:
		if x.key == pygame.K_LEFT:
			hero_sprite.rect.move_ip(-2, 0)
			hero_sprite.x -= 2
		if x.key == pygame.K_RIGHT:
			hero_sprite.rect.move_ip(2, 0)
			hero_sprite.x += 2
		if x.key == pygame.K_UP:
			hero_sprite.rect.move_ip(0, -2)
			hero_sprite.y -= 2
		if x.key == pygame.K_DOWN:
			hero_sprite.rect.move_ip(0, 2)
			hero_sprite.y += 2
	Screen.fill(white)
	object_c.drawObject()
	hero_sprite.drawHero()
	pygame.display.update()



For some reason, the rect of object_c(the one I use for collision), is positioned wherever hero_sprite.rect is positioned, instead of the (x,y) coordinates that the object_c image was given. Is get_rect() doing something wrong, or am I?

Is This A Good Question/Topic? 0
  • +

Replies To: Position of rect

#2 project21124  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 40
  • Joined: 02-February 14

Re: Position of rect

Posted 03 February 2014 - 03:35 PM

How can this possibly have 0 views?
Was This Post Helpful? 0
  • +
  • -

#3 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9487
  • View blog
  • Posts: 35,774
  • Joined: 12-June 08

Re: Position of rect

Posted 03 February 2014 - 03:37 PM

It happens.
Was This Post Helpful? 0
  • +
  • -

#4 project21124  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 40
  • Joined: 02-February 14

Re: Position of rect

Posted 03 February 2014 - 04:24 PM

Ahhh, you gotta love bugs in the system!
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10771
  • View blog
  • Posts: 40,104
  • Joined: 27-December 08

Re: Position of rect

Posted 03 February 2014 - 04:40 PM

The script to update views only does so periodically. Not really a bug, more like a delay.
Was This Post Helpful? 0
  • +
  • -

#6 project21124  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 40
  • Joined: 02-February 14

Re: Position of rect

Posted 03 February 2014 - 05:05 PM

View Postmacosxnerd101, on 03 February 2014 - 11:40 PM, said:

The script to update views only does so periodically. Not really a bug, more like a delay.


Ok, good!
Now that we have established that the view system works on this site, does anyone have any actual input on my problem?
Was This Post Helpful? 0
  • +
  • -

#7 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 116
  • View blog
  • Posts: 212
  • Joined: 11-January 13

Re: Position of rect

Posted 04 February 2014 - 03:27 AM

Quote

Is get_rect() doing something wrong, or am I?

I'm afraid you are doing a lot of stuff very wrong here. Strangest probably being that you are trying to refer to single global instances of your classes within their own definitions.

Anyway... try and understand everything here and get back to me with any questions on things you don't understand (images replaced with solid surfaces):
import os
import sys
import pygame as pg


BLACK = (0,0,0)
WHITE = (255,255,255)

DIRECT_DICT = {pg.K_LEFT  : (-1, 0),
               pg.K_RIGHT : ( 1, 0),
               pg.K_UP    : ( 0,-1),
               pg.K_DOWN  : ( 0, 1)}


class Hero(pg.sprite.Sprite):
    def __init__(self,x,y):
        pg.sprite.Sprite.__init__(self)
##        self.image = pg.image.load('/root/Pictures/hero.png').convert_alpha()
        self.image = pg.Surface((50,50)).convert()
        self.image.fill(pg.Color("red"))
        self.rect = self.image.get_rect(topleft=(x,y))
        self.speed = 4

    def update(self, obstacles, keys, screen_rect):
        for key in DIRECT_DICT:
            if keys[key]:
                self.rect.x += DIRECT_DICT[key][0]*self.speed
                self.rect.y += DIRECT_DICT[key][1]*self.speed
        self.rect.clamp_ip(screen_rect)
        self.check_collision(obstacles)

    def check_collision(self, other):
        if pg.sprite.collide_rect(self, other):
            print("COLLIDE")
        else:
            print("NOT_COLLIDING")

    def draw(self, surface):
        surface.blit(self.image, self.rect)


class Obstacle(pg.sprite.Sprite):
    def __init__(self, x, y):
        pg.sprite.Sprite.__init__(self)
##        self.image = pg.image.load('/root/Pictures/grass.png')
        self.image = pg.Surface((50,50)).convert()
        self.image.fill(pg.Color("blue"))
        self.rect = self.image.get_rect(topleft=(x,y))

    def draw(self, surface):
        surface.blit(self.image, self.rect)


class Control(object):
    def __init__(self):
        os.environ["SDL_VIDEO_CENTERED"] = "TRUE"
        pg.init()
        pg.display.set_caption("Man_Control/Collision")
        self.screen = pg.display.set_mode((500, 500)) #((1900, 1020))
        self.screen_rect = self.screen.get_rect()
        self.fps = 60.0
        self.clock = pg.time.Clock()
        self.hero = Hero(200, 200)
        self.obstacle = Obstacle(100, 100)
        self.done = False

    def event_loop(self):
        for event in pg.event.get():
            if event.type == pg.QUIT:
                self.done = True

    def draw(self):
        self.screen.fill(WHITE)
        self.obstacle.draw(self.screen)
        self.hero.draw(self.screen)

    def main_loop(self):
        while not self.done:
            keys = pg.key.get_pressed()
            self.event_loop()
            self.hero.update(self.obstacle, keys, self.screen_rect)
            self.draw()
            pg.display.update()
            self.clock.tick(self.fps)


if __name__ == "__main__":
    app = Control()
    app.main_loop()
    pg.quit()
    sys.exit()

-Mek

This post has been edited by Mekire: 04 February 2014 - 03:30 AM

Was This Post Helpful? 0
  • +
  • -

#8 project21124  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 40
  • Joined: 02-February 14

Re: Position of rect

Posted 04 February 2014 - 11:08 AM

Well, that introduces a lot of new material, I understand what you're doing here, but a lot of the functions in pygame, I didn't know existed, so I'll have to do some research..again.
I'm teaching myself this language without one of those crappy tutorials, so it takes a little bit longer to find specific functions within pygame.
Thanks!

This post has been edited by andrewsw: 04 February 2014 - 12:55 PM
Reason for edit:: Removed huge previous quote

Was This Post Helpful? 0
  • +
  • -

#9 project21124  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 40
  • Joined: 02-February 14

Re: Position of rect

Posted 04 February 2014 - 12:51 PM

You said to ask you questions about your version of the problem.
Here they are:
What does clamp_ip() do, and why did you use it?
What does line 56 do, and why do you use it?
Why would you use the whole fps/Clock thing(I know how it works, I just don't know what the point of it is)?
What in the world does line 87 do!?

It seems that you use the same basic checks for collision that I use, and I don't see much difference in the way that you make the rects. Why won't my object_c rect draw where it is supposed to when yours does, is that because you use the clamp_ip()?
Thanks again.
Was This Post Helpful? 0
  • +
  • -

#10 project21124  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 40
  • Joined: 02-February 14

Re: Position of rect

Posted 04 February 2014 - 02:01 PM

Please still answer the questions, but I fiddled with my code, and found the problem.
I wasn't setting the (x,y) values for the rect.
This code works fine! :bananaman:
black = (0,0,0)
white = (255,255,255)
import pygame
pygame.init()
Screen = pygame.display.set_mode((1900, 1020))
pygame.display.set_caption("Man_Control/Collision")


class Hero_Sprite(pygame.sprite.Sprite):
	def __init__(self,x,y):
		pygame.sprite.Sprite.__init__(self)
		self.hero = pygame.image.load('/root/Pictures/hero.png').convert_alpha()
		self.x = x
		self.y = y
		self.rect = self.hero.get_rect()
		self.rect.x = x
		self.rect.y = y
	def drawHero(self):
		Screen.blit(self.hero, (self.x, self.y))
		if hero_sprite.x <= 0:
			hero_sprite.x += 2
		if hero_sprite.y <= 0:
			hero_sprite.y += 2
		if collide(hero_sprite, object_c):
			print "COLLIDE"
			Screen.blit(self.hero, (self.x, self.y))
		else: 
			print "NOT_COLLIDING"
			Screen.blit(self.hero, (self.x, self.y))
			

class Object(pygame.sprite.Sprite):
	def __init__(self, x, y):
		pygame.sprite.Sprite.__init__(self)
		self.object_c = pygame.image.load('/root/Pictures/grass.png')
		self.x = x
		self.y = y
		self.rect = self.object_c.get_rect()
		self.rect.x = x
		self.rect.y = y
	def drawObject(self):
			Screen.blit(self.object_c, (self.rect.x, self.rect.y))
			print self.rect.x, self.rect.y

def collide(Object1, Object2):
	if pygame.sprite.collide_rect(Object1, Object2):
		return True
	else:
		return False

hero_sprite = Hero_Sprite(200, 200)
object_c = Object(100, 100)
x=True
while x:
	for x in pygame.event.get():
		if x.type == pygame.QUIT:
			x=False
	if x.type == pygame.KEYDOWN:
		if x.key == pygame.K_LEFT:
			hero_sprite.rect.move_ip(-2, 0)
			hero_sprite.x -= 2
		if x.key == pygame.K_RIGHT:
			hero_sprite.rect.move_ip(2, 0)
			hero_sprite.x += 2
		if x.key == pygame.K_UP:
			hero_sprite.rect.move_ip(0, -2)
			hero_sprite.y -= 2
		if x.key == pygame.K_DOWN:
			hero_sprite.rect.move_ip(0, 2)
			hero_sprite.y += 2
	Screen.fill(white)
	object_c.drawObject()
	hero_sprite.drawHero()
	pygame.display.update()


Was This Post Helpful? 0
  • +
  • -

#11 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3729
  • View blog
  • Posts: 13,022
  • Joined: 12-December 12

Re: Position of rect

Posted 04 February 2014 - 02:21 PM

project21124 said:

I'm teaching myself this language without one of those crappy tutorials, so it takes a little bit longer to find specific functions within pygame.


Are you teaching yourself Python, or pygame, without a tutorial? How?

This post has been edited by andrewsw: 04 February 2014 - 02:22 PM

Was This Post Helpful? 0
  • +
  • -

#12 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9487
  • View blog
  • Posts: 35,774
  • Joined: 12-June 08

Re: Position of rect

Posted 04 February 2014 - 02:22 PM

... like the dancing banana? Flailing one's arms around?! ;)
Was This Post Helpful? 2
  • +
  • -

#13 project21124  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 40
  • Joined: 02-February 14

Re: Position of rect

Posted 04 February 2014 - 02:46 PM

Haha modi!
Very funny...
I'm teaching myself Python, and pygame is merely the module that I'm using to enforce what I've learned.
How...hmm.
Well, I know the basic syntax of the language(Looked it up, of course), and I have some programming skills(not even close to intermediate, imo), so I just come up with things that can help my every day life, and try to make them. When I hit a wall, I fiddle, then fiddle some more, look it up, fiddle some more, and get it done.
Was This Post Helpful? 0
  • +
  • -

#14 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9487
  • View blog
  • Posts: 35,774
  • Joined: 12-June 08

Re: Position of rect

Posted 04 February 2014 - 02:48 PM

One would figure that would burn a lot of time and gears out.. I mean even the basic tutorials would be helpful to read through - if for anything then to get your bearings.. Typically books are great for that.. O'Reily being one that comes to mind.

http://docs.python.org/2/tutorial/
https://wiki.python....ide/Programmers
Was This Post Helpful? 0
  • +
  • -

#15 project21124  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 40
  • Joined: 02-February 14

Re: Position of rect

Posted 04 February 2014 - 03:24 PM

I'll check those out, but any tutorial that has a limiting scope(i.e., learning how to make an RPG game with Python), then I won't do it, because I'll lost interest, knowing that hundreds to thousands of people have done it before. That's why I would rather learn the fundamentals, and then make my OWN programs, because I'll be excited that I was able to make it by myself without any outside infringement.
I read through "Learn Python the Hard Way", but he spent like 15 exercises on printing, and like 2 on classes...
Thanks for the lit!
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3