How to create a rhomboid in pygame - python

I want to create a rhomboid like this. I can not do this I found the solution just for the diamond.
my code:
import pygame as pg
pg.init()
screen = pg.display.set_mode((500, 700))
clock = pg.time.Clock()
BG_COLOR = pg.Color('gray12')
BLUE = pg.Color('dodgerblue')
points = [(200, 200), (250, 250), (200, 300), (150, 250)]
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
screen.fill(BG_COLOR)
pg.draw.polygon(screen, BLUE, points)
pg.display.flip()
clock.tick()

Write a function that draws a rhomboid with a with, height and offset:
import pygame as pg
def drawRhomboid(surf, color, x, y, width, height, offset, thickness=0):
points = [
(x + offset, y),
(x + width + offset, y),
(x + width, y + height),
(x, y + height)]
pg.draw.polygon(surf, color, points, thickness)
pg.init()
screen = pg.display.set_mode((500, 700))
clock = pg.time.Clock()
BG_COLOR = pg.Color('gray12')
BLUE = pg.Color('dodgerblue')
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
screen.fill(BG_COLOR)
drawRhomboid(screen, BLUE, 50, 50, 300, 200, 100, 3)
pg.display.flip()
clock.tick()
If you want to draw an isometric cube, you just need to calculate the correct points and stick the cube from Rhomboids:
import pygame, math
def drawCube(surf, center, size, angle):
v = pygame.math.Vector2(1, 0)
v.rotate_ip(angle + 45)
lx = round(v.x * size * math.sqrt(2) / 2)
ly = round(v.y * size * math.sqrt(2) / 2)
x, y = center
s = size/2
f1 = [(x+lx, y-s+ly//2), (x-ly, y-s+lx//2), (x-lx, y-s-ly//2), (x+ly, y-s-lx//2)]
pts = [(lx, ly//2), (-ly, lx//2), (-lx, -ly//2), (ly, -lx//2)]
faces = []
for i in range(4):
p0, p1 = pts[i], pts[(i+1) % 4]
f = [(p0[0]+x, p0[1]+y-s), (p1[0]+x, p1[1]+y-s), (p1[0]+x, p1[1]+y+s), (p0[0]+x, p0[1]+y+s)]
faces.append(f)
colors = ["red", "yellow", "green", "blue"]
for face, color in zip(faces, colors):
if (face[0][1] + face[1][1]) / 2 > y-s:
pygame.draw.polygon(surf, color, face, 3)
pygame.draw.polygon(surf, "white", f1, 3)
pygame.init()
window = pygame.display.set_mode((300, 300))
clock = pygame.time.Clock()
angle = 0
run = True
while run:
clock.tick(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
window_center = window.get_rect().center
window.fill(0)
drawCube(window, window_center, 150, angle)
pygame.display.flip()
angle += 1
pygame.quit()
exit()

Related

How can I Make appear Button above a Pygame Circle?

I have been trying for a long time to try to put a button above a multicoloured circle but I can't, when I do it the button is covered by the circle, I have tried several things but none of them work.
import pygame, math
from pygame import font
pygame.init()
def lerp_color(colors, value):
fract, index = math.modf(value)
color1 = pygame.Color(colors[int(index) % len(colors)])
color2 = pygame.Color(colors[int(index + 1) % len(colors)])
return color1.lerp(color2, fract)
def button(screen, position, text):
font = pygame.font.SysFont("Anton", 50)
text_render = font.render(text, 1, (0, 0, 0))
x, y, w , h = text_render.get_rect()
x, y = position
pygame.draw.line(screen, (150, 150, 150), (x, y), (x + w , y), 5)
pygame.draw.line(screen, (150, 150, 150), (x, y - 2), (x, y + h), 5)
pygame.draw.rect(screen, (100, 100, 100), (x, y, w , h))
return screen.blit(text_render, (x, y))
pygame.init()
screen = pygame.display.set_mode((800, 800))
screen2 = pygame.display.set_mode((800,800))
clock = pygame.time.Clock()
start_time = pygame.time.get_ticks()
b1 = button(screen,(310,200), "Faster")
variable = 1
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
start_time = pygame.time.get_ticks()
screen.blit(b1)
colors = [(000,000,000), (255,0,0), (000,255,000), (000,000,255), (255,0,255), (000,255,255), (255,255,000), (255,000,255)]
value = (pygame.time.get_ticks() - start_time) / 1000
current_color = lerp_color(colors, value)
pygame.draw.circle(screen2, current_color, screen.get_rect().center, 500)
print(value)
pygame.display.flip()
pygame.quit()
exit()
You must draw the button after the circle:
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
start_time = pygame.time.get_ticks()
colors = [(000,000,000), (255,0,0), (000,255,000), (000,000,255), (255,0,255), (000,255,255), (255,255,000), (255,000,255)]
value = (pygame.time.get_ticks() - start_time) / 1000
print(value)
current_color = lerp_color(colors, value)
screen.fill(0)
pygame.draw.circle(screen2, current_color, screen.get_rect().center, 500)
button(screen,(310,200), "Faster")
pygame.display.flip()
Note, The instruction screen.blit(b1) does not do what you expect. It only cuases an error. blit does not return an object, it only returns the rectangular area that was affected by the operation. Therefore the button returns a pygame.Rect object and bl is just a rectangle.

how do i set the mouse position around a circle in pygame

I'm trying to create this program in pygame to study code/math, but i don't know how to lock the mouse position around a circle in pygame, any help?
import pygame
pygame.init()
x = 250
y = 250
window = pygame.display.set_mode((500, 500))
pygame.display.set_caption("around circle")
#line to be created
def Lin(xref, yref):
lin = pygame.draw.line(window, (250, 250, 0), (x, y), (xref, yref), 1)
window_open = True
while window_open:
pygame.display.update()
window.fill((0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
window_open = False
mousepos = pygame.mouse.get_pos()
xref = mousepos[0]
yref = mousepos[1]
# 2 circles to get only the border
cir0 = pygame.draw.circle(window, (250, 250, 250), (x, y), 100, 1)
cir1 = pygame.draw.circle(window, (250, 250, 250), (x, y), 99, 1)
Lin(xref, yref)
pygame.quit()
I would do something similar to Mike67 answer, but would take advantage of the features of pygame's Vector's (see docs here).
import pygame
pygame.init()
x = 250
y = 250
radius = 100
center = pygame.Vector2(x, y)
window = pygame.display.set_mode((500, 500))
pygame.display.set_caption("around circle")
#line to be created
def Lin(start_point, vector):
pygame.draw.line(window, (250, 250, 0), start_point, start_point+vector, 1)
window_open = True
while window_open:
pygame.display.update()
window.fill((0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
window_open = False
mousepos = pygame.mouse.get_pos()
line_vector = pygame.Vector2(mousepos) - center
if line_vector.length() > radius:
line_vector.scale_to_length(radius)
# 2 circles to get only the border
cir0 = pygame.draw.circle(window, (250, 250, 250), (x, y), radius, 1)
cir1 = pygame.draw.circle(window, (250, 250, 250), (x, y), radius-1, 1)
Lin(center, line_vector)
pygame.quit()
I modified the Lin() function as well.
EDIT:
From your comment you indicated that you want it to always scale to the side of the circle. In that case just skip the test if line_vector.length() > radius and always scale it. You might be tempted to only scale it if length != radius, but when comparing floating point calculated numbers they are very unlikely to actually be the same (because of the decimal places involved) and you could see if the difference is less than a threshold and call them equal, but it really isn't worth the complication.
import pygame
pygame.init()
x = 250
y = 250
radius = 100
center = pygame.Vector2(x, y)
window = pygame.display.set_mode((500, 500))
pygame.display.set_caption("around circle")
#line to be created
def Lin(start_point, vector):
pygame.draw.line(window, (250, 250, 0), start_point, start_point+vector, 1)
window_open = True
while window_open:
pygame.display.update()
window.fill((0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
window_open = False
mousepos = pygame.mouse.get_pos()
line_vector = pygame.Vector2(mousepos) - center
line_vector.scale_to_length(radius)
# 2 circles to get only the border
cir0 = pygame.draw.circle(window, (250, 250, 250), (x, y), radius, 1)
cir1 = pygame.draw.circle(window, (250, 250, 250), (x, y), radius-1, 1)
Lin(center, line_vector)
pygame.quit()
To keep the line in the circle, just adjust the mouse x/y coordinates relative to the mouse distance from the center.
Here is the updated code:
import pygame
import math
pygame.init()
x = 250
y = 250
window = pygame.display.set_mode((500, 500))
pygame.display.set_caption("around circle")
#line to be created
def Lin(xref, yref):
lin = pygame.draw.line(window, (250, 250, 0), (x, y), (xref, yref), 1)
window_open = True
while window_open:
pygame.display.update()
window.fill((0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
window_open = False
mousepos = pygame.mouse.get_pos()
xref = mousepos[0]
yref = mousepos[1]
# get mouse distance from center
dist = math.sqrt((xref-x)**2 + (yref-y)**2)
if (dist > 100): # if mouse outside circle, adjust x/y proportionally
xref = x + (xref - x) * (100/dist)
yref = y + (yref - y) * (100/dist)
# 2 circles to get only the border
cir0 = pygame.draw.circle(window, (250, 250, 250), (x, y), 100, 1)
cir1 = pygame.draw.circle(window, (250, 250, 250), (x, y), 99, 1)
Lin(xref, yref)
pygame.quit()

how to generate random rectangles in a pygame and make them move like flappy bird?

I am a python beginner. I want to recreate chrome dino game. the random rectangle won't stop and the loop runs forever...please help me to stop the loop and make rectangles move.
Code:
import pygame
import random
pygame.init()
win = pygame.display.set_mode((500, 500))
#red rectangle(dino)
x = 20
y = 400
width = 30
height = 42
gravity = 5
vel = 18
black = (0, 0, 0)
#ground
start_pos = [0, 470]
end_pos = [500, 470]
#cactus
x1 = 20
y1 = 30
white = (2, 200, 200)
run = True
clock = pygame.time.Clock()
while run:
clock.tick(30)
pygame.time.delay(10)
win.fill(black)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
#random rectangle generation
for i in range(1):
width2 = random.randint(25, 25)
height2 = random.randint(60, 60)
top = random.randint(412, 412)
left = random.randint(300, 800)
rect = pygame.draw.rect(win, white, (left, top, width2,height2))
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
y = y - vel
else:
y = min(428, y + gravity)
pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))
pygame.draw.line(win, white, start_pos, end_pos, 2)
pygame.display.update()
pygame.display.flip()
pygame.quit()
pygame.draw.rect() des not "generate" a rectangle, it draws a rectangle on a surface.
pygame.Rect is a rectangle object. Create an instance of pygame.Rect before the main application loop:
obstracle = pygame.Rect(500, random.randint(0, 412), 25, 60)
Change the position of the rectangle:
obstracle.x -= 3
if obstracle.right <= 0:
obstracle.y = random.randint(0, 412)
And draw the rectangle to the window surface:
pygame.draw.rect(win, white, obstracle)
Example:
import pygame
import random
pygame.init()
win = pygame.display.set_mode((500, 500))
start_pos = [0, 470]
end_pos = [500, 470]
gravity = 5
vel = 18
black = (0, 0, 0)
white = (2, 200, 200)
hit = 0
dino = pygame.Rect(20, 400, 30, 40)
obstracles = []
number = 5
for i in range(number):
ox = 500 + i * 500 // number
oy = random.randint(0, 412)
obstracles.append(pygame.Rect(ox, oy, 25, 60))
run = True
clock = pygame.time.Clock()
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
dino.y -= vel
else:
dino.y = min(428, dino.y + gravity)
for obstracle in obstracles:
obstracle.x -= 3
if obstracle.right <= 0:
obstracle.x = 500
obstracle.y = random.randint(0, 412)
if dino.colliderect(obstracle):
hit += 1
win.fill(black)
color = (min(hit, 255), max(255-hit, 0), 0)
pygame.draw.rect(win, color, dino)
for obstracle in obstracles:
pygame.draw.rect(win, white, obstracle)
pygame.draw.line(win, white, start_pos, end_pos, 2)
pygame.display.update()
pygame.quit()

Python - Pygame - circular movement

I´ve the following code:
import pygame, sys, math
run = True
white = (255, 255, 255)
black = (0, 0, 0)
angle = 0
size = width, height = 800, 800
screen = pygame.display.set_mode(size)
clock = pygame.time.Clock()
screen.fill(white)
while run:
msElapsed = clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
screen.fill(white)
pygame.draw.circle(screen, black, (int(math.cos(angle) * 100), int(math.sin(angle) * 100)), 10)
pygame.display.flip()
angle += 0.05
pygame.quit()
this creates me a circle, which circles around the coordination (0, 0).
i want to move the center from (0, 0) to (100, 100).
Thank you in advance
Just add 100 to the x and y coordinates to adjust the center position:
x = int(math.cos(angle) * 100) + 100
y = int(math.sin(angle) * 100) + 100
pygame.draw.circle(screen, black, (x, y), 10)

Why does the game lag when I load an image and when I display pygame shapes?

Just out of curiosity, why does a game lag when I load an image as well as display pygame shapes for example rectangles, circles and ellipses. I have this code where you shoot the ghosts that fall down (I'm still working on the shooting part). I made the cannon out of pygame shapes. But when I run it the images of the ghosts are prefect but the images of the cannons lag and disappears and reappear and so on. Is there any way to stop this lag or disappear and reappear thing? I'm running python 2.6 with windows vista.
import pygame, sys, random, math
from pygame.locals import *
WINDOWHEIGHT = 600
WINDOWWIDTH = 600
FPS = 30
BACKGROUNDCOLOR = (255, 255, 255)
TEXTCOLOR = (0, 0, 0)
WHITE = (255, 255, 255)
BLACK = ( 0, 0, 0)
BROWN = (139, 69, 19)
DARKGRAY = (128, 128, 128)
BGCOLOR = WHITE
GHOSTSPEED = 10
GHOSTSIZE = 20
ADDNEWGHOSTRATE = 8
def keyToPlayAgain():
while True:
for event in event.type.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
return
def getAngle(x1, y1, x2, y2):
# Return value is 0 for right, 90 for up, 180 for left, and 270 for down (and all values between 0 and 360)
rise = y1 - y2
run = x1 - x2
angle = math.atan2(run, rise) # get the angle in radians
angle = angle * (180 / math.pi) # convert to degrees
angle = (angle + 90) % 360 # adjust for a right-facing sprite
return angle
def Text(text, font, surface, x, y):
textobj = font.render(text, 2, TEXTCOLOR)
textrect = textobj.get_rect()
textrect.topleft = (x, y)
surface.blit(textobj, textrect)
pygame.init()
mainClock = pygame.time.Clock()
DISPLAYSURF = pygame.display.set_mode((WINDOWHEIGHT, WINDOWWIDTH))
pygame.display.set_icon(pygame.image.load('s.icon'))
pygame.display.set_caption('Ghost Invasion Pacfighters')
pygame.mouse.set_visible(False)
font = pygame.font.SysFont(None, 48)
gameOverSound = pygame.mixer.Sound('gameover.wav')
pygame.mixer.music.load('background.mid')
ghostImage = pygame.image.load('ghosts.png')
dotImage = pygame.image.load('dot.png')
dotRect = dotImage.get_rect()
creditsPage = pygame.image.load('credits.png')
titlePage = pygame.image.load('title.png')
pygame.time.wait(10000)
DISPLAYSURF.blit(creditsPage, (0, 0))
pygame.time.wait(10000)
DISPLAYSURF.blit(titlePage, (0, 0))
pygame.display.update()
cannonSurf = pygame.Surface((100, 100))
cannonSurf.fill(BGCOLOR)
pygame.draw.circle(cannonSurf, DARKGRAY, (20, 50), 20)
pygame.draw.circle(cannonSurf, DARKGRAY, (80, 50), 20)
pygame.draw.rect(cannonSurf, DARKGRAY, (20, 30, 60, 40))
pygame.draw.circle(cannonSurf, BLACK, (80, 50), 15)
pygame.draw.circle(cannonSurf, BLACK, (80, 50), 20, 1)
pygame.draw.circle(cannonSurf, BROWN, (30, 70), 20)
pygame.draw.circle(cannonSurf, BLACK, (30, 70), 20, 1)
health = 100
score = 0
topScore = 0
while True:
ghosts = []
moveLeft = moveRight = moveUp = moveDown = False
reverseCheat = slowCheat = False
ghostAddCounter = 0
pygame.mixer.music.play(-1, 0.0)
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
if event.key == K_x:
bombs()
elif event.type == ESCAPE:
pygame.quit()
sys.exit()
mousex, mousey = pygame.mouse.get_pos()
for cannonx, cannony in ((100, 500), (500, 500)):
degrees = getAngle(cannonx, cannony, mousex, mousey)
rotatedSurf = pygame.transform.rotate(cannonSurf, degrees)
rotatedRect = rotatedSurf.get_rect()
rotatedRect.center = (cannonx, cannony)
DISPLAYSURF.blit(rotatedSurf, rotatedRect)
pygame.draw.line(DISPLAYSURF, BLACK, (mousex - 10, mousey), (mousex + 10, mousey))
pygame.draw.line(DISPLAYSURF, BLACK, (mousex, mousey - 10), (mousex, mousey + 10))
pygame.draw.rect(DISPLAYSURF, BLACK, (0, 0, WINDOWWIDTH, WINDOWHEIGHT), 1)
pygame.display.update()
if not reverseCheat and not slowCheat:
ghostAddCounter += 1
if ghostAddCounter == ADDNEWGHOSTRATE:
ghostAddCounter = 0
newGhost = {'rect': pygame.Rect(random.randint(0, WINDOWWIDTH-GHOSTSIZE), 0 - GHOSTSIZE, GHOSTSIZE, GHOSTSIZE),
'speed': (GHOSTSIZE),
'surface':pygame.transform.scale(ghostImage, (GHOSTSIZE, GHOSTSIZE)),
}
ghosts.append(newGhost)
for s in ghosts:
if not reverseCheat and not slowCheat:
s['rect'].move_ip(0, s['speed'])
elif reverseCheat:
s['rect'].move_ip(0, -5)
elif slowCheat:
s['rect'].move_ip(0, -1)
for s in ghosts[:]:
if s['rect'].top > WINDOWHEIGHT:
health -= 10
DISPLAYSURF.fill(BACKGROUNDCOLOR)
Text('Score: %s' % (score), font, DISPLAYSURF, 10, 0)
Text('Top score: %s' % (topScore), font, DISPLAYSURF, 10, 40)
Text('Health: %s' % (health), font, DISPLAYSURF, 10, 560)
for s in ghosts:
DISPLAYSURF.blit(s['surface'], s['rect'])
pygame.display.update()
mainClock.tick(FPS)
pygame.mixer.music.stop()
gameOverSound.play()
Text('GAME OVER', font, windowSurface, (WINDOWWIDTH / 3), (WINDOWHEIGHT / 3))
pygame.display.update()
keyToPlayAgain()
pygame.display.update()
gameOverSound.stop()
The problem is that you draw your cannons, update the display, then clear the display, draw the other stuff, and update the display again. You basically never see the falling ghosts and the cannons at the same time. This results in the flickering you see.
So remove pygame.display.update() from this for loop
for cannonx, cannony in ((100, 500), (500, 500)):
...
pygame.display.update()
and put DISPLAYSURF.fill(BACKGROUNDCOLOR) at the top of your while loop (or at least before you draw anything):
while True:
for event in pygame.event.get():
...
mousex, mousey = pygame.mouse.get_pos()
DISPLAYSURF.fill(BACKGROUNDCOLOR)
for cannonx, cannony in ((100, 500), (500, 500)):
...
It's best to clear the background once at the start of your code that draws everything, and call pygame.display.update() once at the end of that drawing code.

Categories

Resources