yangnj
yangnj

Reputation: 67

Sprite with gravity does not move with gravity

I'm working on a game and I need to make enemies that move to the right or left (the direction is chosen randomly) and bounce up and down. I've set it up and it mostly works, but the gravity is weird. Instead of bouncing up and down smoothly, its speed up and down suddenly changes. Here's the code:

class Mob(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image=pygame.Surface((30,40))
        self.image.fill(RED)
        self.rect = self.image.get_rect()
        self.direct = random.randrange(2)
        if self.direct == 0:
            self.rect.x = -20.0
        else:
            self.rect.x = 500.0
        self.rect.y = 180.0
        self.speedy = 0.0

    def update(self):
        if self.rect.y >= 275:
            self.speedy = -3.0
        else:
            self.speedy += 0.02

        if self.direct == 0:
            self.rect.x += 1.0
            if self.rect.x > width + 20:
                self.rect.x = -20
        else:
            self.rect.x -= 1.0
            if self.rect.x > width - 500:
                self.rect.x = 500

        self.rect.y = self.rect.y + self.speedy

all_sprites = pygame.sprite.Group()
mobs = pygame.sprite.Group()
for i in range(2):
    m = Mob()
    all_sprites.add(m)
    mobs.add(m)

The code that draws the enemies is this:

all_sprites.update()
all_sprites.draw(screen)

pygame.display.flip()

The enemy is currently just a red rectangle, but I am going to replace it with something else. It is hard to explain exactly what the problem is if you don't try it yourself. you can try running this:

import pygame
import math
import random
from pygame.locals import *

RED = (255,0,0)
FPS = 120

class Mob(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image=pygame.Surface((30,40))
        self.image.fill(RED)
        self.rect = self.image.get_rect()
        self.direct = random.randrange(2)
        if self.direct == 0:
            self.rect.x = -20.0
        else:
            self.rect.x = 500.0
        self.rect.y = 180.0
        self.speedy = 0.0

    def update(self):
        if self.rect.y >= 275:
            self.speedy = -3.0
        else:
            self.speedy += 0.02

        if self.direct == 0:
            self.rect.x += 1.0
            if self.rect.x > width + 20:
                self.rect.x = -20
        else:
            self.rect.x -= 1.0
            if self.rect.x > width - 500:
                self.rect.x = 500

        self.rect.y = self.rect.y + self.speedy

all_sprites = pygame.sprite.Group()
mobs = pygame.sprite.Group()
for i in range(2):
    m = Mob()
    all_sprites.add(m)
    mobs.add(m)

while True:
    clock.tick(FPS)
    all_sprites.update()
    all_sprites.draw(screen)

    pygame.display.flip()

If that bit of code doesn't work then sorry, I am still new to python and pygame.

Upvotes: 1

Views: 47

Answers (1)

skrx
skrx

Reputation: 20438

Your gravity is too low. Try a higher value like 0.08.

if self.rect.y >= 275:
    self.speedy = -3.0
else:
    self.speedy += 0.08

Upvotes: 2

Related Questions