Здравствуйте, решил сделать игру легкую игру. Игра простая и заключается в том, что есть 2 шарика, одним из которых управляет игрок, он двигается только по оси y, второй двигается по оси x, им уже никто не управляет. Возникла проблема с тем, что при написании коллизии, шарик, который двигается независимо, не прорисовывается. Прошу помочь мне и разобраться с этой проблемой
Python:
#main.py
import pygame, controls
import sys
from ball import Ball
from controls import events
from lol import Lol
from pygame.sprite import Group
def run():
pygame.init()
screen = pygame.display.set_mode((1200, 800)) #меняем разрешение
pygame.display.set_caption("Шароёб(Управление на W и S)") # меняем название
bg_color = ( 0 , 0, 0) # задний цвет
ball = Ball(screen)
lol = Group()
while True:
screen.fill(bg_color)
controls.update_lol(lol)
lol.draw(screen)
controls.events(ball)
ball.output()
controls.collideBalls(ball, lol)
pygame.display.flip()
run()
Python:
#lol.py
import pygame
class Lol(pygame.sprite.Sprite):
def __init__(self, screen):
super(Lol, self).__init__()
self.screen = screen
self.image = pygame.image.load('images/pixil-frame-1.png')
self.rect= self.image.get_rect()
self.screen_rect = self.screen.get_rect()
self.rect.x = self.rect.width
self.rect.y = self.rect.height
self.x= float(self.rect.x)
self.y= float(self.rect.y)
def draw(self):
self.screen.blit(self.image, self.rect)
def update(self):
self.x+= 0.1
self.rect.x= self.x
Python:
#controls.py
import pygame,sys
def events(ball):
"""обработка событий"""
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYUP:
if event.key == pygame.K_w:
ball.rect.centery+= -6
elif event.type== pygame.KEYDOWN:
if event.key == pygame.K_s:
ball.rect.centery+= 6
def update_lol(lol):
lol.update()
def collideBalls(ball, lol):
for Lol in lol:
if ball.collidepoint(lol.rect.center):
ball.kill()
Python:
#ball.py
import pygame
class Ball():
def __init__(self, screen):
self.screen = screen
self.image = pygame.image.load('images/pixil-frame-0.png')
self.rect = self.image.get_rect()
self.screen_rect = self.screen.get_rect()
self.rect.centerx = self.screen_rect.centerx
self.rect.center = self.screen_rect.center
def output(self):
self.screen.blit(self.image, self.rect)
Последнее редактирование: