Python Forum
drawing, moving, and collision problems (pygame)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
drawing, moving, and collision problems (pygame)
#16
It is really time consuming reading your code. And thus its hard to spot the problem. For example there should be no self.x or self.y if you are using rects. Pygame rects contain that data. You should be using rects for positioning and collision. You should never have to have a list such as Pos.

I also have no idea what this is suppose to be.
Quote:
    def getWeapRect(self):
        if self.weaponPos != None:
            if self.direct == 'Left':
                return self.weaponPos[0], self.weaponPos[1], self.weaponPos[0] + 50, self.weaponPos[1] + 20
            if self.direct == 'Right':
                return self.weaponPos[0], self.weaponPos[1], self.weaponPos[0] + 50, self.weaponPos[1] + 20
            if self.direct == 'Up':
                return self.weaponPos[0], self.weaponPos[1], self.weaponPos[0] + 20, self.weaponPos[1] + 50
            if self.direct == 'Down':
                return self.weaponPos[0], self.weaponPos[1], self.weaponPos[0] + 20, self.weaponPos[1] + 50
Any pygame image can return a rect of its size. You can do this with the charactes as well as the weapons.

image = pygame.image.load(IMAGE_NAME).convert()
image_rect = image.get_rect()
and can draw that too

screen.blit(image, image_rect)
and of course collision:
if image_rect.colliderect(enemy.weapon.rect)
Because you have assigned the rect yourself, I am assuming you messed the math up somewhere. That is why pygame has these methods. To simplify it. I would suggest you reread different pygame tutorials other than sentdex because his are pretty bad.

As a side note:
Quote:
        if self.x >= 0 and self.x  + self.width <= width and self.y >= 0 and self.y + self.height <= height:
            if keyed[pygame.K_LEFT] or keyed[pygame.K_a]:
                self.x -= 1
                self.direct = 'Left'
            if keyed[pygame.K_RIGHT] or keyed[pygame.K_d]:
                self.x += 1
                self.direct = 'Right'
            if keyed[pygame.K_UP] or keyed[pygame.K_w]:
                self.y -= 1
                self.direct = 'Up'
            if keyed[pygame.K_DOWN] or keyed[pygame.K_s]:
                self.y += 1
                self.direct = 'Down'
You can do this with the screen itself too

screen = display.set_mode((800,600))
screen_rect = screen.get_rect()
to check collision with the side of the screen and make adjustments.

also you do not need to load the same image more than once. Its a waste of resources and redundant coding. Check here for a title of rotation.
Recommended Tutorials:
Reply


Messages In This Thread
RE: Drawing player and enemy (pygame) - by Windspar - Apr-14-2019, 10:25 AM
RE: drawing, moving, and collision problems (pygame) - by metulburr - Apr-17-2019, 12:21 PM
pygame problems - by SheeppOSU - Apr-18-2019, 11:27 PM
pygame problems - by SheeppOSU - Apr-19-2019, 11:59 PM
changing position errors - by SheeppOSU - Apr-22-2019, 03:09 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question [PyGame] Problem with collision of player and enemy Nekotrooper 1 971 Dec-08-2023, 03:29 PM
Last Post: deanhystad
  pygame installation problems Gheryk 5 8,930 Nov-29-2023, 08:49 PM
Last Post: E_Mohamed
  can't get collision detection to work in platform game chairmanme0wme0w 10 4,317 Aug-19-2022, 03:51 PM
Last Post: deanhystad
  [PyGame] Problems with jump code in pygame Joningstone 4 5,587 Aug-23-2021, 08:23 PM
Last Post: deanhystad
  [PyGame] drawing images onto pygame window djwilson0495 1 3,640 Feb-22-2021, 05:39 PM
Last Post: nilamo
  [PyGame] Collision in not happening onizuka 3 3,587 Sep-07-2020, 11:30 AM
Last Post: metulburr
  [PyGame] No collision detection onizuka 6 3,895 Aug-18-2020, 01:29 PM
Last Post: onizuka
  [PyGame] pygame, help with making a function to detect collision between player and enemy. Kris1996 3 3,496 Mar-07-2020, 12:32 PM
Last Post: Kris1996
  Problem with collision detection... michael1789 4 3,493 Nov-12-2019, 07:49 PM
Last Post: michael1789
  Pygame sprite not moving michael1789 1 2,966 Nov-10-2019, 03:54 AM
Last Post: michael1789

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020