How can I create an interactive object in pygame? - python

In a pygame program I am creating, I have need for an interactive object on the screen that will call a function when the player character moves onto it and presses the enter key. Here is the code I have so far:
import pygame
pygame.init()
screen = pygame.display.set_mode((800, 600))
done = False
x = 30
y = 30
clock = pygame.time.Clock()
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
pressed = pygame.key.get_pressed()
if pressed[pygame.K_UP] and y > 0: y -= 5
if pressed[pygame.K_DOWN] and y < 600 - 60: y += 5
if pressed[pygame.K_LEFT] and x > 0: x -= 5
if pressed[pygame.K_RIGHT] and x < 800 - 60: x += 5
screen.fill((0, 0, 0))
color = (0, 128, 255)
pygame.draw.rect(screen, color, pygame.Rect(x, y, 60, 60))
myfont = pygame.font.SysFont("monospace", 15)
label = myfont.render("Start the experiment simulator", 1, (255,255,255))
screen.blit(label, (100, 100))
label2 = myfont.render("Start the quiz", 1, (255,255,255))
screen.blit(label2, (550, 100))
label3 = myfont.render("Quit game", 1, (255,255,255))
screen.blit(label3, (350, 400))
pygame.draw.rect(screen, red, pygame.Rect(600, 125, 30, 30))
pygame.draw.rect(screen, red, pygame.Rect(225, 125, 30, 30))
pygame.draw.rect(screen, red, pygame.Rect(375, 425, 30, 30))
pygame.display.flip()
clock.tick(60)
At the moment, the object is just a test so would be best to be a small red rectangle, about half the size of the player, that I can replace with an icon later on. This rectangle should be placed below the 'quit game' label on the pygame window and quit the game when interacted with. This is one method that I have tried so far:
if pressed[pygame.K_RETURN] and x >= 375 or x <= 405 and y >=425 or y <= 455:
pygame.display.quit()
pygame.quit()
sys.exit()
Where theoretically the system checks if the user is in a specific area and has pressed the enter key before performing the command.

You can check for collision, using pygame's colliderect
First, create three rects that will represent your three option rects :
simulator_rect = pygame.Rect(600, 125, 30, 30)
quiz_rect = pygame.Rect(225, 125, 30, 30)
quit_rect = pygame.Rect(375, 425, 30, 30)
Next, we'll create a rect that will represent the blue selector rect :
selector_rect = pygame.Rect(50, 50, 60, 60)
So now you've got rects that are created only once, instead of unnamed rects that are created every time
Now, for the actual collision detection :
# Check to see if the user presses the enter key
if pressed[pygame.K_RETURN]:
# Check to see if the selection rect
# collides with any other rect
for rect in option_rects:
if selector_rect.colliderect(rect):
if rect == simulator_rect:
# Do simulations stuff!
print('Simulating!')
elif rect == quiz_rect:
# Do quizzing stuff!
print('Quizzing!')
elif rect == quit_rect:
# Quit!
done = True
Final code :
import pygame
pygame.init()
screen = pygame.display.set_mode((800, 600))
done = False
x = 30
y = 30
clock = pygame.time.Clock()
# RGB values for red
red = (255, 0 ,0)
# Your three button rects :
simulator_rect = pygame.Rect(225, 125, 30, 30)
quiz_rect = pygame.Rect(600, 125, 30, 30)
quit_rect = pygame.Rect(375, 425, 30, 30)
# These represent your three option rects
option_rects = [simulator_rect, quiz_rect, quit_rect]
# Your blue selector rect
selector_rect = pygame.Rect(50, 50, 60, 60)
# The 50, 50 xy coords are temporary
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
pressed = pygame.key.get_pressed()
if pressed[pygame.K_UP] and y > 0: y -= 5
if pressed[pygame.K_DOWN] and y < 600 - 60: y += 5
if pressed[pygame.K_LEFT] and x > 0: x -= 5
if pressed[pygame.K_RIGHT] and x < 800 - 60: x += 5
# Set the slector rect's coords to x/y
selector_rect.x, selector_rect.y = x, y
screen.fill((0, 0, 0))
color = (0, 128, 255)
pygame.draw.rect(screen, color, selector_rect)
myfont = pygame.font.SysFont("monospace", 15)
label = myfont.render("Start the experiment simulator", 1, (255,255,255))
screen.blit(label, (100, 100))
label2 = myfont.render("Start the quiz", 1, (255,255,255))
screen.blit(label2, (550, 100))
label3 = myfont.render("Quit game", 1, (255,255,255))
screen.blit(label3, (350, 400))
# Use our created rects
pygame.draw.rect(screen, red, simulator_rect)
pygame.draw.rect(screen, red, quiz_rect)
pygame.draw.rect(screen, red, quit_rect)
# Check to see if the user presses the enter key
if pressed[pygame.K_RETURN]:
# Check to see if the selection rect
# collides with any other rect
for rect in option_rects:
# Add rects as needed
if selector_rect.colliderect(rect):
if rect == simulator_rect:
# Do simulations stuff!
print('Simulating!')
elif rect == quiz_rect:
# Do quizzing stuff!
print('Quizzing!')
elif rect == quit_rect:
# Quit!
done = True
pygame.display.flip()
clock.tick(60)
This does add some complication to your program, but at least it's a rock solid method that you can add features too, and that will remain robust.

