简体   繁体   English

如何使碰撞在此侧滚动条中起作用?

[英]How do I make the collisions work in this side scroller?

import pygame #importing the pygame library

pygame.init()


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

defines the sprite groups we'll be calling on later定义我们稍后将调用的精灵组

####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, 0)
hitbox = (150, 200, 100, 100)

here we are making our player class, first we are initializing our player这里我们正在制作我们的播放器 class,首先我们正在初始化我们的播放器

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)

here we are making the function that draws our character, we also have a jump system and an falling system在这里,我们正在制作吸引我们角色的 function,我们还有一个跳跃系统和一个下降系统

  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.y + 20), (self.width - 33), (self.height- 33))
    if pygame.sprite.spritecollideany(self, obstacle_group):
      print("collide")
    

Here we are making the green rectangle that is our obstacle in the game, first initializing it and that drawing it to the screen in the color jade在这里,我们正在制作作为我们在游戏中的障碍物的绿色矩形,首先对其进行初始化,然后将其以彩色jade绘制到屏幕上

class Obstacle(pygame.sprite.Sprite):

  def __init__(self, x, y, width, height, 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.y, self.width, self.height))
    self.hitbox = pygame.Rect((self.x - self.pos),self.y, self.width, self.height)
    pygame.draw.rect(screen, red, self.hitbox, 1)
  

pos is the variable we use to scroll the background pos是我们用来滚动背景的变量

pos = 0

here we are making the player character and the only obstacle in the side scroller在这里,我们正在制作玩家角色和侧滚动条中的唯一障碍

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

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

this is our main drawing function that we call on in our main while loop这是我们在主循环中调用的主图while

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

this is our while loop这是我们的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 

this is the point we can't figure out, we want it to print collide when the two sprites are colliding这是我们无法弄清楚的一点,我们希望它在两个精灵collide时打印碰撞

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

Here we made a simple end screen这里我们做了一个简单的结束画面

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

pygame.sprite.spritecollideany attributes of the pygame.sprite.Sprite objects to detect a collision. pygame.sprite.spritecollideany属性的pygame.sprite.Sprite对象来检测碰撞。 Therefore you have to update the location of the rectangle by the position of the player:因此,您必须通过播放器的 position 更新矩形的位置:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM