问题描述
我的老师给了我们这段代码来分析,我不知道他为什么把27放在那里进行碰撞.我问他他说如果玩家和敌人之间的距离小于27,我就称之为碰撞,但我还是不明白,请有人用简单的术语向我解释一下.我不明白数字 27 从何而来……当我的尺寸如此之大时?
导入数学随机导入导入pygame# 初始化pygamepygame.init()# 创建屏幕屏幕 = pygame.display.set_mode((1000, 700))# 背景background = pygame.image.load('undersea.png')# 玩家playerImg = pygame.image.load('space-invaders.png')玩家X = 500玩家Y = 600玩家X_change = 0# 敌人敌人图像 = []敌人X = []敌人Y = []敌人X_change = []敌人Y_change = []num_of_enemies = 5对于我在范围内(num_of_enemies):敌人Img.append(pygame.image.load('plastic.png'))敌人X.append(random.randint(0, 636))敌人Y.append(random.randint(50, 150))敌人X_change.append(4)敌人Y_change.append(40)#子弹# 准备好了 - 你看不到屏幕上的子弹# Fire - 子弹正在移动bulletImg = pygame.image.load('bullet (1).png')子弹X = 0子弹Y = 480子弹X_change = 0子弹Y_change = 10bullet_state =准备好";# 分数得分值 = 0font = pygame.font.Font('freesansbold.ttf', 32)文本X = 10测试Y = 10# 游戏结束over_font = pygame.font.Font('freesansbold.ttf', 64)def show_score(x, y):score = font.render("Score: " + str(score_value), True, (255, 255, 255))screen.blit(score, (x, y))def game_over_text():over_text = over_font.render("GAME OVER", True, (255, 255, 255))screen.blit(over_text, (200, 250))定义玩家(x,y):screen.blit(playerImg, (x, y))定义敌人(x,y,i):screen.blit(enemyImg[i], (x, y))def fire_bullet(x, y):全局 bullet_state子弹状态=火";screen.blit(bulletImg, (x + 16, y + 10))def isCollision(enemyX,enemyY,bulletX,bulletY):距离 = ((enemyX - bulletX) ** 2 + (enemyY - bulletY) ** 2)距离 = math.sqrt(距离)打印(距离)如果距离<27:返回真别的:返回错误# 游戏循环运行 = 真在跑步的时候:# RGB = 红、绿、蓝screen.fill((0, 0, 0))# 背景图片screen.blit(背景, (0, 0))对于 pygame.event.get() 中的事件:如果 event.type == pygame.QUIT:运行 = 错误# 如果按键被按下,检查它是向右还是向左如果 event.type == pygame.KEYDOWN:如果 event.key == pygame.K_LEFT:playerX_change = -5如果 event.key == pygame.K_RIGHT:玩家X_change = 5如果 event.key == pygame.K_SPACE:如果 bullet_state 是就绪":# 获取飞船当前的x坐标子弹X = 玩家Xfire_bullet(bulletX,bulletY)如果 event.type == pygame.KEYUP:如果 event.key == pygame.K_LEFT 或 event.key == pygame.K_RIGHT:玩家X_change = 0# 5 = 5 + -0.1 ->5 = 5 - 0.1# 5 = 5 + 0.1playerX += playerX_change如果玩家X = 636:玩家X = 636# 敌人运动对于我在范围内(num_of_enemies):# 游戏结束如果敌人Y[i] >440:对于范围内的 j(num_of_enemies):敌人Y[j] = 2000game_over_text()休息敌人X[i] +=敌人X_change[i]如果敌人X[i] = 736:敌人X_change[i] = -4敌人Y[i] +=敌人Y_change[i]# 碰撞碰撞 = isCollision(enemyX[i],敌人Y[i],bulletX,bulletY)如果碰撞:子弹Y = 480bullet_state =准备好";得分值 += 1敌人X[i] = random.randint(0, 736)敌人Y[i] = random.randint(50, 150)敌人(敌人X[i],敌人Y[i],我)# 子弹运动如果bulletY
该值为 27,因为您计算的是 函数isCollision
中(enemyX,enemyY)
和(bulletX,bulletY)
之间的欧氏距离:
def isCollision(enemyX,enemyY,bulletX,bulletY):距离 = ((enemyX - bulletX) ** 2 + (enemyY - bulletY) ** 2)距离 = math.sqrt(距离)打印(距离)
两点(Ax, Ay)和(Bx, By)之间的欧几里得距离是
d = sqrt((Bx-Ax)**2 + (By-Ay)**2) = hypot(Bx-Ax, By-Ay)
在二维空间中,这与 勾股定理相同.边长为19的正方形对角线的长度约为27.
My teacher gave us this code to analyze and I am not sure why he put 27 there for collision.. I asked him he said if the distance between the player and the enemy is less than 27 then I will call it a collision, but I still don't understand, can someone please kindly explain it to me in simpler terms. I don't understand where the number 27 comes from.. when my dimensions are so big?
import math
import random
import pygame
# Intialize the pygame
pygame.init()
# create the screen
screen = pygame.display.set_mode((1000, 700))
# Background
background = pygame.image.load('undersea.png')
# Player
playerImg = pygame.image.load('space-invaders.png')
playerX = 500
playerY = 600
playerX_change = 0
# Enemy
enemyImg = []
enemyX = []
enemyY = []
enemyX_change = []
enemyY_change = []
num_of_enemies = 5
for i in range(num_of_enemies):
enemyImg.append(pygame.image.load('plastic.png'))
enemyX.append(random.randint(0, 636))
enemyY.append(random.randint(50, 150))
enemyX_change.append(4)
enemyY_change.append(40)
# Bullet
# Ready - You can't see the bullet on the screen
# Fire - The bullet is currently moving
bulletImg = pygame.image.load('bullet (1).png')
bulletX = 0
bulletY = 480
bulletX_change = 0
bulletY_change = 10
bullet_state = "ready"
# Score
score_value = 0
font = pygame.font.Font('freesansbold.ttf', 32)
textX = 10
testY = 10
# Game Over
over_font = pygame.font.Font('freesansbold.ttf', 64)
def show_score(x, y):
score = font.render("Score : " + str(score_value), True, (255, 255, 255))
screen.blit(score, (x, y))
def game_over_text():
over_text = over_font.render("GAME OVER", True, (255, 255, 255))
screen.blit(over_text, (200, 250))
def player(x, y):
screen.blit(playerImg, (x, y))
def enemy(x, y, i):
screen.blit(enemyImg[i], (x, y))
def fire_bullet(x, y):
global bullet_state
bullet_state = "fire"
screen.blit(bulletImg, (x + 16, y + 10))
def isCollision(enemyX, enemyY, bulletX, bulletY):
distance = ((enemyX - bulletX) ** 2 + (enemyY - bulletY) ** 2)
distance = math.sqrt(distance)
print(distance)
if distance < 27:
return True
else:
return False
# Game Loop
running = True
while running:
# RGB = Red, Green, Blue
screen.fill((0, 0, 0))
# Background Image
screen.blit(background, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# if keystroke is pressed check whether its right or left
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX_change = -5
if event.key == pygame.K_RIGHT:
playerX_change = 5
if event.key == pygame.K_SPACE:
if bullet_state is "ready":
# Get the current x cordinate of the spaceship
bulletX = playerX
fire_bullet(bulletX, bulletY)
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 0
# 5 = 5 + -0.1 -> 5 = 5 - 0.1
# 5 = 5 + 0.1
playerX += playerX_change
if playerX <= 0:
playerX = 0
elif playerX >= 636:
playerX = 636
# Enemy Movement
for i in range(num_of_enemies):
# Game Over
if enemyY[i] > 440:
for j in range(num_of_enemies):
enemyY[j] = 2000
game_over_text()
break
enemyX[i] += enemyX_change[i]
if enemyX[i] <= 0:
enemyX_change[i] = 4
enemyY[i] += enemyY_change[i]
elif enemyX[i] >= 736:
enemyX_change[i] = -4
enemyY[i] += enemyY_change[i]
# Collision
collision = isCollision(enemyX[i], enemyY[i], bulletX, bulletY)
if collision:
bulletY = 480
bullet_state = "ready"
score_value += 1
enemyX[i] = random.randint(0, 736)
enemyY[i] = random.randint(50, 150)
enemy(enemyX[i], enemyY[i], i)
# Bullet Movement
if bulletY <= 0:
bulletY = 480
bullet_state = "ready"
if bullet_state is "fire":
fire_bullet(bulletX, bulletY)
bulletY -= bulletY_change
player(playerX, playerY)
show_score(textX, testY)
pygame.display.update()
The value is 27, because you compute the Euclidean distance between (enemyX, enemyY)
and (bulletX, bulletY)
in the function isCollision
:
The Euclidean distance between 2 points (Ax, Ay) and (Bx, By) is
d = sqrt((Bx-Ax)**2 + (By-Ay)**2) = hypot(Bx-Ax, By-Ay)
In 2 dimensional space this is the same as the Pythagorean theorem. The length of the diagonal in a square with a side length of 19 is approximately 27.
这篇关于我想知道为什么它说距离必须小于 27?为什么小于27?27从哪里来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!