Answer to my own question, I managed to make my first attempt work by adding brackets around the x and y checks in this section:
and x >= 375 or x <= 405 and y >=425 or y <= 455:
So that it now reads:
and (x >= 375 and x <= 405) and (y >= 425 and y <= 455):

I just create a system for interactable object and it easy to scale.
listBox = [] # this list used to store all object inside
listBox.append(("text", pos, size, bgColor, textColor, InteractAction))
# add dumy object to the list, "text" can be empty if you dont want.
def InteractAction(mousePos): # sample action used to tie to object
print("do somehthing")
def newDrawBox(IableO):
pygame.draw.rect(gameDisplay, IableO[3],(IableO[1][0], IableO[1][1], IableO[2][0], IableO[2][1]))
text = basicfont.render(str(IableO[0]), True,IableO[4], None)
textrect = text.get_rect()
textrect.centerx = IableO[1][0] + IableO[2][0] / 2
textrect.centery = IableO[1][1] + IableO[2][1] / 2
gameDisplay.blit(text, textrect)
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
elif event.type == pygame.MOUSEBUTTONDOWN:
Mouse[event.button] = 1
Mouse[0] = (event.pos[0], event.pos[1])
elif event.type == pygame.MOUSEBUTTONUP:
Mouse[event.button] = 0
Mouse[0] = (event.pos[0], event.pos[1])
#-------- check if mouse is click on any object in the list of Interatable object
if Mouse[1] == 1:
for x in listBox:
if x[1][0] < Mouse[0][0] < x[1][0] + x[2][0] and x[1][1] < Mouse[0][1] < x[1][1] + x[2][1]:
x[5](Mouse[0])
#---- draw all object -----
for x in listBox:
newDrawBox(x)

Related

How to add a background and simple square as a character for a single screen platformer in pygame?

