Answer the question
In order to leave comments, you need to log in
How to make a collision reaction in pygame?
How to make a collision reaction in pygame?
in my game:
import pygame
import pygame as pg
import sys
import ctypes
from random import *
import time
import tkinter as tk
from PIL import ImageGrab
FPS = 60
W = 700 # ширина экрана
H = 300 # высота экрана
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
BLUE = (0, 70, 225)
BLACK = (0, 0, 0)
RED = (220, 0, 0)
DARKGRAY = (40, 40, 40)
PURPLE = (120, 0, 120)
BROWN = (148, 19, 19)
ORANGE = (255, 179, 0)
pg.init()
sc = pygame.display.set_mode((W, H))
дождь = True
сила_дождя = 0.1
pygame.mixer.music.load(r'C:\\дождь.ogg')
pygame.mixer.music.play()
t1 = time.time()
pygame.mixer.music.set_volume(сила_дождя)
clock = pygame.time.Clock()
pygame.display.set_caption("circle!")
кув = pygame.image.load("C:\спрайти\кув.bmp")
pygame.display.set_icon(кув)
import inspect, os.path
sc.blit(кув, (0, 0))
exec('''
surf = pg.Surface((25, 80))
surf.fill((252, 185, 0))
surf.set_alpha(130)''')
CARS = ('C:\\спрайти\капля.png', 'C:\\спрайти\капля.png', 'C:\\спрайти\капля.png')
# для хранения готовых машин-поверхностей
CARS_SURF = []
CARSу = ('C:\\спрайти\дим.png', 'C:\\спрайти\дим.png', 'C:\\спрайти\дим.png')
# для хранения готовых машин-поверхностей
CARSу_SURF = []
дерево = 'C:\\дерево pygame.bmp'
# надо установить видео режим
# до вызова image.load()
for i in range(len(CARS)):
CARS_SURF.append(
pg.image.load(CARS[i]).convert_alpha())
for i in range(len(CARSу)):
CARSу_SURF.append(
pg.image.load(CARSу[i]).convert_alpha())
class Car(pg.sprite.Sprite):
def __init__(self, x, surf, group):
pg.sprite.Sprite.__init__(self)
self.image = surf
self.rect = self.image.get_rect(
center=(x, 0))
# добавляем в группу
self.add(group)
# у машин будет разная скорость
self.speed = randint(1, 3)
def update(self):
if self.rect.y < H:
self.rect.y += self.speed
else:
# теперь не перебрасываем вверх,
# а удаляем из всех групп
self.kill()
class Дерево():
def __init__(self, sceen, x=600, y=150):
self.x = x
self.y = y
self.bmp = "C:\\дерево pygame.bmp"
self.bmp_дерево = pygame.image.load("C:\\спрайти\\дерево pygame.bmp")
self.bmp_дерево.set_colorkey((255, 255, 255))
self.bmp_image = self.bmp_дерево.get_rect(center=(x, y))
sceen.blit(self.bmp_дерево, self.bmp_image)
self.cords = {"x1":x-79, "x2":x+20+53, "y1":y-25, "y2":y+50+46}
cars = pg.sprite.Group()
# добавляем первую машину,
# которая появляется сразу
Car(randint(1, W),
CARS_SURF[randint(0, 2)], cars)
# координаты и радиус круга
x = W // 2
y = H // 2
r = 50
pygame.mouse.set_visible(False)
час_дошю = 0
button = pygame.Rect(200, 100, 150, 50)
вогонь = [False]
IMG1 = pg.Surface((100, 100))
IMG1.fill((240, 240, 240))
img_rect = IMG1.get_rect(topleft=(200, 200))
f2 = pygame.font.SysFont('serif', 48)
text2 = f2.render("World Мир", False,
ORANGE)
sc.blit(text2, (10, 100))
час_дошю = 0.3
start = True
def start2():
if start:
root = tk.Tk()
def save():
exec('''
x0 = root.winfo_rootx()
y0 = root.winfo_rooty()
x1 = x0 + root.winfo_width()
y1 = y0 + root.winfo_height()
try:
ImageGrab.grab().crop((x0, y0, x1, y1)).save("C:\\спрайти\\red2.png")
pygame.image.save_extended(sc, "C:\\спрайти\\red2.png")
except:
pass
start = False''')
s = tk.Entry(root)
s2 = tk.Button(root, text='save', command=lambda:save())
while 1:
sc.fill((110, 180, 110))
for i in pygame.event.get():
if i.type == pygame.QUIT:
pygame.quit()
sys.exit()
if i.type == pygame.MOUSEBUTTONDOWN:
mouse_pos = i.pos
print(str(button.collidepoint(mouse_pos))+" "+str(mouse_pos))
if img_rect.collidepoint(i.pos):
if вогонь[0] == True:
вогонь[0] = False
print(вогонь[0])
elif вогонь[0] == False:
вогонь[0] = True
print(вогонь[0])
red = '''
if mouse_pos[0] >= 555 and mouse_pos[0] <= 426 and mouse_pos[1] <= 158 and mouse_pos[1] >= 124:
вогонь[0] = True
print(вогонь[0])'''
дерево1 = Дерево(sc, x=550, y=120)
pygame.draw.circle(sc, BLUE, (x, y), r)
keys = pygame.key.get_pressed()
IMG1 = pg.Surface((100, 100))
IMG1.fill((240, 240, 240))
img_rect = IMG1.get_rect(topleft=(10, 200))
red = '''
button = pygame.Rect(400, 160, 100, 20)'''
button = pygame.Rect(200, 100, 150, 50)
pygame.draw.rect(sc, BROWN, img_rect)
pygame.draw.rect(sc, BROWN, (400, 160, 100, 20))
red = '''
pygame.draw.rect(sc, BROWN, (400, 160, 100, 20), button)'''
f2 = pygame.font.SysFont('serif', 40)
text2 = f2.render("огонь", False, ORANGE)
sc.blit(text2, (10, 220))
# фото
IMG2 = pg.Surface((100, 100))
IMG2.fill((240, 240, 240))
img_rect2 = IMG2.get_rect(topleft=(120, 200))
red = '''
button = pygame.Rect(400, 160, 100, 20)'''
pygame.draw.rect(sc, BROWN, img_rect2)
red = '''
pygame.draw.rect(sc, BROWN, (400, 160, 100, 20), button)'''
f1 = pygame.font.SysFont('serif', 40)
text1 = f1.render("фото", False, BLUE)
sc.blit(text1, (120, 220))
#фото
if not дождь:
if вогонь[0]:
# создание огня
ted = []
ted.append(sc.blit(surf, (400, 100)))
ted.append(sc.blit(surf, (420, 80)))
ted.append(sc.blit(surf, (420, 100)))
ted.append(sc.blit(surf, (436, 97)))
ted.append(sc.blit(surf, (405, 120)))
ted.append(sc.blit(surf, (438, 120)))
ted.append(sc.blit(surf, (455, 120)))
#создание огня
else:
ted = []
pygame.mixer.music.pause()
сила_дождя = 0
else:
сила_дождя = 0.5
Car(randint(1, W), CARS_SURF[randint(0, 2)], cars)
cars.draw(sc)
if (time.time() - t1)/60 >= час_дошю:
дождь = False
вогонь[0] = False
if keys[pygame.K_LEFT]:
if x <= 47 and W == 700 or ?:
pass
else:
x -= 3
elif keys[pygame.K_RIGHT]:
if x >= 647 and W == 700 or ?:
pass
else:
x += 3
if keys[pygame.K_DOWN]:
if y >= 246 and H == 300 or ?:
pass
else:
y += 3
elif keys[pygame.K_UP]:
if y <= 51 and H == 300 or ?:
pass
else:
y -= 3
if keys[pygame.K_SPACE]:
print("x:"+str(x)+" "+"y:"+str(y))
button = pygame.Rect(100, 100, 50, 50)
pressed = pygame.mouse.get_pressed()
pos = pygame.mouse.get_pos()
pygame.draw.rect(sc, GREEN, (pos[0] - 10, pos[1] - 10, 20, 20))
pygame.display.update()
pygame.time.delay(20)
cars.update()
clock.tick(FPS)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question