Reputation: 1
I have no clue how to add a gravity mechanism to my square. Here is the code:
import pygame, sys
from colors import *
from random import randint
import particles
pygame.init()
#background = pygame.image.load("graphics//background.jpg")
#Background = pygame.Surface(background.get_size(), pygame.HWSURFACE)
#Background.blit(background, (0, 0))
grav_x = 2
grav_y = 2
global window, window_height, window_width, window_title
window_width, window_height = 800, 600
window_title = "particle game"
#title_icon = "graphics//icons//icon_title.jpg"
pygame.display.set_caption(window_title)
window = pygame.display.set_mode((window_width, window_height),
pygame.HWSURFACE|pygame.DOUBLEBUF)
particle_size = 40
#class Particle(object):
#def __init__(self, Color, xpos, ypos):
#pygame.draw.rect(window, Color, pygame.Rect(xpos, ypos, particle_size, particle_size))
class Hydrogen:
def __init__(self, Color, xpos, ypos):
#Particle.__init__(self, Color, xpos, ypos)
pygame.draw.rect(window, Color, pygame.Rect(xpos, ypos, particle_size, particle_size))
hydrogen = Hydrogen
isRunning = True
window.fill(Color.LightGray)
while isRunning:
pygame.display.update()
mx, my = pygame.mouse.get_pos()
mx -= 20
my -= 20
for event in pygame.event.get():
if event.type == pygame.QUIT:
isRunning = False
elif event.type == pygame.MOUSEBUTTONDOWN:
hydrogen(Color.Purple, mx, my)
pygame.quit()
sys.exit()
The grav_x and grav_y are my measly attempts to create gravity.
I have tried adding gravity to my class, but it never works out, and I'm stuck brain dead.
If anyone has any ideas please tell me.
Upvotes: 0
Views: 298
Reputation: 4416
Here:
import pygame, sys
from random import randint
pygame.init()
#background = pygame.image.load("graphics//background.jpg")
#Background = pygame.Surface(background.get_size(), pygame.HWSURFACE)
#Background.blit(background, (0, 0))
grav_x = 2
grav_y = 2
global window, window_height, window_width, window_title
window_width, window_height = 800, 600
window_title = "particle game"
#title_icon = "graphics//icons//icon_title.jpg"
pygame.display.set_caption(window_title)
window = pygame.display.set_mode((window_width, window_height),
pygame.HWSURFACE|pygame.DOUBLEBUF)
particle_size = 40
#class Particle(object):
#def __init__(self, Color, xpos, ypos):
#pygame.draw.rect(window, Color, pygame.Rect(xpos, ypos, particle_size, particle_size))
class Hydrogen:
def __init__(self, Color, xpos, ypos, size):
#Particle.__init__(self, Color, xpos, ypos)
self.color = Color
self.position = [xpos, ypos]
self.size = size
self.velocity = [0, 0]
def draw(self):
pygame.draw.rect(window, self.color, (self.position[0], self.position[1], self.size, self.size))
def move(self):
self.position[0] += self.velocity[0]
self.position[1] += self.velocity[1]
print(self.velocity)
print(self.position)
def gravity(self):
self.velocity[1] += 0.1
def floor_collide(self):
if self.position[1] + self.size > window_height:
self.position[1] = window_height - self.size
self.velocity[1] = -0.5 * self.velocity[1]
hydrogen = Hydrogen((255, 255, 0), 50, 50, particle_size)
isRunning = True
clock = pygame.time.Clock()
while isRunning:
mx, my = pygame.mouse.get_pos()
mx -= 20
my -= 20
for event in pygame.event.get():
if event.type == pygame.QUIT:
isRunning = False
hydrogen.gravity()
hydrogen.floor_collide()
hydrogen.move()
window.fill((150, 150, 150))
hydrogen.draw()
pygame.display.update()
clock.tick(60)
pygame.quit()
sys.exit()
Some of it seems redundant now I have changed it, but I left it in. Also, since I did not have settings.py
or the colors file I removed those, so you'll have to re-add them.
Upvotes: 1
Reputation: 443
In general, what you do too implement gravity is to accelerate each particle downward on each update. I don't know pygame, but it looks like your particles don't move at all, so you should add the idea of motion and changing the position of particles repeatedly. I'd suggest setting a constant downward velocity first that is, subtract a constant from the y (or add, if the origin is at the top) and get that going. Then, gravity becomes multiplying the y velocity by 1.1 (for instance, play with it!) on each update.
Upvotes: 0