Using Pygame and Pymunk Circle will not spawn in space - python

So, I'm trying to make a function create_particle then make the function draw a partial with draw_circle. However, whenever I open the window, I get my grey window but no particle is shown. I'm extremely new to both pygame and pymunk so any help is appreciated.
import sys, pygame, random, pymunk
BG = (94, 93, 93)
S_width = 800
S_height = 800
pygame.init()
Window = pygame.display.set_mode((S_width,S_height))
clock = pygame.time.Clock()
pygame.display.set_caption("H20 Particle simulation")
Window.fill(BG)
space = pymunk.Space()
space.gravity = (0,100)
def create_particle(space):
body = pymunk.Body(1, 100, body_type = pymunk.Body.DYNAMIC)
body.position = (400, 400)
shape = pymunk.Circle(body,80)
space.add(body, shape)
return shape
def draw_circle(circle):
for circle in circles:
pos_x = int(circle.body.position.x)
pos_y = int(circle.body.position.y)
pygame.draw.circle(screen,(0,0,0),circle.body.position20)
circles = []
circles.append(create_particle(space))
while True:
Window.fill((217,217,217))
clock.tick(120)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()

A few changes are needed:
draw_circle() does not require a parameter
When you draw the circle, you need to specify the coordinates and radius
In the main loop, call draw_circle() and space.step(0.02)
Here is the updated code:
def draw_circle():
for circle in circles:
pos_x = int(circle.body.position.x)
pos_y = int(circle.body.position.y)
pygame.draw.circle(Window,(0,200,0), (pos_x, pos_y), 20)
circles = []
circles.append(create_particle(space))
while True:
Window.fill((217,217,217))
draw_circle()
space.step(0.02)
clock.tick(120)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
pygame.display.update()

Related

How do I make this pymunk code work? It shows a black screen

I am new to stackoverflow.
My code has a problem which the screen turns completely black.
The error.
I need the answer quickly so any help will be good.
Heres the code:
import pygame, sys
import pymunk
import pymunk.pygame_util
from pymunk.vec2d import Vec2d
size = (800, 800)
FPS = 120
space = pymunk.Space()
space.gravity = (0,250)
pygame.init()
screen = pygame.display.set_mode(size)
clock = pygame.time.Clock()
class Ball:
global space
def __init__(self, pos):
self.body = pymunk.Body(1,1, body_type = pymunk.Body.DYNAMIC)
self.body.position = pos
self.radius = 60
self.shape = pymunk.Circle(self.body, self.radius)
space.add(self.body, self.shape)
def draw(self):
x = int(self.body.position.x)
y = int(self.body.position.y)
pygame.draw.circle(screen, (255,0,0), (x,y), self.radius)
balls = []
balls.append(Ball((400,0)))
balls.append(Ball((100,0)))
balls.append(Ball((600,100)))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screeen.fill(217,217,217)
for ball in balls:
ball.draw()
space.step(1/50)
pygame.display.update()
clock.tick(FPS)
Any help on what to do???
Thanks.
screeen.fill(217,217,217) should be screen.fill((217,217,217))
The Indentation is not correct. In particular, the scene must be pulled in the application loop and not in the bleed loop:
import pygame, sys
import pymunk
import pymunk.pygame_util
from pymunk.vec2d import Vec2d
size = (800, 800)
FPS = 120
space = pymunk.Space()
space.gravity = (0,250)
pygame.init()
screen = pygame.display.set_mode(size)
clock = pygame.time.Clock()
class Ball:
global space
def __init__(self, pos):
self.body = pymunk.Body(1,1, body_type = pymunk.Body.DYNAMIC)
self.body.position = pos
self.radius = 60
self.shape = pymunk.Circle(self.body, self.radius)
space.add(self.body, self.shape)
# INDENTATION
#<--|
def draw(self):
x = int(self.body.position.x)
y = int(self.body.position.y)
pygame.draw.circle(screen, (255,0,0), (x,y), self.radius)
balls = []
balls.append(Ball((400,0)))
balls.append(Ball((100,0)))
balls.append(Ball((600,100)))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# INDENTATION
#<------|
screen.fill((217,217,217))
for ball in balls:
ball.draw()
space.step(1/50)
pygame.display.update()
clock.tick(FPS)

Pygame: Efficient way to call draw function in for loops [duplicate]

