2017-10-19 95 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进行编程的新手肯定会遇到很多混乱。附:我教自己如何编码。而有趣的是,我试图用数学......并没有很好地结束。 –