I need to create a clickable 8 by 8 grid in pygame.
Right now I have something like this:
#!/usr/bin/python2
#-------------------------------------------------------------------------------
# Imports & Inits
import pygame, sys
from pygame.locals import *
pygame.init()
#-------------------------------------------------------------------------------
# Settings
WIDTH = 105
HEIGHT = 105
FPS = 60
#-------------------------------------------------------------------------------
# Screen Setup
WINDOW = pygame.display.set_mode([WIDTH,HEIGHT])
CAPTION = pygame.display.set_caption('Test')
SCREEN = pygame.display.get_surface()
TRANSPARENT = pygame.Surface([WIDTH,HEIGHT])
TRANSPARENT.set_alpha(255)
TRANSPARENT.fill((255,255,255))
#-------------------------------------------------------------------------------
# Misc stuff
rect1 = pygame.draw.rect(SCREEN, (255, 255, 255), (0,0, 50, 50))
rect2 = pygame.draw.rect(SCREEN, (255, 255, 255), (0,55, 50, 50))
rect3 = pygame.draw.rect(SCREEN, (255, 255, 255), (55,0, 50, 50))
rect4 = pygame.draw.rect(SCREEN, (255, 255, 255), (55,55, 50, 50))
...
#-------------------------------------------------------------------------------
# Refresh Display
pygame.display.flip()
#-------------------------------------------------------------------------------
# Main Loop
while True:
pos = pygame.mouse.get_pos()
mouse = pygame.draw.circle(TRANSPARENT, (0, 0, 0), pos , 0)
# Event Detection---------------
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
elif event.type == MOUSEBUTTONDOWN:
if rect1.contains(mouse):
rect1 = pygame.draw.rect(SCREEN, (155, 155, 155), (0,0, 50, 50))
pygame.display.flip()
Now, in my original code, I have far more rectangles and I need a way to do something like this:
for i in rectangles:
if i hasbeenclickedon:
change color
Obviously, my solution is far too static.
So, how could I accomplish this?
Though your solution is indeed a little cumbersome, I'd first say
rectangles = (rect1, rect2, ...)
then you can iterate over them as intended.
Try sth like
pos = pygame.mouse.get_pos()
for rect in rectangles:
if rect.collidepoint(pos):
changecolor(rect)
You'd have to implement the changecolor method, of course.
Generally, I'd recommend creating a class for you clickable fields that defines a method changecolor.
Simple 'human' colors:
Color("red")
Color(255,255,255)
Color("#fefefe")
Use:
import pygame
# This makes event handling, rect, and colors simpler.
# Now you can refer to `Sprite` or `Rect()` vs `pygame.sprite.Sprite` or `pygame.Rect()`
from pygame.locals import *
from pygame import Color, Rect, Surface
pygame.draw.rect(screen, Color("blue"), Rect(10,10,200,200), width=0)
pygame.draw.rect(screen, Color("darkred"), Rect(210,210,400,400), width=0)
Related
Hello I'm creating a sudoku game in pygame and while making the board I wanted to make all lines black but some thicker than others like so:
Do any of you know of a way to do this?
The last argument of pygame.draw.line is optional and specifies the thickness of the line:
draw a straight line
line(surface, color, start_pos, end_pos, width=1) -> Rect
...
width (int) -- (optional) used for line thickness
Minimal example:
import pygame
pygame.init()
window = pygame.display.set_mode((400, 400))
clock = pygame.time.Clock()
run = True
while run:
clock.tick(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
window.fill((255, 255, 255))
for i in range(10):
w = 3 if i == 3 or i == 6 else 1
pygame.draw.line(window, (0, 0, 0), (50, 50+i*30), (320, 50+i*30), w)
pygame.draw.line(window, (0, 0, 0), (50+i*30, 50), (50+i*30, 320), w)
pygame.display.flip()
pygame.quit()
exit()
I am trying to represent the RGB color model using python + pygame.
I wanted to get the following:
So then, I wrote the following code:
import pygame, sys
from pygame.locals import *
ALPHA = 100
TRANSPARENT = (255,0,255)
BLACK = (0,0,0)
WHITE = (255,255,255)
RED = (255,0,0,100)
GREEN = (0,255,0,100)
BLUE = (0,0,255,100)
pygame.init()
size=(800,500)
screen= pygame.display.set_mode(size)
surf1 = pygame.Surface(size)
surf1.fill(TRANSPARENT)
surf1.set_colorkey(TRANSPARENT)
pygame.draw.circle(surf1, BLUE, (430, 280), 60 )
surf2 = pygame.Surface(size)
surf2.fill(TRANSPARENT)
surf2.set_colorkey(TRANSPARENT)
pygame.draw.circle(surf2, GREEN, (370, 280), 60 )
surf3 = pygame.Surface(size)
surf3.fill(TRANSPARENT)
surf3.set_colorkey(TRANSPARENT)
pygame.draw.circle(surf3, RED, (400, 220), 60 )
surf1.set_alpha(ALPHA)
surf2.set_alpha(ALPHA)
surf3.set_alpha(ALPHA)
while True :
screen.fill(WHITE)
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
screen.blit(surf1, (0,0))
screen.blit(surf2, (0,0))
screen.blit(surf3, (0,0))
pygame.display.flip()
But instead of getting the RGB color model, I got this, with colors incorrectly blended:
Does anybody know what it could be? Thanks!
This should work:
import pygame, sys
from pygame.locals import *
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0, 100)
GREEN = (0, 255, 0, 100)
BLUE = (0, 0, 255, 100)
pygame.init()
size = (800, 500)
screen = pygame.display.set_mode(size)
surf1 = pygame.Surface(size)
surf1.fill(BLACK)
surf1.set_colorkey(BLACK)
pygame.draw.circle(surf1, BLUE, (430, 280), 60)
surf2 = pygame.Surface(size)
surf2.fill(BLACK)
surf2.set_colorkey(BLACK)
pygame.draw.circle(surf2, GREEN, (370, 280), 60)
surf3 = pygame.Surface(size)
surf3.fill(BLACK)
surf3.set_colorkey(BLACK)
pygame.draw.circle(surf3, RED, (400, 220), 60)
surf4 = pygame.Surface(size)
surf4.set_colorkey(BLACK)
surf4.blit(surf1, (0, 0), special_flags=pygame.BLEND_RGB_ADD)
surf4.blit(surf2, (0, 0), special_flags=pygame.BLEND_RGB_ADD)
surf4.blit(surf3, (0, 0), special_flags=pygame.BLEND_RGB_ADD)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
screen.fill(WHITE)
screen.blit(surf4, (0, 0))
pygame.display.flip()
You want to add the colors at the intersections of the circles, hence use the BLEND_RGB_ADD flag.
I created a fourth surface since you wanted a white background.
The screen wont fill and it says there is no fill cmd for screen, what did I do wrong?
import pygame
pygame.init()
screen = pygame.display
set_mode((0, 0))
white = (255, 255, 255)
Square =(0, 0, 0)
loop = True
while loop:
screen.fill(white)
pygame.draw.rect(screen, Square, pygame)Rect(100, 100, 200, 200)
pygame.display.update()
There are some syntax errors in your code:
screen = pygame.display
set_mode((0, 0))
screen = pygame.display.set_mode((0, 0))
pygame.draw.rect(screen, Square, pygame)Rect(100, 100, 200, 200)
pygame.draw.rect(screen, Square, pygame.Rect(100, 100, 200, 200))
More than that, the event loop is missing. This can cause your application to freeze. See pygame.event.get() respectively pygame.event.pump():
For each frame of your game, you will need to make some sort of call to the event queue. This ensures your program can internally interact with the rest of the operating system.
Correct program:
import pygame
pygame.init()
screen = pygame.display.set_mode((640, 480))
white = (255, 255, 255)
Square = (0, 0, 0)
loop = True
while loop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
loop = False
screen.fill(white)
pygame.draw.rect(screen, Square, pygame.Rect(100, 100, 200, 200))
pygame.display.update()
This question already has an answer here:
Why is my pygame application loop not working properly?
(1 answer)
Closed 2 years ago.
I made a picture of Pacman and I am trying to make it move to the right across the screen. This is my code so far. I have the Pacman drawing but it is many shapes combined together and I don't know how to move all of them at once.
import os
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d, %d" %(20, 20)
import pygame
pygame.init()
BLACK = (0,0,0)
YELLOW = (255, 245, 59)
WHITE = (242, 242, 242)
SIZE = (500, 500)
screen = pygame.display.set_mode(SIZE)
# Fill background
pygame.draw.rect(screen, WHITE, (0,0, 500, 500))
pygame.draw.circle(screen, YELLOW, (250,250), 100,)
pygame.draw.circle(screen, BLACK, (250,250), 100, 3)
pygame.draw.circle(screen, BLACK, (260,200), 10,)
pygame.draw.polygon(screen, WHITE, ((250,250),(500,500),(500,100)))
pygame.draw.line(screen, BLACK, (250, 250), (334, 198), 3)
pygame.draw.line(screen, BLACK, (250, 250), (315, 318), 3)
pygame.display.flip()
pygame.time.wait(5000)
You have to add an application loop. The main application loop has to:
handle the events by either pygame.event.pump() or pygame.event.get().
update the game states and positions of objects dependent on the input events and time (respectively frames)
clear the entire display or draw the background
draw the entire scene (draw pacman)
update the display by either pygame.display.update() or pygame.display.flip()
clock = pygame.time.Clock()
run = True
while run:
clock.tick(60)
# event loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
# update position
# [...]
# clear the display
screen.fill(WHITE)
# draw the scene
pacman(px, py, dir_x)
# update the display
pygame.display.flip()
Furthermore you have to draw pacman relative to a position (x, y) and a direction (dir_x). See the example:
import pygame
pygame.init()
BLACK = (0,0,0)
YELLOW = (255, 245, 59)
WHITE = (242, 242, 242)
SIZE = (500, 500)
screen = pygame.display.set_mode(SIZE)
def pacman(x, y, dir_x):
sign_x = -1 if dir_x < 0 else 1
pygame.draw.circle(screen, YELLOW, (x, y), 100,)
pygame.draw.circle(screen, BLACK, (x, y), 100, 3)
pygame.draw.circle(screen, BLACK, (x+10*sign_x, y-50), 10,)
pygame.draw.polygon(screen, WHITE, ((x, y),(x+250*sign_x, y+250),(x+250*sign_x, y-150)))
pygame.draw.line(screen, BLACK, (x, y), (x+84*sign_x, y-52), 3)
pygame.draw.line(screen, BLACK, (x, y), (x+65*sign_x, y+68), 3)
px, py, dir_x = 250, 250, 1
clock = pygame.time.Clock()
run = True
while run:
clock.tick(60)
# event loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
px += dir_x
if px > 300 or px < 200:
dir_x *= -1
# clear the display
screen.fill(WHITE)
# draw the scene
pacman(px, py, dir_x)
# update the display
pygame.display.flip()
I am programming in Python with the pygame library. I want to know how to make a rectangle variable, something like:
import ...
rect1 = #RECT VARIABLE
rect2 = #RECT VARIABLE
Just do
pygame.Rect(left, top, width, height)
this will return you a Rect object in pygame
Perhaps something like this will work?
import pygame
# your whole code until you need a rectangle
rect1 = pygame.draw.rect( (x_coordinate, y_coordinate), (#a rgb color), (#size of rect in pixels) )
If you want a red rectangle with 100x150 size in 0x50 position, it will be:
rect1 = pygame.draw.rect( (0, 50), (255, 0, 0), (100, 150) )
To insert it into the screen you use:
pygame.screen_you_have_created.blit(rect1, 0, 50)
For rectangle draw pattern is : pygame.Rect(left, top, width, height)
You can modify this code according to your need.
import pygame, sys
from pygame.locals import *
pygame.init()
windowSurface = pygame.display.set_mode((500, 400), 0, 32)
pygame.display.set_caption('Title')
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
basicFont = pygame.font.SysFont(None, 48)
message = 'Hello '
text = basicFont.render(message, False, WHITE, BLUE)
textRect = text.get_rect()
textRect.centerx = windowSurface.get_rect().centerx
textRect.centery = windowSurface.get_rect().centery
windowSurface.fill(WHITE)
pixArray = pygame.PixelArray(windowSurface)
pixArray[480][380] = BLACK
del pixArray
windowSurface.blit(text, textRect)
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == K_ESCAPE:
pygame.quit()
sys.exit()