如何使碰撞在这个侧滚动条中工作?

2024-09-24 08:37:43 发布

您现在位置:Python中文网/ 问答频道 /正文

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)

在这里,我们创建了我们的player类,首先我们初始化我们的player

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.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()

Tags: andposselffalseifgroupwidthscreen
1条回答
网友
1楼 · 发布于 2024-09-24 08:37:43

^{}对象的^{}属性来检测冲突。因此,您必须根据播放器的位置更新矩形的位置:

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

相关问题 更多 >