我如何使这个侧滚动条发生碰撞?

问题描述

import pygame #importing the pygame library

pygame.init()


all_sprites = pygame.sprite.Group()   
obstacle_group = pygame.sprite.Group()
player_group = pygame.sprite.Group()

定义我们稍后将调用的精灵组

####Variables
width,height = 626,375
fontObj = pygame.font.Font('Alsina Ultrajada.TTF',16)
pygame.display.set_caption("side scroller")
screen = pygame.display.set_mode((width,height))
bg = pygame.image.load("achtergrondPixel.png")
gameOver = pygame.image.load("gameOver.png")

R_char = pygame.transform.scale(pygame.image.load("character.png"),(100,100))
L_char = pygame.transform.flip((R_char),True,False)
char = R_char
jade = (55,255,20)
red = (255,0)
hitBox = (150,200,100,100)

在这里我们制作我们的播放器类,首先我们初始化我们的播放器

class Player(pygame.sprite.Sprite): #making a class for our character so we can easily call the variables

  def __init__(self,x,y,width,height,pos):
    global player_group
    super().__init__(player_group,all_sprites)
    self.x = x
    self.y = y
    self.width = width
    self.height = height
    self.charFlip = False
    self.isJump = False
    self.jumpCount = 10
    self.isFalling = False
    self.fallCount = int(1)
    self.pos = pos
    self.rect = pygame.Rect((self.x - self.pos + 21),(self.y + 20),(self.width - 33),(self.height- 33))
    self.add(player_group)

这里我们制作了绘制角色的函数,我们还有一个跳跃系统和一个下降系统

  def draw(self,screen):
    screen.blit(char,(self.x,self.y))
    if self.isFalling:
      if self.y <= 200  and self.x < 488 and self.x > 573:  #hier moet var worden aangemaakt voor als hij op platform staat
          self.y -= (self.fallCount**2) * 0.1 * -1
          self.fallCount += 1
      else:
        self.isFalling = False
        self.fallCount = 1
    if self.isJump:
      if self.jumpCount >= 0:
        neg = 1
        if self.jumpCount == 0:
          self.isFalling = True
        self.y -= (self.jumpCount**2) * .25 * neg
        self.jumpCount -= 1
      else:
        self.isJump = False
        self.jumpCount = 10
    self.hitBox = pygame.Rect((self.x - self.pos + 21),(self.height- 33))
    if pygame.sprite.spritecollideany(self,obstacle_group):
      print("collide")
    

这里我们制作了作为游戏中障碍物的绿色矩形,首先对其进行初始化,然后将其绘制到屏幕上,颜色为 jade

class Obstacle(pygame.sprite.Sprite):

  def __init__(self,pos):
    global obstacle_group
    super().__init__(obstacle_group,all_sprites)
    self.pos = pos
    self.x = x
    self.y = y
    self.width = width
    self.height = height
    self.rect = pygame.Rect((self.x - self.pos),self.y,self.width,self.height)
    #self.add(obstacle_group)

  def draw(self,screen,pos):
    pygame.draw.rect(screen,jade,pygame.Rect((self.x - self.pos),self.height))
    self.hitBox = pygame.Rect((self.x - self.pos),self.height)
    pygame.draw.rect(screen,red,self.hitBox,1)
  

pos 是我们用来滚动背景的变量

pos = 0

在这里,我们正在制作玩家角色和侧卷轴中唯一的障碍

obstacle2 = Obstacle(300,20,pos)
nkdMonkey = Player(150,pos)

FPS = 60
run = True
clock = pygame.time.Clock()

这是我们在主 while 循环中调用的主要绘图函数

def draw_window():
  screen.blit(bg,((-1 * pos),0))
  textSurfaceObj = fontObj.render((str(pos)),False,(240,240,255))
  
  screen.blit(textSurfaceObj,(40,40)) 
  obstacle2.draw(screen,pos)
  nkdMonkey.draw(screen)
  #pygame.draw.rect(screen,nkdMonkey.hitBox,1)
  #for if you want to see the hitBox of the player which is used for collisions
  pygame.display.update()

这是我们的while循环

while run:

   
    clock.tick(FPS)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
    #all_sprites = pygame.sprite.Group()   
    #obstacle_group = pygame.sprite.Group()
    #player_group = pygame.sprite.Group()
    keys_pressed = pygame.key.get_pressed()
    #Checks if the front and and back button is pressed and if so,changes pos.
    if keys_pressed[pygame.K_d] and pos < 1874:
        pos += 2
        char = R_char
    if keys_pressed[pygame.K_d] and keys_pressed[
            pygame.K_LSHIFT] and pos < 2000:
        pos += 5
        char = R_char
    if keys_pressed[pygame.K_a] and pos > 0:
        pos -= 2
        char = L_char
    if keys_pressed[pygame.K_a] and keys_pressed[pygame.K_LSHIFT] and pos > 0:
        pos -= 5
        char = L_char
    if keys_pressed[pygame.K_w] and nkdMonkey.isJump == False:
        nkdMonkey.isJump = True
    if nkdMonkey.y > 200 and nkdMonkey.x > 488 and nkdMonkey.x < 573:
      nkdMonkey.y -= 1  
    #if nkdMonkey.x > 488 and nkdMonkey.x < 573:
      #nkdMonkey.isFalling = True
    

    if pos > 1980:
      
      run = False 

这是我们想不通的地方,我们希望它在两个精灵碰撞时打印collide

    if pygame.sprite.spritecollideany(nkdMonkey,obstacle_group):
      print("collide")
    all_sprites.update()
    draw_window()

这里我们制作了一个简单的片尾画面

screen.fill(jade)
screen.blit(pygame.image.load("character.png"),(0,70))
text = fontObj.render('You win!!',255))
textRect = text.get_rect()
score = fontObj.render(str(pos),red)
screen.blit(score,40)) 
textRect.center = (width // 2,(height // 2 + 20))
screen.blit(text,textRect)
pygame.display.flip()

解决方法

用于检测碰撞的 pygame.sprite.spritecollideany 对象的

pygame.sprite.Sprite 属性。因此,您必须根据玩家的位置更新矩形的位置:

nkdMonkey.rect.topleft = (nkdMonkey.x - nkdMonkey.pos),nkdMonkey.y)
if pygame.sprite.spritecollideany(nkdMonkey,obstacle_group):
    print("collide")