當蘋果超過三角形時,我試圖讓蘋果圖片在白色三角形的頂部,然後按h。我卡住了,不知道該怎麼辦。當我擊中h時,沒有好的或壞的事情發生。我可以移動我的矩形,但不能拿起圖片。而且我不知道如何讓矩形離開蘋果。這裏是我的代碼:如何讓東西卡住別的東西?
import pygame
import random
from pygame.locals import *
screenSize = (640,480)
surface = pygame.display.set_mode(screenSize)
running = True
clock = pygame.time.Clock()
x = 1
y = 1
a = random.randint(0,640)
b = random.randint(0,480)
while running:
clock.tick(15)
surface.fill((20,150,100))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x -= 10
elif event.key == pygame.K_RIGHT:
x += 10
elif event.key == pygame.K_UP:
y -= 10
elif event.key == pygame.K_DOWN:
y += 10
elif event.key == pygame.K_h:
a == x and b == y
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x -= 0
elif event.key == pygame.K_UP or event.key == pygame.K_DOWN:
y -= 0
line1 = pygame.draw.line(surface, (0,0,0), [100,100], [10,150], 4)
line2 = pygame.draw.line(surface, (0,0,0), [100,100], [100,300], 4)
line3 = pygame.draw.line(surface, (10, 20, 39), [100,100], [190,150] , 4)
line4 = pygame.draw.line(surface, (0,0,0), [100,300], [10,350], 4)
line5 = pygame.draw.line(surface, (0,0,0), [100,300], [190,350], 4)
circle = pygame.draw.circle(surface, (0,0,0), [100,60], 40, 4)
rect = pygame.draw.rect(surface, (150, 50, 0), [350, 250, 100, 100])
linerect = pygame.draw.line(surface, (75,25,0), [350, 280], [450,280],3)
rect2 = pygame.draw.rect(surface, (200, 150, 0), [395,275,10,10])
rect3 = pygame.draw.rect(surface, (75,25,0), [360,290,80,50],3)
box = pygame.draw.rect(surface, (255,255,255), [x,y,100,100])
apple = pygame.image.load('apple1.bmp')
surface.blit(apple,(a,b))
pygame.display.flip()
pygame.quit()
我沒有測試你的代碼,但看起來奇怪線我。你認爲這行代碼是'a == x and b == y'?那是測試'a'等於'x'和'b'等於'y'。我懷疑這就是你想要的。你只是想把'a'設置爲'x'和'b'爲'y'? – elParaguayo