So I have this mini particle effect that produces circles that move up. I want to make it look like smoke. I'm having a lot of problems though.
First off I want to make it recursive so that when the particle reaches the top it
returns to its original spot and begins again, what I have works a little but not exactly.
The other thing is I don't feel like it really looks like smoke can anyone suggest changes to make it look better?
Also I want to add this into my game, how would I make this callable for my game so I could just call particle and give it a location and it appears there. Can anyone help with any of this?
My Code
import pygame,random
from pygame.locals import *
xmax = 1000 #width of window
ymax = 600 #height of window
class Particle():
def __init__(self, x, y, dx, dy, col):
self.x = x
self.y = y
self.col = col
self.ry = y
self.rx = x
self.dx = dx
self.dy = dy
def move(self):
if self.y >= 10:
if self.dy < 0:
self.dy = -self.dy
self.ry -= self.dy
self.y = int(self.ry + 0.5)
self.dy -= .1
if self.y < 1:
self.y += 500
def main():
pygame.init()
screen = pygame.display.set_mode((xmax,ymax))
white = (255, 255, 255)
black = (0,0,0)
grey = (128,128,128)
particles = []
for part in range(25):
if part % 2 > 0: col = black
else: col = grey
particles.append( Particle(random.randint(500, 530), random.randint(0, 500), 0, 0, col))
exitflag = False
while not exitflag:
for event in pygame.event.get():
if event.type == QUIT:
exitflag = True
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
exitflag = True
screen.fill(white)
for p in particles:
p.move()
pygame.draw.circle(screen, p.col, (p.x, p.y), 8)
pygame.display.flip()
pygame.quit()
if __name__ == "__main__":
main()
I have made some major edits to your code. For starters, I cleaned up your class a great deal. Let's start with the arguments and the __init__ function.
First of all, instead of going down 500 to reset, particles go to the location that was set as their starting point. It's location that it starts out in is now chosen randomly in the __init__ function rather than in the game. I got rid of some of your unnecessary arguments as well.
In the move function of your class, I simplified quite a bit. In order for the particle to detect if it should reset, it simply sees if it's above 0. The going up is just a simple decrease of the y by 1. A change I added in is that the x changes randomly and goes to the right and left. This will make the smoke look a lot better / more realistic.
I didn't make many changes to the rest of your code. I changed your calling of the Particle class to fit the new arguments. I made a ton more particles, once again for visual effect. I also massively decreased the size of the circles drawn for (can you guess it?) visual effect. I added in a clock as well to keep the particles from going at supersonic speed.
Here is the final code. I hope you like it.
import pygame,random
from pygame.locals import *
xmax = 1000 #width of window
ymax = 600 #height of window
class Particle():
def __init__(self, startx, starty, col):
self.x = startx
self.y = random.randint(0, starty)
self.col = col
self.sx = startx
self.sy = starty
def move(self):
if self.y < 0:
self.x=self.sx
self.y=self.sy
else:
self.y-=1
self.x+=random.randint(-2, 2)
def main():
pygame.init()
screen = pygame.display.set_mode((xmax,ymax))
white = (255, 255, 255)
black = (0,0,0)
grey = (128,128,128)
clock=pygame.time.Clock()
particles = []
for part in range(300):
if part % 2 > 0: col = black
else: col = grey
particles.append( Particle(515, 500, col) )
exitflag = False
while not exitflag:
for event in pygame.event.get():
if event.type == QUIT:
exitflag = True
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
exitflag = True
screen.fill(white)
for p in particles:
p.move()
pygame.draw.circle(screen, p.col, (p.x, p.y), 2)
pygame.display.flip()
clock.tick(50)
pygame.quit()
if __name__ == "__main__":
main()
update
In order to add particles into your code, just do what you did in the code above. It works fine. If you wanted to do something to show the smoke starting, just put a pause time into your arguments and inhibit the movement of the smoke until that amount of time has passed. New class with that added in:
class Particle():
def __init__(self, startx, starty, col, pause):
self.x = startx
self.y = starty
self.col = col
self.sx = startx
self.sy = starty
self.pause = pause
def move(self):
if self.pause==0:
if self.y < 0:
self.x=self.sx
self.y=self.sy
else:
self.y-=1
self.x+=random.randint(-2, 2)
else:
self.pause-=1
The code you will need to create new particles:
for part in range(1, A):
if part % 2 > 0: col = black
else: col = grey
particles.append( Particle(515, B, col, round(B*part/A)) )
A and B are variables (I reccomend around 300 for A, B will be the Y value)
The new code will make particles spawn at the start location, and rise continously with no breaks. Hope you enjoy.
I have made a lot of changes to your code, especially in the Particle class.
Although, there are rather puzzling things in this code, it will be more flexible than your current code.
Here,
I have quite literately rewritten you Particle class.
Other than changing the __init__, to take many arguments (7 to be exact),
I have used trigonometry and the math module to move the particle, making it easier to manage (if you are good at math!). And I have also added bounce and draw methods to the Particle, and made the code more readable.
Just like #PygameNerd, I have added a clock, for restricting the max fps.
I haven't changed any event handling, but have used the bounce and draw funcs in the for p in particles: loop.
import pygame, random, math
def radians(degrees):
return degrees*math.pi/180
class Particle:
def __init__(self, (x, y), radius, speed, angle, colour, surface):
self.x = x
self.y = y
self.speed = speed
self.angle = angle
self.radius = 3
self.surface = surface
self.colour = colour
self.rect = pygame.draw.circle(surface,(255,255,0),
(int(round(x,0)),
int(round(y,0))),
self.radius)
def move(self):
""" Update speed and position based on speed, angle """
# for constant change in position values.
self.x += math.sin(self.angle) * self.speed
self.y -= math.cos(self.angle) * self.speed
# pygame.rect likes int arguments for x and y
self.rect.x = int(round(self.x))
self.rect.y = int(round(self.y))
def draw(self):
""" Draw the particle on screen"""
pygame.draw.circle(self.surface,self.colour,self.rect.center,self.radius)
def bounce(self):
""" Tests whether a particle has hit the boundary of the environment """
if self.x > self.surface.get_width() - self.radius: # right
self.x = 2*(self.surface.get_width() - self.radius) - self.x
self.angle = - self.angle
elif self.x < self.radius: # left
self.x = 2*self.radius - self.x
self.angle = - self.angle
if self.y > self.surface.get_height() - self.radius: # bottom
self.y = 2*(self.surface.get_height() - self.radius) - self.y
self.angle = math.pi - self.angle
elif self.y < self.radius: # top
self.y = 2*self.radius - self.y
self.angle = math.pi - self.angle
def main():
xmax = 640 #width of window
ymax = 480 #height of window
white = (255, 255, 255)
black = (0,0,0)
grey = (128,128,128)
pygame.init()
screen = pygame.display.set_mode((xmax,ymax))
clock = pygame.time.Clock()
particles = []
for i in range(1000):
if i % 2:
colour = black
else:
colour = grey
# for readability
x = random.randint(0, xmax)
y = random.randint(0, ymax)
speed = random.randint(0,20)*0.1
angle = random.randint(0,360)
radius = 3
particles.append( Particle((x, y), radius, speed, angle, colour, screen) )
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
break
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
done = True
break
if done:
break
screen.fill(white)
for p in particles:
p.move()
p.bounce()
p.draw()
clock.tick(40)
pygame.display.flip()
pygame.quit()
if __name__ == "__main__":
main()
Related
This is the image Okay so I am not able to figure out how to delete the previous render of the object every frame from screen so as to not leave a trail behind, in other words I want the object to have the appearance of Movement rather than leaving a bunch of red squares behind it. Image has been inserted for understanding what I mean by "leaving a trail" The following is my code:-
import pygame
import random
WIDTH = 1000
ROWS = 25
WIN = pygame.display.set_mode((WIDTH,WIDTH))
pygame.display.set_caption("Particle Physics")
clock = pygame.time.Clock()
RED = (255, 0, 0)
GRAVITY = 2
wid = 5
class Square:
def __init__(self,x,y,width,color,vel_x,vel_y):
self.x = x
self.y = y
self.width = width
self.color = color
self.velx = vel_x
self.vely = vel_y
def draw(self):
pygame.draw.rect(WIN, self.color, (self.x, self.y, self.width, self.width))
def move(self):
self.x += self.velx
self.y += self.vely
self.vely += GRAVITY[enter image description here][1]
def particles():
pass
def main():
run = True
a,b = 500,300
c = random.randint(-20,20)
d = random.randint(-50,0)
square = Square(a,b,wid, RED, c, d)
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
square.move()
square.draw()
pygame.display.update()
pygame.time.delay(100)
#clock.tick(60)
pygame.quit()
main()
In the main function add the set background color at the start so as to delete the previous frames.
def main()
...
while run
....
screen.fill((255,255,255))
...
so I am new to pygame/python/coding and first time using classes. I already made a working pygame program of a ball bouncing around and accelerating with gravity. However, now that I use classes, the ball only seems to be moving at a constant speed. Please tell me what I am doing wrong and how
I should change my code. Thanks in advance!
import pygame, sys
pygame.init()
red = (255,0,0)
black = (0,0,0)
white = (255,255,255)
blue = (0,0,255)
pygame.mouse.set_visible(0)
clock = pygame.time.Clock()
displaySize = (800,600)
screen = pygame.display.set_mode(displaySize)
g = 30
dt = 0.05
class ball:
def __init__(self, x, y, vx, vy, r,ax,ay, color):
dt = 0.05
Cd = 0.01
m = 5
self.ay = ay
self.ax = ax
self.x = x
self.y = y
self.r = r
self.color = color
self.vx = vx
self.vy = vx
def update(self, x, y):
x, y = physics()
pygame.draw.circle(screen, self.color, (int(round(x)),int(round(y))), self.r)
pygame.display.update()
def physics(self):
self.Dy = Cd*self.vy*abs(self.vy)
self.Dx = Cd*self.vx*abs(self.vx)
self.Fy = self.m*g - self.Dy
self.Fx = -self.Dx
self.ay = self.Fy/m
self.ax = self.Fx/m
self.vy += self.ay*dt
self.vx += self.ax*dt
self.x +=self.vx*dt
self.y +=self.vy*dt
return self.x, self.y
one = ball(100,100,0,0,0,0,30,red)
while 1:
clock.tick(30)
screen.fill(blue)
one.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
self.vx and self.vy should be within the constructor method instead of the physics method, otherwise their values will be set back to 50 and 0 each time the physics method is called within the game loop, resulting in a constant speed.
I'm doing the Pong game. However instead of 2 players, I'm doing it for 4 (one on each side of the screen). I need to have the ball randomly "choose" the direction it has to go.
import pygame
import random
class Ball(object):
#classmethod
def init(cls, SCREEN_WIDTH, SCREEN_HEIGHT):
cls.radius = 20
cls.centerx = SCREEN_WIDTH*0.5
cls.centery = SCREEN_HEIGHT*0.5
cls.rect = pygame.Rect(cls.centerx - cls.radius,
cls.centery - cls.radius,
cls.radius * 2,
cls.radius * 2)
# x , y
cls.direction = [random.choice([1, -1]), random.choice([1, -1])]
cls.speed = [5, 8] # x, y
# left, right, top, bottom
cls.hit_edge = [False, False, False, False]
#classmethod
def update(cls, player1, player2, player3, player4, SCREEN_WIDTH,
SCREEN_HEIGHT):
cls.centerx += cls.direction[0] * cls.speed[0]
cls.centery += cls.direction[1] * cls.speed[1]
cls.rect.center = (cls.centerx, cls.centery)
#detects if someone losses
if cls.rect.left <= 0:
cls.hit_edge[0] = True
elif cls.rect.right >= SCREEN_WIDTH-1:
cls.hit_edge[1] = True
elif cls.rect.top <= 0:
cls.hit_edge[2] = True
elif cls.rect.bottom >= SCREEN_HEIGHT-1:
cls.hit_edge[3] = True
#detects collision between players & the ball
if cls.rect.colliderect(player1.rect):
cls.direction[0] = 1
cls.up_speed()
elif cls.rect.colliderect(player2.rect):
cls.direction[0] = -1
cls.up_speed()
elif cls.rect.colliderect(player3.rect):
cls.direction[1] = 1
cls.up_speed()
elif cls.rect.colliderect(player4.rect):
cls.direction[1] = -1
cls.up_speed()
#classmethod
def up_speed(cls):
cls.speed[0] += random.uniform(0, 0.25)
cls.speed[1] += random.uniform(0, 0.25)
#classmethod
def render(cls, SCREEN, color):
pygame.draw.circle(SCREEN, color, cls.rect.center, cls.radius, 0)
To take into account: I had the idea to add a "0" in every random.choice(), although if I do this only function at the beginning, then it will not be able to move in the axis where the "0" . Also I have two types of speeds in X and Y, could be solved by putting a "0.1" in random.choice () but this would make when the game starts the ball goes very slow. As you would do for the ball to start in a random direction (taking into account that the speed of the ball at the start must be the same for all players. If the ball goes at the beginning to the left,and later (in another game) when it starts but the ball goes up has to go at the same speed)
This may be a little over-complicating things, but if you know the speed you want the ball to start with overall, you could use something like this:
Generate random number between 0-1
angle = 360 * random number
xSpeed = startSpeed * sin(angle)
ySpeed = startSpeed * cos(angle)
This will mean that your ball will always travel at the same speed. The only thing that is random is the direction it travels in.
I recommend to use vectors. For the velocity you can just pick an arbitrary start speed like (8, 0) and then rotate the vector by a random angle.
position = pg.math.Vector2(100, 200)
velocity = pg.math.Vector2(8, 0).rotate(random.randrange(360))
To update the position:
position += velocity
Here's an example program that spawns balls with random color and velocity.
import sys
import math
from random import randrange
import pygame as pg
class Ball(pg.sprite.Sprite):
def __init__(self, pos, *groups):
super().__init__(groups)
self.image = pg.Surface((30, 30), pg.SRCALPHA)
col = randrange(256), randrange(256), randrange(256)
pg.draw.circle(self.image, col, (15, 15), 15)
self.rect = self.image.get_rect(center=pos)
self.vel = pg.math.Vector2(8, 0).rotate(randrange(360))
self.pos = pg.math.Vector2(pos)
def update(self):
self.pos += self.vel
self.rect.center = self.pos
if self.rect.left < 0 or self.rect.right > 640:
self.vel.x *= -1
if self.rect.top < 0 or self.rect.bottom > 480:
self.vel.y *= -1
def main():
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
sprite_group = pg.sprite.Group()
ball = Ball((320, 240), sprite_group)
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
elif event.type == pg.MOUSEBUTTONDOWN:
sprite_group.add(Ball((320, 240)))
sprite_group.update()
screen.fill((30, 30, 30))
sprite_group.draw(screen)
pg.display.flip()
clock.tick(30)
if __name__ == '__main__':
pg.init()
main()
pg.quit()
sys.exit()
I'm trying to apply OOP in this bouncing ball code in order to have multiple balls bouncing around the screen. The objective of this code is to create a method called settings, which controls all the settings for the screen and the bouncing behaviour of multiple balls, under the class Ball. However, I keep on getting errors related to attributes (e.g., speed). I'm new to OOP in Python so your help will be much appreciated. Thanks in advance.
import pygame
import random
import sys
pygame.init()
class Ball:
def __init__(self, X, Y):
self.velocity = [1,1]
self.ball_image = pygame.image.load ("ball.jpg")
self.ball_boundary = self.ball_image.get_rect ()
self.black = [0,0,0]
self.width = 800
self.height = 600
self.num = 8
self.X = random.randint(0, self.width)
self.Y = random.randint(0, self.height)
def settings(self):
#X = random.randint(0, self.width)
#Y = random.randint(0, self.height)
clock = pygame.time.Clock()
size = self.width, self.height
screen = pygame.display.set_mode(size)
ball_boundary = self.ball_image.get_rect()
speed = self.velocity
pic = self.ball_image
pygame.display.set_caption("Balls")
num_balls = self.num
ball_list = []
for i in range(num_balls):
ball_list.append( Ball(random.randint(10, self.width-10),random.randint(10, self.height-10)) )
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit(0)
screen.fill(self.black)
for balls in ball_list:
if balls.ball_boundary.left < 0 or balls.ball_boundary.right > self.width:
balls.speed[0] = -balls.speed[0]
if balls.ball_boundary.top < 0 or balls.ball_boundary.bottom > self.height:
balls.speed[1] = -balls.speed[1]
balls.ball_boundary = balls.ball_boundary.move (self.velocity)
screen.blit (balls.ball_image, balls.ball_boundary)
pygame.display.flip()
play = Ball(random.randint(0, 800), random.randint(0, 600))
play.settings()
I'm writing a little pirate game in Pygame. If you played sea battles in Empires Total War, you have an idea of what I would like to achieve:
The ship's sprite is at position (x1|y1). The player now clicks at position (x2|y2) on the screen. The sprite is now supposed to take (x2|y2) as its new position - by going there step by step, not by beaming there instantly.
I figured out that it has something to do with the diagonal of the rectangle (x1|y1),(x1|y2),(x2|y2),(x2|y1) but I just can't figure it out, especially not with keeping the speed the same no matter what angle that diagonal has and considering that the x and y values of either (ship or click) might be bigger or smaller than the respective other.
This little snippet is my last try to write a working function:
def update(self, new_x, new_y, speed, screen, clicked):
if clicked:
self.xshift = (self.x - new_x)
self.yshift = ((self.y - new_y) / (self.x - new_x))
if self.x > (new_x + 10):
self.x -= 1
self.y -= self.yshift
elif self.x > new_x and self.x < (new_x + 10):
self.x -= 1
self.y -= self.yshift
elif self.x < (new_x - 10):
self.x += 1
self.y += self.yshift
elif self.x < new_x and self.x < (new_x - 10):
self.x += 1
self.y += self.yshift
else:
self.x += 0
self.y += 0
screen.set_at((self.x, self.y), (255, 0, 255))
The "ship" is just a pink pixel here. The reaction it shows upon my clicks onto the screen is to move roughly towards my click but to stop at a seemingly random distance of the point I clicked.
The variables are:
new_x, new_y = position of mouseclick
speed = constant speed depending on ship types
clicked = set true by the MOUSEBUTTONDOWN event, to ensure that the xshift and yshift of self are only defined when the player clicked and not each frame again.
How can I make the ship move smoothly from its current position to the point the player clicked?
Say the current position is pos, and the point the player clicked is target_pos, then take the vector between pos and target_pos.
Now you know how to get from pos to target_pos, but to move in constant speed (and not the entire distance at once), you have to normalize the vector, and apply a speed constant by scalar multiplication.
That's it.
Complete example: (the relevant code is in the Ship.update method)
import pygame
class Ship(pygame.sprite.Sprite):
def __init__(self, speed, color):
super().__init__()
self.image = pygame.Surface((10, 10))
self.image.set_colorkey((12,34,56))
self.image.fill((12,34,56))
pygame.draw.circle(self.image, color, (5, 5), 3)
self.rect = self.image.get_rect()
self.pos = pygame.Vector2(0, 0)
self.set_target((0, 0))
self.speed = speed
def set_target(self, pos):
self.target = pygame.Vector2(pos)
def update(self):
move = self.target - self.pos
move_length = move.length()
if move_length < self.speed:
self.pos = self.target
elif move_length != 0:
move.normalize_ip()
move = move * self.speed
self.pos += move
self.rect.topleft = list(int(v) for v in self.pos)
def main():
pygame.init()
quit = False
screen = pygame.display.set_mode((300, 300))
clock = pygame.time.Clock()
group = pygame.sprite.Group(
Ship(1.5, pygame.Color('white')),
Ship(3.0, pygame.Color('orange')),
Ship(4.5, pygame.Color('dodgerblue')))
while not quit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
if event.type == pygame.MOUSEBUTTONDOWN:
for ship in group.sprites():
ship.set_target(pygame.mouse.get_pos())
group.update()
screen.fill((20, 20, 20))
group.draw(screen)
pygame.display.flip()
clock.tick(60)
if __name__ == '__main__':
main()