How to properly Dirty rectangles for simple application: Logic Issue - python

I am currently just testing the waters with Pygame display as I am extremely new to the module.
Here is the code:
import pygame
pygame.init()
SCALE = 1
display_width = int(1200 * SCALE)
display_height = int(800 * SCALE)
x = (display_width * 0.45)
y = (display_height * 0.8)
transparent = (0, 0, 0, 0)
black = (0, 0, 0)
white = (255, 255, 255)
green = (44, 215, 223)
red = (255, 67, 34)
blue = (77, 77, 77)
count = 0
running = True
box1_image = True
box3_image = True
background = pygame.image.load('C:/Users/Justi/source/repos/03 Game/Game/assets/background.jpg')
dirty_spot = pygame.image.load('C:/Users/Justi/source/repos/03 Game/Game/assets/dirty.png')
background_resized = pygame.transform.scale(dirty_spot, (display_width, display_height))
background_rect = background_resized.get_rect()
gameDisplay = pygame.display.set_mode(background_rect.size)
pygame.display.set_caption("Game 1")
clock = pygame.time.Clock()
dirty_spot_resized = pygame.transform.scale(dirty_spot, (200, 200))
square_dirty_spot = dirty_spot_resized.convert()
rect_dirty_spot = square_dirty_spot.get_rect()
def dirtyspot1(imagex, imagey):
gameDisplay.blit(dirty_spot_resized, (imagex,imagey))
while running:
rects = []
if rects != []:
pygame.display.update(rects)
for event in pygame.event.get():
if event.type == pygame.quit:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_r:
running = False
#if box3_image: #Replacing the Bool passing variable with a simple loop count to trigger the else condition
if count < 50:
print("Test 1")
dirtyspot1(0,0)
pygame.display.update()
else:
print("Test 2")
dirty_rect = background.subsurface(rect_dirty_spot)
gameDisplay.blit(dirty_rect, (0,0))
rects.append(pygame.Rect(0,0, 200, 200))
gameDisplay.fill(white)
clock.tick(30)
count += 1
pygame.quit()
quit()
box1_image & box1_image are bool variables to flag certain condition which is passed from another function.
But I have gotten the passing of said variables working by doing a simple test with print("Test 2"). However, when it tries to blit dirty_rect. Nothing changes on the pygame display.
Assets (if needed):
background.jpg
dirty.png
May I check what is missing to properly "remove/delete" the dirty_spot blit? Thank you in advance.

import pygame
pygame.init()
SCALE = 1
display_width = int(1200 * SCALE)
display_height = int(800 * SCALE)
x = (display_width * 0.45)
y = (display_height * 0.8)
transparent = (0, 0, 0, 0)
black = (0, 0, 0)
white = (255, 255, 255)
green = (44, 215, 223)
red = (255, 67, 34)
blue = (77, 77, 77)
count = 0
running = True
box1_image = True
box3_image = True
background = pygame.image.load('background.jpg')
dirty_spot = pygame.image.load('dirty.png')
background_resized = pygame.transform.scale(background, (display_width, display_height))
background_rect = background_resized.get_rect()
gameDisplay = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption("Game 1")
clock = pygame.time.Clock()
dirty_spot_resized = pygame.transform.scale(dirty_spot, (200, 200))
square_dirty_spot = dirty_spot_resized.convert()
rect_dirty_spot = square_dirty_spot.get_rect()
show_dirt = False
def dirtyspot1(imagex, imagey):
gameDisplay.blit(dirty_spot_resized, (imagex,imagey))
while running:
rects = []
if rects != []:
pygame.display.update(rects)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_r:
running = False
if event.key == pygame.K_d:
show_dirt = not show_dirt
if event.key == pygame.K_UP:
rect_dirty_spot.y -= 5
if event.key == pygame.K_DOWN:
rect_dirty_spot.y += 5
if event.key == pygame.K_RIGHT:
rect_dirty_spot.x += 5
if event.key == pygame.K_LEFT:
rect_dirty_spot.x -= 5
gameDisplay.blit(background_resized, (0,0))
if show_dirt:
gameDisplay.blit(dirty_spot_resized, rect_dirty_spot)
pygame.display.update()
clock.tick(5)
count += 1
pygame.quit()
quit()