I was coded a simple pygame grid window. but pygame window start lag after that.
Here is that simple code👇
import pygame
import random
pygame.init()
pygame.font.init()
screen_width = 500
screen_height = screen_width
screen = pygame.display.set_mode((screen_width,screen_height))
pygame.display.set_caption("Snake GaMe By Akila")
def drawGrid():
grid_list = []
blockSize = 25
for x in range(screen_width):
for y in range(screen_height):
rect = pygame.Rect(x*blockSize, y*blockSize, blockSize, blockSize)
pygame.draw.rect(screen, (255,255,255), rect, 1)
running = True
while running:
screen.fill((0,0,0))
drawGrid()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.display.update()
I was tried changing the call snippet of drawGrid() function position.
To improve the performance, do not construct the grind in every frame.
Create a pygame.Surface with the size of the window and draw the grid on this surface:
grid_surf = pygame.Surface((screen_width,screen_height))
drawGrid(grid_surf)
This surface is the back ground for your scene. blit it at the begin of every frame:
screen.blit(grid_surf, (0, 0))
Example code:
import pygame
import random
pygame.init()
pygame.font.init()
screen_width = 500
screen_height = screen_width
screen = pygame.display.set_mode((screen_width,screen_height))
pygame.display.set_caption("Snake GaMe By Akila")
def drawGrid(surf):
grid_list = []
blockSize = 25
for x in range(screen_width):
for y in range(screen_height):
rect = pygame.Rect(x*blockSize, y*blockSize, blockSize, blockSize)
pygame.draw.rect(surf, (255,255,255), rect, 1)
grid_surf = pygame.Surface((screen_width,screen_height))
drawGrid(grid_surf)
running = True
while running:
screen.blit(grid_surf, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.display.update()

cs50 minesweeper algorithm sometimes stuck in forever loop [duplicate]

I was coded a simple pygame grid window. but pygame window start lag after that.
Here is that simple code👇
import pygame
import random
pygame.init()
pygame.font.init()
screen_width = 500
screen_height = screen_width
screen = pygame.display.set_mode((screen_width,screen_height))
pygame.display.set_caption("Snake GaMe By Akila")
def drawGrid():
grid_list = []
blockSize = 25
for x in range(screen_width):
for y in range(screen_height):
rect = pygame.Rect(x*blockSize, y*blockSize, blockSize, blockSize)
pygame.draw.rect(screen, (255,255,255), rect, 1)
running = True
while running:
screen.fill((0,0,0))
drawGrid()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.display.update()
I was tried changing the call snippet of drawGrid() function position.
To improve the performance, do not construct the grind in every frame.
Create a pygame.Surface with the size of the window and draw the grid on this surface:
grid_surf = pygame.Surface((screen_width,screen_height))
drawGrid(grid_surf)
This surface is the back ground for your scene. blit it at the begin of every frame:
screen.blit(grid_surf, (0, 0))
Example code:
import pygame
import random
pygame.init()
pygame.font.init()
screen_width = 500
screen_height = screen_width
screen = pygame.display.set_mode((screen_width,screen_height))
pygame.display.set_caption("Snake GaMe By Akila")
def drawGrid(surf):
grid_list = []
blockSize = 25
for x in range(screen_width):
for y in range(screen_height):
rect = pygame.Rect(x*blockSize, y*blockSize, blockSize, blockSize)
pygame.draw.rect(surf, (255,255,255), rect, 1)
grid_surf = pygame.Surface((screen_width,screen_height))
drawGrid(grid_surf)
running = True
while running:
screen.blit(grid_surf, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.display.update()

How to generate actually, draw a several walls(10-15) on randomly position at the beginning of the game with pygame

strong text
I try to do It but have some problems. And also need to know how to at the "randomly time"(This is important) again generate, draw circles like food? I do tank game, with classes. So can someone help?
import random
import pygame
pygame.init()
window = pygame.display.set_mode((800, 600))
screen = pygame.Surface((window.get_size()))
bg = pygame.image.load('###')
class Wall():
def __init__(self, x_wall, y_wall, width_wall, height_wall):
self.x_wall = x_wall # random.randint(50, 750)
self.y_wall = y_wall # random.randint(50, 550)
self.width_wall = width_wall
self.height_wall = height_wall
def draw(self, screen):
pygame.draw.rect(screen, (3, 3, 3), (self.x_wall, self.y_wall, self.width_wall,
self.height_wall))
while game:
mills = clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
game = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
game = False
screen.blit(bg, (0, 0))
window.blit(screen, (0, 0))
pygame.display.flip()
Create a list containing your random walls before your while loop:
walls=[]
for i in range(15):
w=Wall(random.randint(50, 750), random.randint(50, 750), 100, 200)
walls.append(w)
Then draw all the walls in every step of the game loop:
while game:
#....
for w in walls:
w.draw()
pygame.display.flip()
Concerning having food appear at "random time", this can be done in many ways. An easy way would be to import time and create a variable last_food and a random variable secs_until_food and then inside the game loop do:
now = time.time() #current time in seconds
if now-last_food > secs_until_food:
#Create food object
#...
last_food = now
secs_until_food = random.randint(10,20)

How to draw a square wherever you click in the pygame window

You probably know what I want to do from the title but here's a simple example:
#User clicks somewhere in the pygame window
pos = cursorPosition()
#Function/Class that creates a square where the user clicked.
I have tried this:
import pygame
import sys
running = True
pygame.init()
screen = pygame.display.set_mode((800, 500))
pos = pygame.mouse.get_pos()
class Create():
cx, cy = pygame.mouse.get_pos()
square = pygame.Rect(cx, cy, 50, 50)
def cube(self):
self.square = pygame.Rect(self.cx, self.cy, 50, 50)
pygame.draw.rect(screen, (255, 0, 0), self.square)
pygame.display.flip()
create = Create()
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
create.cube()
screen.fill((0, 255, 0))
pygame.display.flip()
pygame.quit()
sys.exit()
But it just gives me a Blue screen and when I click anywhere it doesn't do anything, so if you can help me it would be much appreciated. Thanks!
EDIT: I've managed to do it but now I face another problem:
The square would appear only if I hold down the mouse button.
This is the code
import pygame
import sys
running = True
pygame.init()
screen = pygame.display.set_mode((800, 500))
pos = pygame.mouse.get_pos()
class Create():
cx, cy = pygame.mouse.get_pos()
square = pygame.Rect(cx, cy, 50, 50)
def cube(self):
self.cx, self.cy = pygame.mouse.get_pos()
self.square = pygame.Rect(self.cx, self.cy, 50, 50)
pygame.draw.rect(screen, (255, 0, 0), self.square)
pygame.display.flip()
create = Create()
while running:
screen.fill((0, 255, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
create.cube()
pygame.display.flip()
pygame.quit()
sys.exit()
It's great to see that you are making a good effort to solve the by yourself, and that you are editing the question with the work you've done so far!
What I added to the code basically allows the user to change where the cube is being drawn. If you would like to draw multiple cubes, with their positions based on the mouse clicks from the user, edit your question to add those details, and leave a comment below.
First, I wrote a new class called Cube, which essentially has the same code as Create. I won't go into it in detail, but generally in object-oriented programming, objects are nouns, and their methods are actions. Your class is the opposite, which isn't how object-oriented code is generally written.
I added the update() method which simply updates some of the object's fields with the position of the mouse. Your original code was defining class fields or static variables. I won't go into to too much detail here, but if we were to make 100 instances of cube, we would want to have the positions for all the cubes, right? In cases like these, you are operating on the objects, not the class.
Then, there is one variable which gets set to true after the first mouse click, and, as a result, the cube starts being drawn to the screen.
Here is the fixed code:
import pygame
running = True
pygame.init()
screen = pygame.display.set_mode((800, 500))
class Cube:
def update(self):
self.cx, self.cy = pygame.mouse.get_pos()
self.square = pygame.Rect(self.cx, self.cy, 50, 50)
def draw(self):
pygame.draw.rect(screen, (255, 0, 0), self.square)
cube = Cube()
drawing_cube = False
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
cube.update()
drawing_cube = True
screen.fill((0, 255, 0))
if drawing_cube:
cube.draw()
pygame.display.flip()
pygame.quit()
quit()
I hope this answer helped you, and if you have any further questions, please feel free to leave a comment below!
I have also come across this problem and after a lot of messing around and frustration came up with the following:
import sys
import pygame
import time
pygame.init()
white = (255, 255, 255)
red = (255, 0, 0)
size = (700, 500)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("OwO")
mouse_cords = []
clicked = False
def draw(cord_list, zero):
''' This is what will draw on the screen when the mouse clicks using recursion'''
pygame.draw.circle(screen, red, cord_list[zero], 100, 1)
zero += 1
if len(cord_list) > zero:
draw(cord_list, zero)
done = False
clock = pygame.time.Clock()
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
screen.fill(white)
if pygame.mouse.get_pressed()[0]:
mouse_cords.append(pygame.mouse.get_pos())
clicked = True
if clicked == True:
draw(mouse_cords, 0)
pygame.display.flip()
clock.tick(60)
pygame.quit()
As you can tell I used recursion to solve this problem. Sorry it's circles not boxes but Im sure you can figure out how to change that.
I am a beginner developer and my method is probably not the most efficient, however it does work. I know that I am writing 2 years late, but I am writing this incase anyone has the same question as I did which is to create multiple rectangles, squares or circles with a mousebutton press or in general any event.
I implemented a class to create a Cube object just like Michael O'Dwyer wrote in his explanation, and while his solution works to create one cube at a time, I needed to create multiple for myself.
In addition to his solution, I just created a list "cubeList" and with each mousebutton event I created a new cube object and appended it to that cubeList. Then, I used a for loop in order to iterate through each cube object in the cubeList and used the draw method. This way, I am able to have multiple cubes drawn at the same time, and I am sure there might be a more efficient or better method to do this, but this worked for me.
import pygame
WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()
WHITE = (255,255,255)
RED = (255,0,0)
GREEN = (0,255,0)
BLUE = (0,0,255)
Square1 = pygame.Rect(30,30,60,60)
Square2 = pygame.Rect(90,30,60,60)
class Cube:
def update(self):
self.cx, self.cy = pygame.mouse.get_pos()
self.square = pygame.Rect(self.cx - 25, self.cy - 25, 50, 50)
def draw(self):
pygame.draw.rect(WIN, GREEN, self.square)
def main():
cubeCount = 0
run = True
cubeList = []
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
print("Ending")
run = False
if event.type == pygame.MOUSEBUTTONDOWN:
cube = Cube()
cubeList.append(cube)
cube.update()
clock.tick(60)
WIN.fill((BLUE))
pygame.draw.rect(WIN,RED, Square1)
pygame.draw.rect(WIN, GREEN, Square2)
for x in cubeList:
x.draw()
pygame.display.flip()
pygame.quit()
if __name__ == "__main__":
main()

Categories