我已經試過了一切,不能得到如何檢查我的兩個塊是否發生了碰撞。如何讓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)
這是所有真正的碰撞,我想我會好。哦,是的,如果你們也可以告訴我如何顯示文字,這將是偉大的!
關於文本渲染,請不要在這裏同時提出兩個問題,並查看此問題:https://stackoverflow.com/questions/10077644/python-display-text-w-font-color – skrx
你的代碼在很多地方都是錯誤的,並且使用了可疑的編程技術(比如擁有類屬性的東西顯然應該是實例屬性等)。請務必確保您的代碼正常工作(除非您詢問錯誤/錯誤),並且在提出問題之前對代碼有基本的瞭解。否則,所有的答案將是不全面的,並且很可能只能用作複製粘貼解決方案。 –
它雖然工作。你能不能讓它運行? SnakePart和Apple只是一個綠色的紅色立方體。 –