2017-10-19 67 views
0

我已經試過了一切,不能得到如何檢查我的兩個塊是否發生了碰撞。如何讓pygame檢查兩幅圖像是否碰撞?

這裏是我的代碼:

import pygame 
import random 
pygame.init() 

display_width = 600 
display_height = 300 

class player:#Just info for player 
    width = 30 
    height = 30 
    x = display_width // 2 
    y = display_height - height - 5 
    a = x + 30 
    pic = 'SnakePart.png' 
    thing = pygame.image.load(pic) 
    def place(x,y):#function for placing player object 
     gameDisplay.blit(player.thing, (player.x,player.y)) 
class enemy:#enemy info class 
    width = 30 
    height = 30 
    x = random.randint(0,display_width - width) 
    y = 1 
    a = x + 30 
    pic = 'Apple.png' 
    thing = pygame.image.load(pic) 
    speed = 10#sets speed 
    def change_x(): 
     enemy.x = random.randint(0,display_width - enemy.width) 
    def change_y(): 
     enemy.y += enemy.speed 
    def make(x,y):#set up funtion 
     gameDisplay.blit(enemy.thing, (x,y)) 
    def respawn():#reseting enemy entity 
     enemy.y = 1 
     gameDisplay.blit(enemy.thing, (enemy.x,enemy.y)) 
player.thing#uses the variables in the classes to set up the images for use 
enemy.thing 

black = (0,0,0) 
white = (255,255,255) 


player_height = 30 
player_width = 30 

clock = pygame.time.Clock() 

x_change = 0#This is to make movment 

gameDisplay = pygame.display.set_mode((display_width,display_height)) 
pygame.display.set_caption('Bullet Hell.') 

dodged = 0#counter will be used in the more polished vesion. 

while True: 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      pygame.quit() 
      quit() 
     if event.type == pygame.KEYDOWN:#Checks for keypress, to make player entity move 
      if event.key == pygame.K_RIGHT: 
       x_change = 5 
      if event.key == pygame.K_LEFT: 
       x_change = -5 
     if event.type == pygame.KEYUP: 
      if event.key == pygame.K_RIGHT or pygame.K_LEFT: 
       x_change = 0 

    player.x += x_change 
    gameDisplay.fill(black) 
    enemy.make(enemy.x,enemy.y) 
    player.place(player.x,player.y) 
    enemy.change_y() 
    if enemy.y > display_height:#Brings enemy back to top once it has gotten to th bottom 
     enemy.change_x() 
     dodged += 1 
     enemy.respawn() 
    pygame.display.update() 
    clock.tick(60) 

這是所有真正的碰撞,我想我會好。哦,是的,如果你們也可以告訴我如何顯示文字,這將是偉大的!

+0

關於文本渲染,請不要在這裏同時提出兩個問題,並查看此問題:https://stackoverflow.com/questions/10077644/python-display-text-w-font-color – skrx

+0

你的代碼在很多地方都是錯誤的,並且使用了可疑的編程技術(比如擁有類屬性的東西顯然應該是實例屬性等)。請務必確保您的代碼正常工作(除非您詢問錯誤/錯誤),並且在提出問題之前對代碼有基本的瞭解。否則,所有的答案將是不全面的,並且很可能只能用作複製粘貼解決方案。 –

+0

它雖然工作。你能不能讓它運行? SnakePart和Apple只是一個綠色的紅色立方體。 –

回答

0

看看pygame.Rect類及其碰撞檢測方法。通過調用圖像/表面的.get_rect()方法,將您的對象作爲屬性賦予您的屬性。更新每個框架的這些矩形的座標,並在主循環中調用player.rect.colliderect(enemy.rect)以檢查兩個矩形是否發生碰撞。

import pygame 
import random 


pygame.init() 

BLACK = pygame.Color('black') 

display = pygame.display.set_mode((600, 300)) 
# Create a rect with the dimensions of the screen at coords (0, 0). 
display_rect = display.get_rect() 
clock = pygame.time.Clock() 

