Pygame making a selection like in stategy games - pygame

So I made this in order to select an area like in strategy games, however
the screen keeps blinking, is there a way to solve this?
import pygame
from pygame.locals import *
WHITE = (255,255,255)
BLUE = (0,0,255)
pygame.init()
window = pygame.display.set_mode((640, 480))
window.fill(WHITE)
pygame.display.flip()
LEFT_CLIC = 1
mouse_tracking = False
draw_area = False
while True:
for event in pygame.event.get():
if event.type == QUIT:
continuer = 0
if event.type == MOUSEBUTTONDOWN:
if event.button == LEFT_CLIC:
x_start, y_start = event.pos
x_end, y_end = event.pos
mouse_tracking = True
draw_area = True
if event.type == MOUSEMOTION and mouse_tracking:
x_end, y_end = event.pos
if event.type == MOUSEBUTTONUP:
if event.button == LEFT_CLIC:
x_end, y_end = event.pos
mouse_tracking = True
draw_area = False
if draw_area:
width = x_end-x_start
height = y_end-y_start
pygame.draw.rect(window, BLUE, (x_start, y_start, width, height))
pygame.display.flip()
window.fill(WHITE)
pygame.display.flip()
So it's pretty simple, record coordinates when there is a clic, then follow the mouse until the clic is done.
Thanks.

There should be only one pygame.display.flip() call per frame, otherwise you get this flickering, so remove one of them. Also, fill the screen before you draw the rect.
window.fill(WHITE)
if draw_area:
width = x_end-x_start
height = y_end-y_start
pygame.draw.rect(window, BLUE, (x_start, y_start, width, height))
pygame.display.flip()

Related

How to stop cursor lag in Pygame?