like this? (I changed the FPS to 5 so it takes a while to change)
import pygame
pygame.init()
SCALE = 1
display_width = int(1200 * SCALE)
display_height = int(800 * SCALE)
x = (display_width * 0.45)
y = (display_height * 0.8)
transparent = (0, 0, 0, 0)
black = (0, 0, 0)
white = (255, 255, 255)
green = (44, 215, 223)
red = (255, 67, 34)
blue = (77, 77, 77)
count = 0
running = True
box1_image = True
box3_image = True
background = pygame.image.load('background.jpg')
dirty_spot = pygame.image.load('dirty.png')
background_resized = pygame.transform.scale(background, (display_width, display_height))
background_rect = background_resized.get_rect()
gameDisplay = pygame.display.set_mode(background_rect.size)
pygame.display.set_caption("Game 1")
clock = pygame.time.Clock()
dirty_spot_resized = pygame.transform.scale(dirty_spot, (200, 200))
square_dirty_spot = dirty_spot_resized.convert()
rect_dirty_spot = square_dirty_spot.get_rect()
def dirtyspot1(imagex, imagey):
gameDisplay.blit(dirty_spot_resized, (imagex,imagey))
while running:
rects = []
if rects != []:
pygame.display.update(rects)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_r:
running = False
gameDisplay.fill(white)
#if box3_image: #Replacing the Bool passing variable with a simple loop count to trigger the else condition
if count < 50:
print("Test 1")
dirtyspot1(0,0)
else:
print("Test 2")
dirty_rect = background.subsurface(rect_dirty_spot)
gameDisplay.blit(dirty_rect, (0,0))
rects.append(pygame.Rect(0,0, 200, 200))
pygame.display.update()
clock.tick(5)
count += 1
pygame.quit()
quit()
I think you have misunderstood things. You can blit one image or surface on top of another, and you build things up like that, then blit them to the display and finally update the display.
You have this line:
background_resized = pygame.transform.scale(dirty_spot, (display_width, display_height))
which I assume should be background not dirty_spot.
I moved the call to display.update() out of the if loop, because you call display.update() last.

Related

When the code is runned, it shows a black screen than when i exit the code fires

# no errors were shown in the consle
import pygame
pygame.init()
# display size
width = 1800
height =1000
# colors
black = (0, 0, 0)
blue = (0, 0, 255)
white = (255, 255, 255)
green = (0, 255, 0)
red = (255, 0, 0)
aqua = (1, 255, 255)
# main display
gamedisplay = pygame.display.set_mode((width, height))
pygame.display.set_caption('Race Game') # the
clock = pygame.time.Clock()
# the car image
carImg = pygame.image.load('myCar.png')
# car
def car(x, y):
gamedisplay.blit(carImg, (x, y))
x = (width * 0.45)
y = (height * 0.8)
# events
crashed1 = False
x_change = 0
while not crashed1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed1 = True
# keybinds
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -5
elif event.key == pygame.K_RIGHT:
x_change = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or pygame.K_RIGHT:
x_change = 0
x += x_change
# update loop
print(event)
gamedisplay.fill(white)
car(x, y)
pygame.display.update()
clock.tick(120)
pygame.quit()
Try to put 4 spaces or one tab before these commands and get
import pygame
pygame.init()
# display size
width = 1800
height =1000
# colors
black = (0, 0, 0)
blue = (0, 0, 255)
white = (255, 255, 255)
green = (0, 255, 0)
red = (255, 0, 0)
aqua = (1, 255, 255)
# main display
gamedisplay = pygame.display.set_mode((width, height))
pygame.display.set_caption('Race Game') # the
clock = pygame.time.Clock()
# the car image
carImg = pygame.image.load('myCar.png')
# car
def car(x, y):
gamedisplay.blit(carImg, (x, y))
x = (width * 0.45)
y = (height * 0.8)
# events
crashed1 = False
x_change = 0
while not crashed1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed1 = True
# keybinds
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -5
elif event.key == pygame.K_RIGHT:
x_change = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or pygame.K_RIGHT:
x_change = 0
x += x_change
# update loop
print(event)
gamedisplay.fill(white)
car(x, y)
pygame.display.update()
clock.tick(120)
pygame.quit()

