Ultimate Python Turtle Graphics Tutorial: Space Arena 9 (Collision Detection)

preview_player
Показать описание
Welcome to the Ultimate Python Turtle Graphics Tutorial! In it, I will walk you step-by-step how to create an original shoot'em up game called Space Arena. This is an intermediate level tutorial and uses concepts such as classes, and inheritance in its design as well as some geometry to make the spaceship physics more realistic.

Collision Code:
# NOTE: YouTube doesn't allow angle brackets in descriptions so change lt to the less than symbol and gt to the greater than symbol

def is_collision(self, other):
return True
else:
return False

NEED HELP?

❤️❤️ SHOW SOME LOVE AND SUPPORT THE CHANNEL ❤️❤️

Click Join and Become a Channel Member Today!
Channel members can get preferential comment replies, early access to new content, members only live streams, and access to my private Discord.

Amazon Affiliate Links

Other Affiliate Links

LINKS

LEARN MORE PYTHON

LEARN MORE JAVA

#Python #Tutorial #Beginner
Рекомендации по теме
Комментарии
Автор

the thing that impresses me the most is that when he gets an error message he does not rage and sweat and also he is calm and laughs it off. seriously the coding is easy but that, THAT, takes skill

kaylianpanchoo
Автор

You can use canvas.find_overlapping to identify the objects on the Tk canvas that are overlapping key points.
Establishing the shapes with turtle, it should be fairly direct to create orientation-aligned sensitive points (very small rectangles) on your ship, and then see what is touching them. That'd be really close to perfection, in terms of determining the overlap. It doesn't help you figure out a reactive spin, though, I don't think, ...
Colliding things is really, really hard.

LionKimbro
Автор

AABB! You told me to look out for this, and it was great to finally see it in action! By looking at the formula, it seems to offer more flexibility than the distance method. I really love how methodically you've been approaching the classes and giving detailed, step-by-step instructions throughout. The use of all those functions is keeping the loop so clean and the code so organized. I'm also stoked that you used "isinstance" since I just saw this for the first time in your new classes tutorial! I'm digging the innovation so far in this excellent tutorial, and I can't wait to get into the second half of it! Amazing stuff, as always!

ethansofiadada
Автор

After i made this part, i played the game so much that my laptop almost died.

sominupadhyaya
Автор

Kindly suggesting, I think you need to tweak the formula a bit before using, because in order for the formula to work, their x and y pivot point must start at the top left corner but that's not the same case in turtle, turtle set the pivot point at the middle.

Bryan-bhcy
Автор

what will happen if we kill an enemy? and can we have a game timer?
And you teach really good!!!

decode
Автор

Us: writes the whole collision detection code
TokyoEdTech: im gonna put all this code in the comments so you can copy and paste it
Me: *nani*

pythonicperson
Автор

@tokyoedtech, how do you stop the player from constantly accelerating and not stopping, my player goes annoyingly fast

mcmuf
Автор

The rectangles for collision overlap don't rotate though with the turtle right?

Sebastian--
Автор

i dont feel all these lines you know what im saying board these days in just wanted to have fun of trying new things....that fun of i dont know if i should keep up or i keep on??

estivaou
Автор

I found a fun trick, you can pass a function to assign random colour for your objects every time you open
def random_color():
return random.random(), random.random(), random.random()
enemy_1 = Enemy(0, 100, "square", random_color())
Have fun !!

Bryan-bhcy
Автор

is there any way i can make the bullet go further in distance ? thanks!

doodydoody
Автор

my second enemy and powerup arent moving

oliverlacika
Автор

"missile" is not defined. Error. Code: import turtle
import math

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600

wn = turtle.Screen()
wn.setup(SCREEN_WIDTH, SCREEN_HEIGHT)
wn.title("Space Arena by Simon Alfonzo")
wn.bgcolor("black")
wn.tracer(0)

pen = turtle.Turtle()
pen.speed(0)
pen.shape("square")
pen.color("white")
pen.penup()
pen.hideturtle()

class Game():
def __init__(self, width, height):
self.width = width
self.height = height

def render_border(self, pen):
pen.color("white")
pen.width(3)
pen.penup()

left = -self.width/2.0
right = self.width/2.0
top = self.height/2.0
bottom = -self.height/2.0

pen.goto(left, top)
pen.pendown()
pen.goto(right, top)
pen.goto(right, bottom)
pen.goto(left, bottom)
pen.goto(left, top)
pen.penup()

