簡體   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()

定義我們稍后將調用的精靈組

####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)

這里我們正在制作我們的播放器 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)

在這里,我們正在制作吸引我們角色的 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")
    

在這里,我們正在制作作為我們在游戲中的障礙物的綠色矩形,首先對其進行初始化,然后將其以彩色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是我們用來滾動背景的變量

pos = 0

在這里,我們正在制作玩家角色和側滾動條中的唯一障礙

obstacle2 = Obstacle(300, 200, 100, 20, pos)
nkdMonkey = Player(150, 200, 100, 100, 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,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()

這是我們的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!!', 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屬性的pygame.sprite.Sprite對象來檢測碰撞。 因此,您必須通過播放器的 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