Getting TypeError: object of type 'bool' has no len() In a simple Snake game

Im creating a snake Pygame with a menu and am fine tuning the bugs and such when I come across the error
IndexError: list index out of range
the error actually appears after I open the tab itself and move the cursor over it
I have a faint idea of what it actually means but I am quite new to python and coding in general so I would appreciate it if someone could explain and show a solution,
thank you very much and here is the code
import pygame
import sys
import random
import time
pygame.init()
WHITE = (255, 255, 255)
YELLOW = (255, 255, 102)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
DARKRED = (125, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
screenWidth = 800
screenHeight = 800
screen = pygame.display.set_mode((screenWidth, screenHeight))
pygame.display.set_caption('Snake Game')
clock = pygame.time.Clock()
snakeBlock = 10
snakeSpeed = 15
fontTitle = pygame.font.SysFont("arial",100)
fontStyle = pygame.font.SysFont("ariel", 50)
scoreFont = pygame.font.SysFont("ariel", 35)
def score(score):
value = scoreFont.render(" Score: " + str(score), True, BLACK)
screen.blit(value, [50, 50])
def snake(snakeBlock, snake_list):
for x in snake_list:
pygame.draw.rect(screen, GREEN, [x[0], x[1], snakeBlock, snakeBlock])
def message(msg, colour):
msg = fontStyle.render(msg, True, BLACK)
screen.blit(msg, [screenWidth / 20, screenHeight / 2])
def gameLoop():
gameOver = False
gameEnd = False
instructions = False
game = True
intro = True
main = True
x1 = screenWidth / 2
y1 = screenHeight / 2
dx = 0
dy = 0
snakeList = []
snakeLength = 2
foodx = round(random.randrange(0, screenWidth - snakeBlock) / 10.0) * 10.0
foody = round(random.randrange(0, screenHeight - snakeBlock) / 10.0) * 10.0
def menu(titles):
buttonTitleFont = pygame.font.SysFont("arial", 52)
selection = []
rectWidth = 400
rectHeight = 60
x = int(screen.get_width()/2 - rectWidth/2)
y = 450
length = len(titles)
num = 0
hover = False
# creates the Rects (containers) for the buttons
for i in range (0,length,1):
choiceRect = pygame.Rect(x,y,rectWidth,rectHeight)
selection.append(choiceRect)
y += 100
#main loop in menu
menu = True
while menu:
for event in pygame.event.get():
if event.type == pygame.QUIT:
menu = False
pygame.quit()
sys.exit()
if event.type ==pygame.MOUSEMOTION: # if mouse moved
hover = False
mx, my = pygame.mouse.get_pos() # get the mouse position
for i in range (length):
if selection[i].collidepoint((mx,my)): # check if x,y of mouse is in a button
num = i
hover = True
if event.type == pygame.MOUSEBUTTONDOWN and hover == True: #if mouse is in button
menu = False # and has been clicked
# draw all buttons
for choice in selection:
pygame.draw.rect(screen,WHITE,choice,0)
# redraw selected button in another colour
pygame.draw.rect(screen,GREEN,selection[num],0)
# draw all the titles on the buttons
x = int(screen.get_width()/2 - 150)
y = 450
for i in range(0,length,1):
buttonTitle = buttonTitleFont.render(titles[i],True,BLACK)
screen.blit(buttonTitle,(x,y))
y += 100
pygame.display.update()
return num
while main:
for event in pygame.event.get(): # check for any events (i.e key press, mouse click etc.)
if event.type ==pygame.QUIT: # check to see if it was "x" at top right of screen
main = False # set the "main" variable to False to exit while loop
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
main = False
intro = False
screen.fill(BLACK)
menuMain = ["Launch", "Instructions","QUIT"]
mainMenu = True
mainInt = True
while mainInt:
for event in pygame.event.get():
if event.type == pygame.QUIT:
main = False
intro = False
mainInt = False
screen.fill(BLACK)
#Centers the rendered tiles
textTitle = fontTitle.render("Snake", True, GREEN )
textW = textTitle.get_width()
textH = textTitle.get_height()
xTitle = int(screenWidth/2 - textW/2)
yTitle = int(screenHeight/4 - textH/2)
screen.blit(textTitle, (xTitle,yTitle))
pygame.display.update()
# in the intro, this asks the user where they would like to go
if mainMenu ==True:
choose = menu(menuMain)
if choose == 0:
menu = False
intro = False
mainInt = False
mainMenu = False
game = True
screen.fill(BLACK)
elif choose ==1:
menu = False
instructions = True
mainMenu = False
screen.fill(BLACK)
pygame.display.update()
else:
menu = False
main = False
intro = False
mainInt = False
mainMenu = False
while game:
if gameOver == True:
game = False
while gameEnd == True:
screen.fill(DARKRED)
message("You Lost! Press C to Play Again or Q to Quit", RED)
score(snakeLength - 1)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
gameOver = True
gameEnd = False
if event.key == pygame.K_c:
gameLoop()
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameOver = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
dx = -snakeBlock
dy = 0
elif event.key == pygame.K_RIGHT:
dx = snakeBlock
dy = 0
elif event.key == pygame.K_UP:
dx = 0
dy = -snakeBlock
elif event.key == pygame.K_DOWN:
dx = 0
dy = snakeBlock
if x1 >= screenWidth or x1 < 0 or y1 >= screenHeight or y1 < 0:
gameEnd = True
x1 += dx
y1 += dy
screen.fill(WHITE)
pygame.draw.rect(screen, RED, [foodx, foody, snakeBlock, snakeBlock])
snakeHead = []
snakeHead.append(x1)
snakeHead.append(y1)
snakeList.append(snakeHead)
if len(snakeList) > snakeLength:
del snakeList[0]
for x in snakeList[:-1]:
if x == snakeHead:
gameEnd = True
snake(snakeBlock, snakeList)
score(snakeLength - 1)
pygame.display.update()
if x1 == foodx and y1 == foody:
foodx = round(random.randrange(0, screenWidth - snakeBlock) / 10.0) * 10.0
foody = round(random.randrange(0, screenHeight - snakeBlock) / 10.0) * 10.0
snakeLength += 1
clock.tick(snakeSpeed)
pygame.quit()
quit()
gameLoop()
These lines:
mainMenu = True
if mainMenu ==True:
choose = menu(mainMenu)
mean that you are passing a bool (True is a type of bool) to your menu() function.
This is how the first argument of menu() is being used:
def menu(titles):
...
length = len(titles)
So like the error message says, you're trying to take the length of a boolean (e.g. len(True) does not make sense, so you get an error). You'll have to reconcile this on your own, I'm guessing you meant for main() to accept a list of menu titles rather than True or False

Sprite mask collision problems in pygame

I am attempting to create a racing game in pygame. I want it such that when the car goes off the track, it slows down. I have tried to do this by having another sprite that is an outline of the track and when the car touches that sprite, it slows down. This does not work and I don't know why. Is there a better way to do this?
Img is the car image
Back is the racetrack
BackHit is the outline
I receive this error code:
Traceback (most recent call last):
File "C:\Users\Daniella\Desktop\Python\Games\game.py", line 75, in
if pygame.sprite.collide_mask(Img, BackHit):
File "C:\Users\Daniella\AppData\Roaming\Python\Python36\site-packages\pygame\sprite.py", line 1470, in collide_mask
xoffset = right.rect[0] - left.rect[0]
AttributeError: 'pygame.Surface' object has no attribute 'rect'
This is the code:
import pygame
Width = 800
Height = 600
Black = (0, 0, 0)
White = (255, 255, 255)
Red = (255, 0, 0)
Green = (0, 255, 0)
Blue = (0, 0, 255)
Yellow = (255, 255, 0)
BackColour = (198, 151, 107)
pygame.init()
GameDisplay = pygame.display.set_mode((Width, Height))
pygame.display.set_caption("A bit Racey")
Clock = pygame.time.Clock()
Img = pygame.image.load("download.png")
ImgWidth = 46
ImgHeight = 68
Img = pygame.transform.scale(Img, (ImgWidth, ImgHeight))
Back = pygame.image.load("back1.png")
BackWidth = Width*4
BackHeight = Height*4
Back = pygame.transform.scale(Back, (BackWidth, BackHeight))
BackHit = pygame.image.load("back1 hit1.png")
BackHitWidth = Width*4
BackHitHeight = Height*4
BackHit = pygame.transform.scale(BackHit, (BackHitWidth, BackHitHeight))
def Car():
GameDisplay.blit(Img, (400-ImgWidth/2, 300-ImgHeight/2))
def Background(X, Y):
GameDisplay.blit(Back, (X, Y))
def BackgroundHit(X, Y):
GameDisplay.blit(BackHit, (X, Y))
X = (Width*0.45)
Y = (Height*0.5)
XChange = 0
YChange = 0
Changer = 1
Crashed = False
while not Crashed:
for Event in pygame.event.get():
if Event.type == pygame.QUIT:
Crashed = True
elif Event.type == pygame.KEYDOWN:
if Event.key == pygame.K_LEFT:
Img = pygame.transform.rotate(Img, -90)
XChange = 5 / Changer
elif Event.key == pygame.K_RIGHT:
Img = pygame.transform.rotate(Img, 90)
XChange = -5 / Changer
elif Event.key == pygame.K_UP:
Img = pygame.transform.rotate(Img, 0)
YChange = 5 / Changer
elif Event.key == pygame.K_DOWN:
Img = pygame.transform.rotate(Img, 180)
YChange = -5 / Changer
if Event.type == pygame.KEYUP:
if Event.key == pygame.K_LEFT or Event.key == pygame.K_RIGHT:
XChange = 0
elif Event.key == pygame.K_UP or Event.key == pygame.K_DOWN:
YChange = 0
if pygame.sprite.collide_mask(Img, BackHit):
Changer = 2
Y += YChange
X += XChange
GameDisplay.fill(White)
BackgroundHit(X, Y)
Background(X, Y)
Car()
pygame.display.update()
Clock.tick(200)
pygame.quit()
quit()
Here's a little example that shows you how you can use pygame.mask.from_surface and pygame.Mask.overlap for pixel perfect collision detection.
import pygame as pg
# Transparent surfaces with a circle and a triangle.
circle_surface = pg.Surface((60, 60), pg.SRCALPHA)
pg.draw.circle(circle_surface, (30, 90, 200), (30, 30), 30)
triangle_surface = pg.Surface((60, 60), pg.SRCALPHA)
pg.draw.polygon(triangle_surface, (160, 250, 0), ((30, 0), (60, 60), (0, 60)))
def main():
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
# Use `pygame.mask.from_surface` to get the masks.
circle_mask = pg.mask.from_surface(circle_surface)
triangle_mask = pg.mask.from_surface(triangle_surface)
# Also create rects for the two images/surfaces.
circle_rect = circle_surface.get_rect(center=(320, 240))
triangle_rect = triangle_surface.get_rect(center=(0, 0))
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
elif event.type == pg.MOUSEMOTION:
triangle_rect.center = event.pos
# Now calculate the offset between the rects.
offset_x = triangle_rect.x - circle_rect.x
offset_y = triangle_rect.y - circle_rect.y
# And pass the offset to the `overlap` method of the mask.
overlap = circle_mask.overlap(triangle_mask, (offset_x, offset_y))
if overlap:
print('The two masks overlap!', overlap)
screen.fill((30, 30, 30))
screen.blit(circle_surface, circle_rect)
screen.blit(triangle_surface, triangle_rect)
pg.display.flip()
clock.tick(30)
if __name__ == '__main__':
pg.init()
main()
pg.quit()
To create a mask for the background or track you need to create an extra image and either leave the track transparent or the area where the car should slow down and then check if the car collides with the track or with the outside area. Here I check if the green triangle collides with the "track" (the blue lines), and in your game you would then slow the car down if it doesn't collide with the track.
import pygame as pg
bg_surface = pg.Surface((640, 480), pg.SRCALPHA)
pg.draw.lines(
bg_surface, (30, 90, 200), True,
((60, 130), (300, 50), (600, 200), (400, 400), (150, 300)),
12)
triangle_surface = pg.Surface((60, 60), pg.SRCALPHA)
pg.draw.polygon(triangle_surface, (160, 250, 0), ((30, 0), (60, 60), (0, 60)))
def main():
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
bg_mask = pg.mask.from_surface(bg_surface)
triangle_mask = pg.mask.from_surface(triangle_surface)
bg_rect = bg_surface.get_rect(center=(320, 240))
triangle_rect = triangle_surface.get_rect(center=(0, 0))
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
elif event.type == pg.MOUSEMOTION:
triangle_rect.center = event.pos
offset_x = triangle_rect.x - bg_rect.x
offset_y = triangle_rect.y - bg_rect.y
overlap = bg_mask.overlap(triangle_mask, (offset_x, offset_y))
if overlap:
print('The two masks overlap!', overlap)
screen.fill((30, 30, 30))
screen.blit(bg_surface, bg_rect)
screen.blit(triangle_surface, triangle_rect)
pg.display.flip()
clock.tick(30)
if __name__ == '__main__':
pg.init()
main()
pg.quit()
pygame.sprite.collide_mask is meant for pygame.sprite.Sprite objects. Instead use mask_var.overlap(other_mask, offset). To calculate the offset between the two masks, use offset = [other_object_x - object_x, other_object_y - object_y]. And to get the mask of an image, use mask_var = pygame.mask.from_surface(image).

Moving multiple rects by clicking on one of them

I've got two lists with 3 rects in each. Can I assign to variables selected1 and selected2 3 rects from each list and move these two rows of rects seperately up and down while keeping the gaps between them? I am beginner and don't know if such constructed code can handle this task.
import pygame
# === CONSTANS ===
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
blue = (0, 0, 255)
green = (0, 102, 0)
yellow = (255, 204, 0)
grid_color = (224, 224, 224)
width = 1200
height = 720
k = 10
screen = pygame.display.set_mode((width, height))
screen_rect = screen.get_rect()
# --- objects ---
G_x = 10 # stripe width
x = 2
stripes_x1 = []
stripes_x2 = []
G1_pos_x = 122
G2_pos_x = 367
G1_pos_y = 0
G2_pos_y = 0
G1_y = 60*x
G2_y = 75*x
G1_start = G1_pos_y + height - G1_y
G2_start = G2_pos_y + height - G2_y
a = 166
b = 222
for x in range(3):
gap = a*x
gap2 = b*x
stripes_x1.append(pygame.Rect(G1_pos_x, (G1_start - 6*k) - gap, G_x, G1_y))
stripes_x2.append(pygame.Rect(G2_pos_x, (G2_start - 6*k) - gap2, G_x, G2_y))
selected1 = None
selected2 = None
# --- mainloop ---
clock = pygame.time.Clock()
is_running = True
while is_running:
for event in pygame.event.get():
# --- global events ---
if event.type == pygame.QUIT:
is_running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
is_running = False
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
for i, r in enumerate(stripes_x1):
if r.collidepoint(event.pos):
selected1 = i
selected_offset_y = r.y - event.pos[1]
if event.type == pygame.MOUSEMOTION:
if selected1 is not None: # selected can be `0` so `is not None` is required
stripes_x1[selected1].y = event.pos[1] + selected_offset_y
if event.type == pygame.MOUSEBUTTONUP:
if event.button == 1:
selected1 = None
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
for j, r in enumerate(stripes_x2):
if r.collidepoint(event.pos):
selected2 = j
selected_offset_y = r.y - event.pos[1]
if event.type == pygame.MOUSEMOTION:
if selected2 is not None: # selected can be `0` so `is not None` is required
stripes_x2[selected2].y = event.pos[1] + selected_offset_y
if event.type == pygame.MOUSEBUTTONUP:
if event.button == 1:
selected2 = None
# --- objects events ---
'''
button.handle_event(event)
'''
# --- updates ---
# empty
# --- draws ---
screen.fill(white)
for i in range(width):
grid_x = k * i
grid_y = k * i
pygame.draw.line(screen, grid_color, (grid_x, 0), (grid_x, height), 1)
pygame.draw.line(screen, grid_color, (0, grid_y), (width, grid_y), 1)
pygame.draw.line(screen, black, (6 * k, height - 6 * k), (width - 6 * k, height - 6 * k), 3)
pygame.draw.line(screen, black, (6 * k, height - 6 * k), (6 * k, 0 * k), 3)
# draw rect
for r in stripes_x1:
pygame.draw.rect(screen, green, r)
for s in stripes_x2:
pygame.draw.rect(screen, green, s)
pygame.display.update()
clock.tick(60)
pygame.quit()
To move the rects simultaneously, you can put them in a list and, if one rect is selected, move them all with the help of the rel attribute of the MOUSEMOTION event (the relative position from the last mouse event). Here's a minimal example:
import sys
import pygame as pg
BLACK = pg.Color('black')
RECT_COLOR = pg.Color(78, 140, 200)
def main():
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
rect_list = [pg.Rect(100, 100+y, 20, 80) for y in range(0, 241, 120)]
selected = False
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
elif event.type == pg.MOUSEBUTTONDOWN:
for rect in rect_list:
if rect.collidepoint(event.pos):
selected = True
elif event.type == pg.MOUSEBUTTONUP:
selected = False
elif event.type == pg.MOUSEMOTION:
if selected:
for rect in rect_list:
rect.y += event.rel[1]
screen.fill(BLACK)
for rect in rect_list:
pg.draw.rect(screen, RECT_COLOR, rect)
pg.display.flip()
clock.tick(30)
if __name__ == '__main__':
pg.init()
main()
pg.quit()
sys.exit()

Why don't any of the if statements in my while loop work?

I made a while loop and .blit() works fine, but when it gets to the if statements, my loading sign appears and nothing seems to work. Am I doing my while loop incorrectly? I also want my menu1 == False to trigger the next while loop.
#Importing Stuff
import pygame
import sys
import time
import random
from pygame.locals import*
pygame.init()
#Naming Variables
menu = 0
color = (65,105,225)
tcolor = (255,255,255)
pcolor = (255,255,255)
hcolor = (255,255,255)
width, height = 1920, 1080
screen = pygame.display.set_mode((width, height))
hecolor = (255,255,255)
sys_font = pygame.font.SysFont \
("None", 60)
menu1 = True
#Initializing Screen
pygame.display.set_caption("TSA Trend Game")
screen.fill(((color)))
pygame.display.update()
#Making Menu
while 1 == 1 and menu == 0:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
#More Variables
rendered = sys_font.render \
("Welcome to Trends of 2016!", True, ((tcolor)))
play = sys_font.render \
("Play Game", True, ((pcolor)))
help = sys_font.render \
("Help", True, ((hcolor)))
play_r = play.get_rect()
play_r.x, play_r.y = 710, 500
help_r = help.get_rect()
help_r.x, help_r.y = 1170, 500
render_r = play.get_rect()
render_r.x, render_r.y = 710, 500
#Display Text
while menu1 == True:
screen.blit(rendered, (710, 440))
screen.blit(help, (1170, 500))
screen.blit(play, (710, 500))
pygame.display.update()
if render_r.collidepoint(pygame.mouse.get_pos()):
pcolor = (255,255,0)
else:
pcolor = (255,255,255)
if help_r.collidepoint(pygame.mouse.get_pos()):
hcolor = (255,255,0)
else:
hcolor = (255,255,255)
if event.type == pygame.MOUSEBUTTONDOWN and help_r.collidepoint(pygame.mouse.get_pos()):
menu1 == False
while menu1 == False:
screen.fill(color)
pygame.display.update()
pygame.display.update()
You need
= instead of == in menu1 = False
if menu1 == True instead of while menu1 == True
But you could organize code in better way (or use separated mainloop for menu, game and help).
import pygame
# --- constants --- (UPPER_CASE names)
BLACK = ( 0, 0, 0)
WHITE = (255, 255, 255)
WIDTH = 1920
HEIGHT = 1080
# --- main --- (lower_case namse)
bg_color = (65,105,225)
text_color = WHITE
play_color = WHITE
help_color = WHITE
menu_background = ( 65, 105, 225)
play_background = (255, 0, 0)
help_background = ( 0, 255, 255)
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
screen_rect = screen.get_rect()
pygame.display.set_caption("TSA Trend Game")
sys_font = pygame.font.SysFont(None, 60)
# create only once
render = sys_font.render("Welcome to Trends of 2016!", True, text_color)
render_rect = render.get_rect(x=710, y=440)
play = sys_font.render("Play Game", True, play_color)
play_rect = play.get_rect(x=710, y=500)
help = sys_font.render("Help", True, help_color)
help_rect = help.get_rect(x=1170, y=500)
other = sys_font.render("click mouse in any place", True, WHITE)
other_rect = other.get_rect(center=screen_rect.center)
# - mainloop -
state_menu = True
state_play = False
state_help = False
running = True
while running:
# - events -
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
if state_menu:
if event.type == pygame.MOUSEBUTTONDOWN:
if help_rect.collidepoint(event.pos):
print("Help")
state_menu = False
state_help = True
elif play_rect.collidepoint(event.pos):
print("Play")
state_menu = False
state_play = True
elif state_play:
if event.type == pygame.MOUSEBUTTONDOWN:
state_menu = True
state_play = False
elif state_help:
if event.type == pygame.MOUSEBUTTONDOWN:
state_menu = True
state_help = False
# - updates -
if state_menu:
mouse_pos = pygame.mouse.get_pos()
if play_rect.collidepoint(mouse_pos):
play_color = (255,255,0)
else:
play_color = WHITE
if help_rect.collidepoint(mouse_pos):
help_color = (255,255,0)
else:
help_color = WHITE
play = sys_font.render("Play Game", True, play_color)
help = sys_font.render("Help", True, help_color)
elif state_play:
pass
# do something
elif state_help:
pass
# do something
# - draws -
if state_menu:
screen.fill(menu_background)
screen.blit(render, render_rect)
screen.blit(help, help_rect)
screen.blit(play, play_rect)
elif state_play:
screen.fill(play_background)
screen.blit(other, other_rect)
elif state_help:
screen.fill(help_background)
screen.blit(other, other_rect)
pygame.display.update()
# - end -
pygame.quit()
exit()

Categories