class Sprite():
# Constructor
def __init__(self, x, y, shape, color):
self.x = x
self.y = y
self.shape = shape
self.color = color
self.dx = 0
self.dy = 0
self.heading = 0
self.da = 0
self.thrust = 0.0
self.acceleration = 0.001
self.health = 100
self.max_health = 100
self.width = 20
self.height = 20

def is_collision(self, other):
if self.x < other.x + other.width and\
self.x + self.width > other.x and\
self.y < other.y + other.height and\
self.y + self.height > other.y:
return True
else:
return False

def update(self):
self.heading += self.da
self.heading %= 360

self.dx += * self.thrust
self.dy += * self.thrust

self.x += self.dx
self.y += self.dy

self.border_check()

def border_check(self):
if self.x > game.width/2.0 - 10:
self.x = game.width/2.0 - 10
self.dx *= -1

elif self.x < -game.width/2.0 + 10:
self.x = -game.width/2.0 + 10
self.dx *= -1

if self.y > game.height/2.0 - 10:
self.y = game.height/2.0 - 10
self.dy *= -1

elif self.y < -game.height/2.0 + 10:
self.y = -game.height/2.0 + 10
self.dy *= -1

def render(self, pen):
pen.goto(self.x, self.y)
pen.setheading(self.heading)
pen.shape(self.shape)
pen.color(self.color)
pen.stamp()



def render_health_meter(self, pen):
# Draw health meter
pen.goto(self.x - 10, self.y + 20)
pen.width(3)
pen.pendown()
pen.setheading(0)

if self.health/self.max_health < 0.3:
pen.color("red")
elif self.health/self.max_health < 0.7:
pen.color("yellow")
else:
pen.color("green")

pen.fd(20 *

if self.health != self.max_health:
pen.color("grey")
pen.fd(20 *

pen.penup()

class Player(Sprite):
def __init__ (self, x, y, shape, color):
Sprite.__init__(self, 0, 0, shape, color)
self.Lives = 3
self.score = 0
self.heading = 90
self.da = 0

def rotate_left(self):
self.da = 0.3

def rotate_right(self):
self.da = -0.3

def stop_rotation(self):
self.da = 0

def accelerate(self):
self.thrust += self.acceleration

def decelerate(self):
self.thrust = 0.0

def render(self, pen):
pen.shapesize(0.5, 1.0, None)
pen.goto(self.x, self.y)
pen.setheading(self.heading)
pen.shape(self.shape)
pen.color(self.color)
pen.stamp()

pen.shapesize(1.0, 1.0, None)



class Enemy(Sprite):
def __init__ (self, x, y, shape, color):
Sprite.__init__(self, x, y, shape, color)

class Powerup(Sprite):
def __init__ (self, x, y, shape, color):
Sprite.__init__(self, x, y, shape, color)

# Create game object
game = Game(1000, 300)

# Create player sprite
player = Player(0, 0, "triangle", "white")

enemy = Enemy(0, 100, "square", "red")
enemy.dx = -0.1
enemy.dy = -0.03

powerup = Powerup(0, -100, "circle", "blue")
powerup.dy = 0.1
powerup.dx = 0.01

# Sprites list
sprites = []
sprites.append(player)
sprites.append(enemy)
sprites.append(powerup)

# Keyoard Bindings
wn.listen()
wn.onkeypress(player.rotate_left, "Left")
wn.onkeypress(player.rotate_right, "Right")

wn.onkeyrelease(player.stop_rotation, "Left")
wn.onkeyrelease(player.stop_rotation, "Right")

wn.onkeypress(player.accelerate, "Up")
wn.onkeyrelease(player.decelerate, "Up")

# Main Loop
while True:
# Clear screen
pen.clear()

# Do game stuff
# Update sprites
for sprite in sprites:
sprite.update()

# Check for collisions
for sprite in sprites:
if isinstance(sprite, Enemy):
if player.is_collision(sprite):
player.x = 0
player.y = 0

if
sprite.x = -100
sprite.y = -100

if isinstance(sprite, Powerup):
if player.is_collision(sprite):
sprite.x = 100
sprite.y = 100

if
sprite.x = +100
sprite.y = -100


# Render sprites
for sprite in sprites:
sprite.render(pen)

game.render_border(pen)

# Update the screen
wn.update()

simonechannel