I have created a shooting game using Pygame in which one's cursor turns into a cross-hair and moves around shooting static images. However, the lag on the cross-hair is simply unbearable as the image is continually redrawn and a new background imposed over it every clock tick.
My code is as below:
import pygame, sys, random
class Crosshair(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load("ch.png").convert_alpha()
self.rect = self.image.get_rect()
self.gunshot = pygame.mixer.Sound("gs.wav")
def shoot(self):
self.gunshot.play()
pygame.sprite.spritecollide(crosshair, target_group, True)
def update(self):
self.rect.center = pygame.mouse.get_pos()
class Target(pygame.sprite.Sprite):
def __init__(self, pos_x, pos_y):
super().__init__()
self.image = pygame.image.load("al.png").convert_alpha()
self.rect = self.image.get_rect()
self.rect.center = [pos_x, pos_y]
pygame.init()
clock = pygame.time.Clock()
screen_width = 1920
screen_height = 1080
screen = pygame.display.set_mode((screen_width, screen_height))
background = pygame.image.load("hoc.png").convert_alpha()
pygame.mouse.set_visible(False)
crosshair = Crosshair()
crosshair_group = pygame.sprite.Group()
crosshair_group.add(crosshair)
target_group = pygame.sprite.Group()
for target in range(20):
new_target = Target(random.randrange(0, screen_width), random.randrange(0, screen_height))
target_group.add(new_target)
Running = True
while Running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
crosshair.shoot()
pygame.display.flip()
screen.blit(background, (0, 0))
target_group.draw(screen)
crosshair_group.draw(screen)
crosshair_group.update()
clock.tick(60)
Is there any way to make the cross-hair motion smoother? Most of the code is taken almost directly from a Youtube tutorial, but for some reason I am experiencing this problem when others are not.
It is a matter of indentation. You must update and draw the scene in the application loop rather than the event loop. The application loop is executed once per frame, but the event loop is only executed when an event occurse. Actually the call of clock.tick(60) slows down your application after each event instead of each frame:
while Running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
crosshair.shoot()
# INDENTATION
#<--|
pygame.display.flip()
screen.blit(background, (0, 0))
target_group.draw(screen)
crosshair_group.draw(screen)
crosshair_group.update()
clock.tick(60)

Text box render on top of main code in pygame?

I have implemented text box code to my simulation but now it is not rendering the main code, or rendering on top of my main code so nothing is displayed but the box :(
Here is my text box code :
def textbox(font20):
input_box = pygame.Rect(300,225,400,100)
color_inactive =(TURQUOISE)
color_active = (0,255,249)
color = color_inactive
active = False
text = ""
done = False
font = font20
while not done:
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
if input_box.collidepoint(event.pos):
active = not active
else:
active = False
color = color_active if active else color_inactive
if event.type == pygame.KEYDOWN:
if active:
if event.key == pygame.K_RETURN:
print(text)
text = ""
done = True
elif event.key == pygame.K_BACKSPACE:
text = text[:-1]
else:
text += event.unicode
# Render the current text.
text_surface = font.render(text, True, color)
# Resize the box if the text is too long.
width = max(200, text_surface.get_width()+10)
input_box.w = width
# Blit the text.
screen.blit(text_surface, (input_box.x+5, input_box.y+5))
# Blit the input_box rect.
pygame.draw.rect(screen, color, input_box, 2)
pygame.display.flip()
I also screen.blit to turquoise at the bottom where I draw the screen and flip it.
Did you remember to click in the box? ;)
There's also a bunch of minor issues:
The code doesn't have a definition for TURQUOISE.
The loop didn't handle the pygame.QUIT event.
It also needs to propagate this event to the outer-loop
As the box grew, it made a mess, I added a black rectangle to erase the old.
It did not return the text value.
With all these, quite minor, bugs fixed, it works pretty well. But just remember to click in the box first!
import pygame
# Window size
WINDOW_WIDTH = 400
WINDOW_HEIGHT = 400
### initialisation
pygame.init()
screen = pygame.display.set_mode( ( WINDOW_WIDTH, WINDOW_HEIGHT ) )
pygame.display.set_caption("Text Box Test")
BLACK = (0,0,0)
TURQUOISE = (0,154,196)
CYAN = (0,255,249)
def textbox(font20):
#input_box = pygame.Rect(300,225,400,100)
input_box = pygame.Rect(10,10,400,100)
color_background = BLACK # for erasing
color_inactive = TURQUOISE
color_active = CYAN
color = color_inactive
active = False
text = ""
done = False
font = font20
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.event.post( pygame.event.Event( pygame.QUIT ) ) # re-send the quit event to the next loop
done = True
elif event.type == pygame.MOUSEBUTTONDOWN:
if input_box.collidepoint(event.pos):
active = not active
else:
active = False
color = color_active if active else color_inactive
elif event.type == pygame.KEYDOWN:
if active:
if event.key == pygame.K_RETURN:
print(text)
#text = ""
done = True
elif event.key == pygame.K_BACKSPACE:
text = text[:-1]
else:
text += event.unicode
print( "DEBUG: text is now [%s]" % ( text ) )
# Render the current text.
text_surface = font.render(text, True, color)
# Resize the box if the text is too long.
width = max(200, text_surface.get_width()+10)
input_box.w = width
# Blit the input_box rect.
pygame.draw.rect(screen, color_background, input_box) # filled box to clear the old one
pygame.draw.rect(screen, color, input_box, 2)
# Blit the text.
screen.blit(text_surface, (input_box.x+5, input_box.y+5))
pygame.display.flip()
return text
font = pygame.font.SysFont('', 24)
textbox( font )
pygame.quit()

Change colour of sprite rect from group

I have a group of rects, they display in a row. I want them to change their colour when they have been clicked, until they are clicked again
I have this code so far to create the sprites:
class DrawableRect(pygame.sprite.Sprite):
def __init__(self,color,width,height,value=0):
super().__init__()
self.image = pygame.Surface([width, height])
self.image.fill(color)
self.rect = self.image.get_rect()
self.value = value
self.x = 0
self.y = 0
def change_value(self,color,value):
self.image.fill(color)
self.value=value
def DrawRects(start_x, start_y, rect_spacing, colour_list):
current_x_pos = start_x
for rect_num in range(0,8):
rect = DrawableRect(colour_list[rect_num], boxW, boxH)
rect.rect.x = current_x_pos
rect.rect.y = start_y
current_x_pos = current_x_pos + rect.rect.width + rect_spacing
rects.add(rect)
rects.draw(screen)
The idea of the app is for each rectangle to represent a bit, and when pressed it alternates between 0 and 1, the makeup of each bit displays the decimal equivalent somewhere.
I read that groups are unordered therefore indexing wouldn't work, is that true?
Here's an example I've modified to suit your purposes. I have a bunch of sprites (coloured rectangles) in a sprite group and I change* the colour of any sprite that collides with the mouse pointer when a mouse button is pressed.
Here's the code, you're probably most interested in the change_color() method and the MOUSEBUTTONUP event handling code.
import random
import pygame
screen_width, screen_height = 640, 480
def get_random_position():
"""return a random (x,y) position in the screen"""
return (random.randint(0, screen_width - 1), #randint includes both endpoints.
random.randint(0, screen_height - 1))
color_list = ["red", "orange", "yellow", "green", "cyan", "blue", "blueviolet"]
colors = [pygame.color.Color(c) for c in color_list]
class PowerUp(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
width, height = 64, 32
self.image = pygame.Surface([width, height])
self.clicked = False # track whether we've been clicked or not
# initialise color
self.color = random.choice(colors)
self.image.fill(self.color)
# Fetch the rectangle object that has the dimensions of the image
self.rect = self.image.get_rect()
# then move to a random position
self.update()
def update(self):
#move to a random position
self.rect.center = get_random_position()
def random_color(self):
# randomise color
self.clicked = not self.clicked
if self.clicked:
color = random.choice(colors)
else:
color = self.color
self.image.fill(color)
if __name__ == "__main__":
pygame.init()
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('Sprite Color Switch Demo')
clock = pygame.time.Clock() #for limiting FPS
FPS = 60
exit_demo = False
pygame.key.set_repeat(300, 200)
#create a sprite group to track the power ups.
power_ups = pygame.sprite.Group()
for _ in range(10):
power_ups.add(PowerUp()) # create a new power up and add it to the group.
# main loop
while not exit_demo:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit_demo = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
exit_demo = True
elif event.key == pygame.K_SPACE:
power_ups.update()
elif event.type == pygame.MOUSEBUTTONUP:
# check for collision
for p in power_ups:
if p.rect.collidepoint(event.pos): # maybe use event?
p.random_color()
screen.fill(pygame.Color("black")) # use black background
power_ups.draw(screen)
pygame.display.update()
clock.tick(FPS)
pygame.quit()
quit()
Let me know if you have any questions. Obviously this doesn't do row alignment of the sprites, I think you have a handle on that. I would suggest that you have all of your screen drawing operations in one place so your code can be clearer.
*The new colour is randomised from a short list, so there's a 14% chance it won't change from the starting colour.

pygame, getting a sprite to move to cursor position on click

I was trying to make a tower defense sort of game in pygame. When I click on the screen, I want it to be able to move the tower sprite to the position. Currently, when I run the program, it moves the sprite to the cursor with out clicking, then does not let me move it again.
import pygame
import time
pygame.init()
WINDOWWIDTH = 1800
WINDOWHEIGHT = 1800
screen = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
background_colour = (188,69,80)
GAMETITLE = "Tower Defence"
def main():
pygame.display.set_caption(GAMETITLE)
clock = pygame.time.Clock()
spritegroup =pygame.sprite.Group()
sprite =pygame.sprite.Sprite()
tower = sprite.image = pygame.image.load("tower.png")
sprite.image = tower
sprite.rect = sprite.image.get_rect()
sprite.rect.x = 10
sprite.rect.y = 10
sprite.add(spritegroup)
while True:
screen.fill(background_colour)
pygame.draw.rect(screen, (255,255,255), ((0, 100), (1100, 90)))
pygame.draw.rect(screen, (255, 255,255), ((1010, 100), (100, 600)))
pygame.draw.rect(screen, (255,255,255), ((1010, 700), (2400, 90)))
spritegroup.draw(screen)
pygame.display.update()
clock.tick(30)
if pygame.mouse.get_pressed():
cursorPos = pygame.mouse.get_pos()
sprite.rect.x = cursorPos[0]
sprite.rect.y = cursorPos[1]
main()
what is happening is that pygame.mouse.get_pressed() returns a tuple. if statements return true if they are not null. this means that regardless if it is pressed it will return True. then the pygame event queue will fill with mouse events and never get processed. your event loop should look something like:
for event in pygame.event.get():
if event.type == pygame.event.QUIT:
pygame.quit()
if event.type == pygame.MOUSEBUTTONDOWN:
#stuff to do on click

How to create a brush with pygame?

I'm trying to create a brush on pygame. It should draw rectangles while the left mouse button is down.
Below is what I tried:
while not game_exit:
if event.type == pygame.MOUSEBUTTONDOWN and pygame.mouse.get_pressed()[0]:
while pygame.mouse.get_pressed()[0]:
pygame.draw.rect(gameDisplay, red, [pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1], 10, 10])
But it is now working. Where is the problem?
If you need to draw rectangle when mouse button is pressed (and mouse is moving) then use
import pygame
RED = (255,0,0)
BLACK = (0,0,0)
gameDisplay = pygame.display.set_mode((600,400))
brush = None
game_exit = False
while not game_exit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_exit = True
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1: # left button pressed
brush = event.pos
elif event.type == pygame.MOUSEBUTTONUP:
if event.button == 1: # left button released
brush = None
elif event.type == pygame.MOUSEMOTION:
if brush: # left button still pressed
brush = event.pos
# clear bufor
gameDisplay.fill(BLACK)
# draw brush in bufor
if brush:
pygame.draw.rect(gameDisplay, RED, [brush[0], brush[1], 10, 10])
# send bufor on the screen
pygame.display.flip()
Here is a stable loop that fills, flips and receives events:
while True:
for event in pregame.event.get():
if event.type == pregame.QUIT:
pregame.quit()
window.fill((0,0,0))
#####Render here
pygame.display.flip()
pygame.time.Clock().tick(30)
Hope it helps :D