I’ve tried adding a background for the game, but as it pops up a second later it goes away It is white and everything else with the background is fine, but this is the only issue. Also, I tried to make a square to use as a character but it just won’t pop up.
import pygame, sys
from pygame.locals import QUIT
background_colour = (255, 255, 255)
BLUE = (0, 0, 255)
RED = (255, 0, 0)
(width, height) = (900, 450)
screen = pygame.display.set_mode((width, height))
screen.fill(background_colour)
pygame.display.flip()
pygame.display.update()
pygame.init()
dt = 0
x = 30
y = 30
w = 30
h = 30
a = 30
e = 30
l = 30
k = 30
def draw():
square = pygame.draw.rect(screen, RED, pygame.Rect(30, 30, 60, 60), 2)
pygame.display.flip()
def screen_bound():
global x
global y
global w
global h
global a
global e
global l
global k
# hit right wall
if ((x+w) > width):
x = width - w
# hit floor
if ((y+h) > height):
y = height - h
# hit left wall
if (x < 0):
x = 0
# hit roof
if (y < 0):
y = 0
def movement():
global x
global y
GRAVITY = .8
keys = pygame.key.get_pressed()
if keys[pygame.K_w]:
y = y - (.5*dt)
if keys[pygame.K_s]:
y = y + (.5*dt)
y = y = GRAVITY
def handle_events():
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
pass
def start():
draw()
movement()
screen_bound()
handle_events()
You need to implement an application loop where you move the objects and redraw the scene in each frame. The typical PyGame application loop has to:
limit the frames per second to limit CPU usage with pygame.time.Clock.tick
handle the events by calling 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 (blit all the objects)
update the display by calling either pygame.display.update() or pygame.display.flip()
import pygame
pygame.init()
window = pygame.display.set_mode((300, 300))
clock = pygame.time.Clock()
background = pygame.Surface(window.get_size())
ts, w, h, c1, c2 = 50, *background.get_size(), (128, 128, 128), (64, 64, 64)
tiles = [((x*ts, y*ts, ts, ts), c1 if (x+y) % 2 == 0 else c2) for x in range((w+ts-1)//ts) for y in range((h+ts-1)//ts)]
[pygame.draw.rect(background, color, rect) for rect, color in tiles]
rect = pygame.Rect(0, 0, 20, 20)
rect.center = window.get_rect().center
speed = 5
# main application loop
run = True
while run:
# limit frames per second
clock.tick(100)
# event loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
# update the game states and positions of objects dependent on the input
keys = pygame.key.get_pressed()
rect.x += (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * speed
rect.y += (keys[pygame.K_DOWN] - keys[pygame.K_UP]) * speed
border_rect = window.get_rect()
rect.clamp_ip(border_rect)
# clear the display and draw background
window.blit(background, (0, 0))
# draw the scene
pygame.draw.rect(window, (255, 0, 0), rect)
# update the display
pygame.display.flip()
pygame.quit()
exit()

How to use Pygame touch events in a mobile game?

Game Target is for android device.
When i add the buttons, they seem only to work one at a time, how can i make it so more button could work?
Here's the function:
def button_move(player_car):
pressed = pygame.mouse.get_pressed()
pos = pygame.mouse.get_pos()
moved = False
if pressed[0] == 1:
if 300 < pos[0] < 400 and 800 < pos[1] < 900:
player_car.move_forward()
moved = True
if 300 < pos[0] < 400 and 1100 < pos[1] < 1200:
player_car.move_backward()
moved = True
if 100 < pos[0] < 200 and 950 < pos[1] < 1050:
player_car.rotate(left=True)
if 500 < pos[0] < 600 and 950 < pos[1] < 1050:
player_car.rotate(right=True)
if not moved:
player_car.reduce_speed()
[...] they seem only to work one at a time [...]"
You only have one mouse. You have to use the "touch" events. Use the FINGERDOWN and FINGERUP event. Store the position of the finger into a dictionary when a FINGERDOWN event occurs and remove it on FINGERUP:
fingers = {}
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.FINGERDOWN:
x = event.x * window.get_height()
y = event.y * window.get_width()
fingers[event.finger_id] = x, y
if event.type == pygame.FINGERUP:
fingers.pop(event.finger_id, None)
# [...]
Use the positions to detect if a button is touched. Use pygame.Rect and pygame.Rect.collidepoint for the "touch" detection. e.g.:
rect = pygame.Rect(300, 800, 100, 100)
touched = False
for finger, pos in fingers.items():
if rect.collidepoint(pos):
touched = True
Minimal example:
import pygame
pygame.init()
window = pygame.display.set_mode((300, 300))
clock = pygame.time.Clock()
buttons = [
pygame.Rect(25, 25, 100, 100),
pygame.Rect(175, 25, 100, 100),
pygame.Rect(25, 175, 100, 100),
pygame.Rect(175, 175, 100, 100)]
colors = [(64, 0, 0), (64, 64, 0), (0, 64, 0), (0, 0, 64)]
colorsH = [(255, 0, 0), (255, 255, 0), (0, 255, 0), (0, 0, 255)]
fingers = {}
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.FINGERDOWN:
x = event.x * window.get_height()
y = event.y * window.get_width()
fingers[event.finger_id] = x, y
if event.type == pygame.FINGERUP:
fingers.pop(event.finger_id, None)
highlight = []
for i, rect in enumerate(buttons):
touched = False
for finger, pos in fingers.items():
if rect.collidepoint(pos):
touched = True
highlight.append(touched)
# the same with list comprehensions
#highlight = [any(r.collidepoint(p) for _, p in fingers.items()) for _, r in enumerate(buttons)]
window.fill(0)
for rect, color, colorH, h in zip(buttons, colors, colorsH, highlight):
c = colorH if h else color
pygame.draw.rect(window, c, rect)
pygame.display.flip()
pygame.quit()
exit()

How to make entities take damage with color collision in pygame?

How do I make entities take damage with colors? I made a game with enemies shooting blue lasers and I want the player to take damage. And the player shoots a red laser. Upon detecting colors, I want a certain statement to be true. That would lower an entity's health by one. If color collision is a thing, it can be very useful for making many different games. Here is my code:
# importing packages
import pygame
import random
import time
import sys
# Initializing Pygame
pygame.init()
# Setting a display caption
pygame.display.set_caption("Space Invaders")
spaceship = pygame.image.load("spaceship2.png")
blue_enemy = pygame.image.load("blue_enemy.png")
green_enemy = pygame.image.load("green_enemy.png")
orange_enemy = pygame.image.load("orange_enemy.png")
pink_enemy = pygame.image.load("pink_enemy.png")
yellow_enemy = pygame.image.load("yellow_enemy.png")
# Creating a font
pygame.font.init()
font = pygame.font.SysFont("consolas", 30)
large_font = pygame.font.SysFont("consolas", 60)
small_font = pygame.font.SysFont("consolas", 20)
# Setting a display width and height and then creating it
display_width = 700
display_height = 500
display_size = [display_width, display_height]
game_display = pygame.display.set_mode(display_size)
intro_display = pygame.display.set_mode(display_size)
# Creating a way to add text to the screen
def message(sentence, color, x, y, font_type, display):
sentence = font_type.render(str.encode(sentence), True, color)
display.blit(sentence, [x, y])
def main():
global white
global black
global clock
# Spaceship coordinates
spaceship_x = 300
spaceship_y = 375
spaceship_x_change = 0
blue_enemy_health = 5
green_enemy_health = 5
orange_enemy_health = 5
pink_enemy_health = 5
yellow_enemy_health = 5
# Clock making
# Initializing pygame
pygame.init()
# Creating colors
red = (0, 0, 0)
blue = (0, 0, 255)
# clock stuff
clock = pygame.time.Clock()
time_elapsed_since_last_action = 0
time_elapsed_since_last_action2 = 0
time_elapsed_since_last_action3 = 0
# Creating a loop to keep program running
while True:
laser_rect = pygame.Rect(spaceship_x + 69, 70, 4, 310)
# --- Event Processing and controls
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
spaceship_x_change = 10
elif event.key == pygame.K_LEFT:
spaceship_x_change = -10
elif event.key == pygame.K_r:
red = (255, 0, 0)
elif time_elapsed_since_last_action2 > 250:
pygame.draw.rect(game_display, red, laser_rect)
time_elapsed_since_last_action2 = 0
elif event.type == pygame.KEYUP:
spaceship_x_change = 0
red = (0, 0, 0)
spaceship_x += spaceship_x_change
# Preventing the ship from going off the screen
if spaceship_x > display_width - 140:
spaceship_x -= 10
if spaceship_x < 1:
spaceship_x += 10
# Setting Display color
game_display.fill(black)
laser_coords = [70, 209, 348, 505, 630]
random_x_coord = random.choice(laser_coords)
dt = clock.tick()
time_elapsed_since_last_action += dt
if time_elapsed_since_last_action > 500:
pygame.draw.rect(game_display, blue, [random_x_coord, 85, 6, 305])
time_elapsed_since_last_action = 0
time_elapsed_since_last_action2 += dt
# Creating a spaceship, lasers, and enemies
game_display.blit(spaceship, (spaceship_x, spaceship_y))
message(str(blue_enemy_health), white, 65, 10, font, game_display)
game_display.blit(blue_enemy, (20, 25))
message(str(green_enemy_health), white, 203, 10, font, game_display)
game_display.blit(green_enemy, (160, 25))
message(str(orange_enemy_health), white, 341, 10, font, game_display)
game_display.blit(orange_enemy, (300, 25))
message(str(pink_enemy_health), white, 496, 10, font, game_display)
game_display.blit(pink_enemy, (440, 25))
message(str(yellow_enemy_health), white, 623, 10, font, game_display)
game_display.blit(yellow_enemy, (580, 25))
health = 10
message("Spaceship durability: " + str(health), white, 20, 480, small_font, game_display)
# Updating Screen so changes take places
pygame.display.update()
# Setting FPS
FPS = pygame.time.Clock()
FPS.tick(60)
black = (0, 0, 0)
white = (255, 255, 255)
gray = (100, 100, 100)
while True:
intro_display.fill(black)
pygame.event.poll()
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
message("Space Bugs", white, 180, 150, large_font, intro_display)
if 150 + 100 > mouse[0] > 150 and 350 + 50 > mouse[1] > 350:
pygame.draw.rect(game_display, gray, [150, 350, 100, 50])
if click[0] == 1:
break
else:
pygame.draw.rect(game_display, white, [150, 350, 100, 50])
if 450 + 100 > mouse[0] > 450 and 350 + 50 > mouse[1] > 350:
pygame.draw.rect(game_display, gray, [450, 350, 100, 50])
if click[0] == 1:
pygame.quit()
quit()
else:
pygame.draw.rect(game_display, white, [450, 350, 100, 50])
message("Start", black, 155, 360, font, intro_display)
message("Quit", black, 465, 360, font, intro_display)
# Go ahead and update the screen with what we've drawn.
pygame.display.update()
# Wrap-up
# Limit to 60 frames per second
clock = pygame.time.Clock()
clock.tick(60)
# Executing the function
if __name__ == "__main__":
main()
No, there is no such a "thing" as a "color collision". What color have the images? Are they uniformly colored? Most likely the pictures are only different in some details. A collision which checks for a uniform color is completely useless. A collision that also looks for a uniform color is almost useless.
If you detect a collision, you know the laser and the enemy. Hence, you know the color with which you represent them. All you have to do is do an additional check after the collision is detected.
Use pygame.sprite.Sprite to implement the objects. Add an attribute that indicates the color of the object:
class ColoredSprite(pygame.sprite.Sprite):
def __init__(self, x, y, image, color):
super().__init__()
self.image = image
self.rect = self.image.get_rect(center = (x, y)
slef.color = color
Manage the spites in pygame.sprite.Groups and use spritecollide to detect the collisions. Check the color attributes when a collision is detected
collide_list = sprite.spritecollide(sprite_group, False):
for other_sprite in collide_list:
if sprite.color == 'red' and other_sprite.color == 'blue':
# [...]
elif sprite.color == 'blue' and other_sprite.color == 'red':
# [...]
It is even possible to implement your own collision detection method and use it in combination with spritecollide by setting the optional collided argument:
def color_collision(sprite1, sprite2):
if sprite1.rect.colliderect(sprite2.rect):
return ((sprite1.color == 'red' and sprite2.color == 'blue') or
(sprite1.color == 'blue' and sprite2.color == 'red'))
return false
collide_list = sprite.spritecollide(sprite_group, False, color_collision):
for other_sprite in collide_list:
# [...]
Minimal example:
import pygame
pygame.init()
window = pygame.display.set_mode((250, 250))
sprite1 = pygame.sprite.Sprite()
sprite1.image = pygame.Surface((75, 75))
sprite1.image.fill('red')
sprite1.rect = pygame.Rect(*window.get_rect().center, 0, 0).inflate(75, 75)
sprite1.color = 'red'
sprite2 = pygame.sprite.Sprite()
sprite2.image = pygame.Surface((75, 75))
sprite2.image.fill('blue')
sprite2.rect = pygame.Rect(*window.get_rect().center, 0, 0).inflate(75, 75)
sprite2.color = 'blue'
all_group = pygame.sprite.Group([sprite2, sprite1])
def color_collision(sprite1, sprite2):
if sprite1.rect.colliderect(sprite2.rect):
return ((sprite1.color == 'red' and sprite2.color == 'blue') or
(sprite1.color == 'blue' and sprite2.color == 'red'))
return False
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
sprite1.rect.center = pygame.mouse.get_pos()
collide = pygame.sprite.spritecollide(sprite1, all_group, False, color_collision)
window.fill(0)
all_group.draw(window)
for s in collide:
pygame.draw.rect(window, (255, 255, 255), s.rect, 5, 1)
pygame.display.flip()
pygame.quit()
exit()

How to find the button on the entered position by user and change it's color in pygame?

import pygame
pygame.init()
width = 800
height = 600
running = True
screen = pygame.display.set_mode((width, height))
def drawGrid():
for y in range (50):
for x in range (50):
pygame.draw.rect (screen , ( 0 , 0 , 255 ) , ( ( x * 20 ) , ( y * 20 ) , 20 , 20 ) , 1)
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONUP:
pos = event.pos
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill((0, 0, 0))
drawGrid()
pygame.display.update()
So I basically create a grid and I also figured out how to get the position at which the user clicks the screen. What I want to do now is that using the position which the user has clicked, I want to find the corresponding button at that location. Once I find which button corresponds to the position at which the user clicked, I want to then change the color of the button to something else.
Edit:
import pygame
pygame.init()
width = 800
height = 600
running = True
screen = pygame.display.set_mode((width, height))
cell_size = 20
size_x = width // cell_size
size_y = height // cell_size
grid = [[False for y in range(size_y)] for x in range(size_x)]
def drawGrid():
mouse_pos = pygame.mouse.get_pos()
for y in range (size_y):
for x in range (size_x):
# rectangle and color of cell
cell_rect = pygame.Rect(x * 20, y * 20, 20, 20)
cell_color = (0, 0, 255)
# change color of button if the mouse is on the button
if cell_rect.collidepoint(mouse_pos):
cell_color = (255, 255, 255)
elif grid[x][y]:
cell_color = (0, 255, 255)
pygame.draw.rect(screen, cell_color, cell_rect, 1)
z = 0
while running and z < 2:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
cell_x, cell_y = event.pos[0] // cell_size, event.pos[1] // cell_size
grid[cell_x][cell_y] = not grid[cell_x][cell_y]
z += 1
screen.fill((0, 0, 0))
drawGrid()
pygame.display.update()
So basically what I want to do is that I want to stop clicking a rectangle in after two rectangles. So like if the user has already clicked two boxes (starting point and ending point), then I want to no longer add more boxes whenever the user clicks over a rectangle. I got it to work but the issue is that I only got it to work when two clicks are done the whole window closes. How can I fix this?
Never get the events (pygame.event.get()) multiple times in the application loop.
Use pygame.mouse.get_pos() to get the position of the mouse. Create a pygame.Rect object with the size of the cell and us collidepoint() to evaluate if the mouse is in the cell.
def drawGrid():
# get current mouse position
mosue_pos = pygame.mouse.get_pos()
for y in range (50):
for x in range (50):
# rectangle and color of cell
cell_rect = pygame.Rect(x * 20, y * 20, 20, 20)
cell_color = (0, 0, 255)
# change color of button if the mouse is on the button
if cell_rect.collidepoint(mosue_pos):
cell_color = (255, 255, 255)
pygame.draw.rect(screen, cell_color, cell_rect, 1)
If you want to change the cell on click, then you have to create a grid of states:
grid = [[False for y in range(size_y)] for x in range(size_x)]
Change the state of the cell when clicked on it:
if event.type == pygame.MOUSEBUTTONDOWN:
cell_x, cell_y = event.pos[0] // cell_size, event.pos[1] // cell_size
grid[cell_x][cell_y] = not grid[cell_x][cell_y]
Set the color dependent on the state of the cell:
if grid[x][y]:
cell_color = (255, 255, 255)
See the example:
import pygame
pygame.init()
width = 800
height = 600
running = True
screen = pygame.display.set_mode((width, height))
cell_size = 20
size_x = width // cell_size
size_y = height // cell_size
grid = [[False for y in range(size_y)] for x in range(size_x)]
def drawGrid():
for y in range(size_y):
for x in range(size_x):
# rectangle and color of cell
cell_rect = pygame.Rect(x * 20, y * 20, 20, 20)
cell_color = (0, 0, 255)
# change color of button if the mouse is on the button
if grid[x][y]:
cell_color = (255, 255, 255)
pygame.draw.rect(screen, cell_color, cell_rect, 1)
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
cell_x, cell_y = event.pos[0] // cell_size, event.pos[1] // cell_size
grid[cell_x][cell_y] = not grid[cell_x][cell_y]
screen.fill((0, 0, 0))
drawGrid()
pygame.display.update()
If you want to prevent to select cells, after 2 boxes have been clicked, then you have to evaluate the number of selected cells in the MOUSEBUTTONDOWN event:
z = 0
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
if z < 2:
cell_x, cell_y = event.pos[0] // cell_size, event.pos[1] // cell_size
grid[cell_x][cell_y] = not grid[cell_x][cell_y]
z += 1 if grid[cell_x][cell_y] else -1
# [...]

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()

Categories

Resources