import os
import math
from Colors import *
from Z import *
class Player(pygame.sprite.Sprite):
def __init__(self, width, height):
pygame.sprite.Sprite.__init__(self)
self.image=pygame.Surface((width,height))
self.image.fill(Red)
self.rect=self.image.get_rect()
self.rect.center=(ScreenWidth/2,ScreenHeight/2)
self.pos= pygame.math.Vector2(ScreenWidth/2,ScreenHeight/2)
def move(self,keys):
self.acc=pygame.math.Vector2(0,self.GRAVITY)
if keys[pygame.K_LEFT]:self.acc.x=-self.ACC
if keys[pygame.K_RIGHT]:self.acc.x=self.ACC
self.acc.x+=self.vel.x*self.FRICTION
self.vel+=self.acc
self.pos+=self.vel+self.acc/2
self.rect.midbottom=self.pos
## if self.rect.x<1:self.rect.x=1
## elif self.rect.x>ScreenWidth-1:self.rect.x=ScreenWidth-1
## if self.rect.y<1:self.rect.y=1
## elif self.rect.bottom>ScreenHeight-1:self.rect.bottom=ScreenHeight-1
def jump(self):
self.rect.y+=1
hits=pygame.sprite.spritecollide(self,objects,False)
self.rect.y-=1
if hits:
self.vel.y=-20
def set_values(self,ACC,FRICTION,GRAVITY):
self.ACC=ACC
self.GRAVITY=GRAVITY
self.FRICTION=FRICTION
self.vel=pygame.math.Vector2(0,0)
self.acc=pygame.math.Vector2(0,0)
class Objects(pygame.sprite.Sprite):
def __init__(self, x, y, width, height):
pygame.sprite.Sprite.__init__(self)
self.image=pygame.Surface((width, height))
self.image.fill(Green)
self.rect=self.image.get_rect()
self.rect.x=x
self.rect.y=y
def Display_All():
screen.fill(Black)
all_sprites.draw(screen)
objects.draw(screen)
pygame.display.flip()
def main():
running=True
global player
player=Player(20,20)
player.set_values(1, -0.15, 2)
all_sprites.add(player)
bounds.append(Objects(0, -9, ScreenWidth, 10))
bounds.append(Objects(0, ScreenHeight-1, ScreenWidth, 10))
bounds.append(Objects(-9, 1, 10, ScreenHeight-2))
bounds.append(Objects(ScreenWidth-1, 1, 10, ScreenHeight-2))
grounds.append(Objects(300, 600, 200, 20))
grounds.append(Objects(200, 650, 70, 20))
for platform in bounds+grounds:
objects.add(platform)
clock=pygame.time.Clock()
while running:
screen.fill(Black)
clock.tick(30)
for event in pygame.event.get():
if event.type==pygame.QUIT:
running=False
if event.type==pygame.KEYDOWN and event.key==pygame.K_ESCAPE:
running=False
if event.type==pygame.KEYDOWN and event.key==pygame.K_SPACE:
player.jump()
keys=pygame.key.get_pressed()
player.move(keys)
player.rect.y+=1
hits_down=pygame.sprite.spritecollide(player,objects,False)
player.rect.y-=1
if hits_down:
player.vel.y=0
player.pos.y=hits_down[0].rect.top
print(player.pos.y,grounds[0].rect.top)
objects.update()
all_sprites.update()
Display_All()
pygame.init()
os.environ["SDL_VIDEO_CENTERED"] = "1"
bounds=[]
grounds=[]
player=None
all_sprites=pygame.sprite.Group()
objects=pygame.sprite.Group()
ScreenWidth, ScreenHeight=700, 700
screen=pygame.display.set_mode((ScreenWidth, ScreenHeight))
main()
pygame.quit()
Проблема
Почему столкновение не работает должным образом? Спрайт прямоугольника плеера отключен на несколько пикселей, но при печати player.rect.bottom,grounds[0].rect.top
вывод равен 600.0,600
. Все равно player.rect.bottom отключен на несколько пикселей.
Может кто-нибудь помочь мне понять, почему пиксели прямоугольников отключены, как показано?