Короче я писал пинг-понг и дело в том что функция colliderect() засчитывает коллизию в том месте где доска стояла во время открытия окна.
import sys
import pygame
import random
import cmath
import math
WIDTH = 600
HEIGHT = 480
FPS = 60
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
gamestart = 0
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Shmup!")
clock = pygame.time.Clock()
class Player1(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((20, 90))
self.image.fill(GREEN)
self.rect = self.image.get_rect()
self.rect.centerx = WIDTH
self.rect.centery = HEIGHT/2
self.speedy = 0
def update(self):
self.speedy = 0
keystate = pygame.key.get_pressed()
if keystate[pygame.K_DOWN]:
self.speedy = 8
if keystate[pygame.K_UP]:
self.speedy = -8
self.rect.y += self.speedy
if self.rect.top < 0:
self.rect.top = 0
if self.rect.bottom > HEIGHT:
self.rect.bottom = HEIGHT
class Player2(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((20, 90))
self.image.fill(GREEN)
self.rect = self.image.get_rect()
self.rect.centerx = 0
self.rect.centery = HEIGHT/2
self.speedy = 0
def update(self):
self.speedy = 0
keystate = pygame.key.get_pressed()
if keystate[pygame.K_s]:
self.speedy = 8
if keystate[pygame.K_w]:
self.speedy = -8
self.rect.y += self.speedy
if self.rect.top < 0:
self.rect.top = 0
if self.rect.bottom > HEIGHT:
self.rect.bottom = HEIGHT
class Ball(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((20, 20))
self.image.fill(WHITE)
self.rect = self.image.get_rect()
self.rect.y = 0
self.rect.x = 0
self.rect.centerx = WIDTH / 2
self.rect.centery = HEIGHT / 2
self.speedy = 0
self.speedx = -4
self.speed = 4
def update(self):
if gamestart == 1:
if self.rect.top < 0:
self.speedy = self.speedy * -1
self.rect.y += self.speedy
if self.rect.bottom > HEIGHT:
self.speedy = self.speedy * -1
self.rect.y += self.speedy
if self.rect.colliderect(Player2().rect):
self.speedx = self.speedx * -1
self.rect.x += self.speedx
if self.rect.colliderect(Player1().rect):
self.speedx = self.speedx * -1
self.rect.x += self.speedx
self.rect.y += self.speedy
self.rect.x += self.speedx
all_sprites = pygame.sprite.Group()
player1 = Player1()
player2 = Player2()
ball = Ball()
all_sprites.add(player1)
all_sprites.add(player2)
all_sprites.add(ball)
running = True
while running:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
gamestart = 1
all_sprites.update()
screen.fill(BLACK)
all_sprites.draw(screen)
pygame.display.flip()
pygame.quit()
Здесь код где я переписал колизию проверкой по координатам и тут происходит тоже самое
import sys
import pygame
import random
import cmath
import math
WIDTH = 600
HEIGHT = 480
FPS = 60
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
gamestart = 0
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Shmup!")
clock = pygame.time.Clock()
class Player1(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((20, 90))
self.image.fill(GREEN)
self.rect = self.image.get_rect()
self.rect.centerx = WIDTH
self.rect.centery = HEIGHT/2
self.speedy = 0
def update(self):
self.speedy = 0
keystate = pygame.key.get_pressed()
if keystate[pygame.K_DOWN]:
self.speedy = 8
if keystate[pygame.K_UP]:
self.speedy = -8
self.rect.y += self.speedy
if self.rect.top < 0:
self.rect.top = 0
if self.rect.bottom > HEIGHT:
self.rect.bottom = HEIGHT
def data(self,nom):
data = [self.rect.x,self.rect.y]
return data[nom]
class Player2(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((20, 90))
self.image.fill(GREEN)
self.rect = self.image.get_rect()
self.rect.centerx = 0
self.rect.centery = HEIGHT/2
self.speedy = 0
def update(self):
self.speedy = 0
keystate = pygame.key.get_pressed()
if keystate[pygame.K_s]:
self.speedy = 8
if keystate[pygame.K_w]:
self.speedy = -8
self.rect.y += self.speedy
if self.rect.top < 0:
self.rect.top = 0
if self.rect.bottom > HEIGHT:
self.rect.bottom = HEIGHT
def data(self,nom):
data = [rect.x,rect.y]
return data[nom]
class Ball(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((20, 20))
self.image.fill(WHITE)
self.rect = self.image.get_rect()
self.rect.y = 0
self.rect.x = 0
self.rect.centerx = WIDTH / 2
self.rect.centery = HEIGHT / 2
self.speedy = 0
self.speedx = -4
self.speed = 4
def update(self):
if gamestart == 1:
if self.rect.top < 0:
self.speedy = self.speedy * -1
self.rect.y += self.speedy
if self.rect.bottom > HEIGHT:
self.speedy = self.speedy * -1
self.rect.y += self.speedy
if Player1().data(0) - 20 < self.rect.x and Player1().data(1) + 90 > self.rect.y and Player1().data(1) < self.rect.y:
self.speedx = self.speedx * -1
self.rect.x += self.speedx
if Player2().data(0) + 20 > self.rect.x and Player2().data(1) + 90 > self.rect.y and Player2().data(1) < self.rect.y:
self.speedx = self.speedx * -1
self.rect.x += self.speedx
self.rect.y += self.speedy
self.rect.x += self.speedx
all_sprites = pygame.sprite.Group()
player1 = Player1()
player2 = Player2()
ball = Ball()
all_sprites.add(player1)
all_sprites.add(player2)
all_sprites.add(ball)
running = True
while running:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
gamestart = 1
all_sprites.update()
screen.fill(BLACK)
all_sprites.draw(screen)
pygame.display.flip()
pygame.quit()