pygame中的isCollision函数

问题描述 投票:1回答:1

我最近开始在pygame中进行编程,一直想知道为什么我的isCollision函数不起作用。您实际上不必运行代码,因为您需要下载图片以使其执行。如果可以,请告诉我为什么isCollision函数不起作用。在mainloop中,在range(num_of_obstacles)循环中有一个for i,其中包含if iscollision语句。如果您想查看程序的功能,请参见以下所有基本文件:顺便说一句,请不要重写整个代码。name it rectangle.png(the first image)[![][1]]2[

不要介意这些评论,因为它们会擦亮。这是我的代码:

3
python python-3.x pygame pycharm pygame-surface
1个回答
2
投票

您是否要检查距离是import pygame import math import random #inicjowanie pygame (to trzeba zawsze dać) pygame.init() screen = pygame.display.set_mode((800, 600)) pygame.display.set_caption('Kosmiczna Przygoda') playericon = pygame.image.load('rocket.png') player2icon = pygame.image.load('rocket.png') backgroundImg = pygame.image.load('1083.jpg') num_of_obstacles = 10 obstacleImg = [] obsX = [random.randint(0, 400) for i in range(num_of_obstacles // 2)] obs2X = [random.randint(400, 800) for i in range(num_of_obstacles // 2)] for i in obs2X: obsX.append(i) obsY = [] for i in range(num_of_obstacles): obstacleImg.append(pygame.image.load('rectangle.png')) for i in range(num_of_obstacles): obsY.append(random.randint(50, 300)) # pierwsze koordynaty PlayerX, PlayerY = 200, 480 Player2X, Player2Y = 500, 480 PlayerY_change, PlayerX_change = 1, 1 Player2Y_change, Player2X_change = 1, 1 def player(x,y,x2,y2): screen.blit(playericon, (x, y)) screen.blit(player2icon, (x2, y2)) winFont = pygame.font.Font('freesansbold.ttf', 64) won1, won2 = False, False def player1Wins(): win_text = winFont.render('PLAYER 1 WON!',True,(255,255,255)) screen.blit(win_text,(30,30)) def player2Wins(): win_text = winFont.render('PLAYER 2 WON!',True,(255,255,255)) screen.blit(win_text,(150,30)) # wzór matematyczny na odległość koordynatów dwóch punktów (sprawdzabnie czy się dotykają) def isCollision(obsX,obsY,pX,pY): distance = math.sqrt(math.pow(obsX - pX, 2) + math.pow(obsY - pY, 2)) if distance >= 27: return True else: return False running = True won = False while running: # tło (blit to rysowanie) screen.blit(backgroundImg, (0, 0)) # event to wydarzenie zarejestrowane przez program # jeżeli klikne krzyzyk w prawym gornym rogu to program sie zamknie # jeżeli nacisne np strzalke w prawo to rakieta przesuwa się na ukos w prawo for event in pygame.event.get(): if event.type == pygame.QUIT: running = False if event.type == pygame.KEYDOWN: if event.key == pygame.K_a: PlayerX_change = -1 if event.key == pygame.K_d: PlayerX_change = 1 if event.key == pygame.K_LEFT: Player2X_change = -1 if event.key == pygame.K_RIGHT: Player2X_change = 1 if event.key == pygame.K_w: PlayerY_change = 2 if event.key == pygame.K_UP: Player2Y_change = 2 if event.type == pygame.KEYUP: if event.key == pygame.K_UP: PlayerY2_change = 0.75 if event.key == pygame.K_w: PlayerY_change = 0.75 for enemy in range(num_of_obstacles): if won: screen.blit(obstacleImg[enemy], (2000, 2000)) else: #if isCollision(obsX[enemy], obsY[enemy],PlayerX,PlayerY): #player2Wins() screen.blit(obstacleImg[enemy], (obsX[enemy], obsY[enemy])) # Granice Ekranu if PlayerX <= 0: PlayerX = 0 elif PlayerX >= 736: PlayerX = 736 if PlayerY <= 0: won, won1 = True, True PlayerY, Player2Y = 480, 480 PlayerX, Player2X = 200, 500 if Player2X <= 0: Player2X = 0 elif Player2X >= 736: Player2X = 736 if Player2Y <= 0: won, won2 = True, True PlayerY, Player2Y = 480, 480 PlayerX, Player2X = 200, 500 if won1: player1Wins() PlayerX_change, PlayerY_change = 0, 0 Player2X_change, Player2Y_change = 0, 0 if won2: player2Wins() PlayerX_change, PlayerY_change = 0, 0 Player2X_change, Player2Y_change = 0, 0 # Zmiana kordynatów rakiety PlayerX += PlayerX_change PlayerY -= PlayerY_change Player2X += Player2X_change Player2Y -= Player2Y_change player(PlayerX, PlayerY, Player2X, Player2Y) pygame.display.update() 而不是<= 27

>= 27
© www.soinside.com 2019 - 2024. All rights reserved.