# SNAKE_IMAGE = pygame.image.load('SnakePart.png').convert_alpha() 
# APPLE_IMAGE = pygame.image.load('Apple.png').convert_alpha() 
# Replacement images. 
SNAKE_IMAGE = pygame.Surface((30, 30)) 
SNAKE_IMAGE.fill((30, 150, 0)) 
APPLE_IMAGE = pygame.Surface((30, 30)) 
APPLE_IMAGE.fill((150, 30, 0)) 


class Player: 

    def __init__(self): 
     self.x = display_rect.w // 2 
     self.y = display_rect.h - 30 - 5 
     self.image = SNAKE_IMAGE 
     # Create a rect with the size of the image at coords (0, 0). 
     self.rect = self.image.get_rect() 
     # Set the topleft coords of the rect. 
     self.rect.x = self.x 
     self.rect.y = self.y 
     self.x_change = 0 

    def update(self): 
     """Move the player.""" 
     self.x += self.x_change 
     # Always update the rect, because it's 
     # needed for the collision detection. 
     self.rect.x = self.x 

    def draw(self, display): 
     display.blit(self.image, self.rect) 


class Enemy: 

    def __init__(self): 
     self.x = random.randint(0, display_rect.w - 30) 
     self.y = 1 
     self.image = APPLE_IMAGE 
     # You can also pass the coords directly to `get_rect`. 
     self.rect = self.image.get_rect(topleft=(self.x, self.y)) 
     self.speed = 10 

    def change_x(self): 
     self.x = random.randint(0, display_rect.w - self.rect.w) 
     self.rect.x = self.x 

    def change_y(self): 
     self.y += self.speed 
     self.rect.y = self.y 

    def draw(self, display): 
     display.blit(self.image, self.rect) 

    def reset(self): 
     """Reset self.y position.""" 
     self.y = -30 


player = Player() 
enemy = Enemy() 
dodged = 0 
done = False 

while not done: 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      done = True 
     if event.type == pygame.KEYDOWN: 
      if event.key == pygame.K_RIGHT: 
       player.x_change = 5 
      if event.key == pygame.K_LEFT: 
       player.x_change = -5 
     if event.type == pygame.KEYUP: 
      if event.key == pygame.K_RIGHT or pygame.K_LEFT: 
       player.x_change = 0 

    # Game logic. 
    player.update() 
    enemy.change_y() 
    # Brings enemy back to top once it has gotten to th bottom 
    if enemy.y > display_rect.h: 
     enemy.change_x() 
     dodged += 1 
     enemy.reset() 

    # Check if the player and the rect collide. 
    if player.rect.colliderect(enemy.rect): 
     print('Collided!') 

    # Draw everything. 
    display.fill(BLACK) 
    enemy.draw(display) 
    player.draw(display) 

    pygame.display.update() 
    clock.tick(60) 

pygame.quit() 

我已經改變了一些更多的東西(順便說一句,最好打電話圖像self.image沒有thing;))特別是在類:

類的屬性應該是在__init__方法,使他們實例屬性而不是類屬性(它被所有實例共享)。另外,請查看如何創建類的實例。

這些方法應該都有self作爲它們的第一個參數(這是對實例的引用)。要訪問類內部的屬性,可以使用self(例如, self.x += self.x_change

player.x += x_change線符合Playerupdate方法中較好的,所以你可以更新的位置和其他球員通過調用player.update()屬性。

有一個約定,類名應該是大寫的MyClass而實例應該有小寫的名字my_instance

+0

我建議查看關於類和pygame精靈的[Program Arcade Games](http://programarcadegames.com/index.php?chapter=introduction_to_classes&lang=de#section_12)的第12和13章。 – skrx

+0

噢,是的,'東西'變量......我只是因爲'pygame.image.load()'而在恐懼中做到了這一點,因爲即時通過pygame進行編程的新手肯定會遇到很多混亂。附:我教自己如何編碼。而有趣的是,我試圖用數學......並沒有很好地結束。 –