Python Game Programming Tutorial: SpaceWar 5

preview_player
Показать описание
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
Рекомендации по теме
Комментарии
Автор

It is really good and so easy to understand because of you

Thank you❤❤

shipraagrawal
Автор

thank u so much for this totorial, i am ten years old and this is my first ever real game thanks again

ashwinp
Автор

#Space Wars
#created by @TokyoEdTech / written in python 2.7 (minor differences for 3.6 which is what I'm using)
import turtle
import os
import random
# required by mac
turtle.fd(0)
#animations speed
turtle.speed(0)
#bgcolour
turtle.bgcolor('black')
#hide turtle
turtle.ht()
#saves memory
turtle.setundobuffer(1)
#this speeds up drawing later
turtle.tracer(1)

class Sprite(turtle.Turtle):
def __init__(self, spriteshape, color, startx, starty):
turtle.Turtle.__init__(self, shape = spriteshape)
self.speed(0)
self.penup()
self.color(color)
self.fd(0)
self.goto(startx, starty)
self.speed = 1

def move(self):
self.fd(self.speed)

#Boundary detection
if self.xcor() > 290:
self.setx(290)
self.rt(60)
if self.xcor() < -290:
self.setx(-290)
self.rt(60)

if self.ycor() > 290:
self.sety(290)
self.rt(60)
if self.ycor() < -290:
self.sety(-290)
self.rt(60)
def is_collision(self, other):
if (self.xcor() >= (other.xcor() - 20)) and \
(self.xcor() <= (other.xcor() + 20)) and \
(self.xcor() >= (other.ycor() - 20)) and \
(self.xcor() <= (other.ycor() + 20)):
return True
else:
return False

class Player(Sprite):
def __init__(self, spriteshape, color, startx, starty):
Sprite.__init__(self, spriteshape, color, startx, starty)
self.speed = 4
self.lives = 3

def turn_left(self):
self.lt(45)
def turn_right(self):
self.lt(-45)
def accelerate(self):
self.speed += 1
def decelerate(self):
self.speed -= 1

class Game():
def __init__(self):
self.level = 1
self.score = 0
self.state = 'playing'
self.pen = turtle.Turtle()
self.lives = 3

def draw_border(self):
#draw border
self.pen.speed(0)
self.pen.color('white')
self.pen.pensize(3)
self.pen.penup()
self.pen.goto(-300, 300)
self.pen.pendown()
for side in range(4):
self.pen.fd(600)
self.pen.rt(90)
self.pen.penup()
self.pen.ht()

class Enemy(Sprite):
def __init__(self, spriteshape, color, startx, starty):
Sprite.__init__(self, spriteshape, color, startx, starty)
self.speed = 6
self.setheading(random.randint(0, 360))

class missile(Sprite):
def __init__(self, spriteshape, color, startx, starty):
Sprite.__init__(self, spriteshape, color, startx, starty)
stretch_len=0.4, outline=None)
self.speed = 20
self.status = 'ready'

def fire(self):
if self.status == 'ready':
self.status = 'firing'
def move(self):
if self.status == 'firing':
self.fd(self.speed)
#create game object
game = Game()

#draw border
game.draw_border()


# create sprites
player = Player('triangle', 'white', 0, 0)
enemy = Enemy('circle', 'red', -100, 0)
missile = Missile('triangle', 'yellow', 0, 0)
#key bindings
turtle.onkey(player.turn_left, 'Left')
turtle.onkey(player.turn_right, 'Right')
turtle.onkey(player.accelerate, 'Up')
turtle.onkey(player.decelerate, 'Down')
turtle.listen()

# main gameloop
while True:
player.move()
enemy.move()
#check for colission
if player.is_collision(enemy):
x = random.randint(-250, 250)
y = random.randint(-250, 250)
enemy.goto(x, y)

gorgestboi
Автор

can we set the mouse button to shoot ?

scuffedgod
Автор

Well, I've made it this far. Thank you so much TokyoEdtech. Your tutorials are helping me and others so much.

teacherinthailan
Автор

I watch all your python vids, please notice me senpai!

creamygoodness
Автор

AttributeError : Missile attribute has no object missile
kindly help

Sam-iqss
Автор

Hi, my missile does not stop advancing, it appears outside the frame in position (-1000, 1000) and it returns to the frame as it advances.

KERDYftVADOR
Автор

Hi
Do you know how to make infite ammo,
Could you make a tutorial on that?

GasimovTV
Автор

When you realise he wrote missile and not self before he did: DA STUDENT HAS BECOME DA MASTER!!

celiacasimiro
Автор

it seems like you skipped alot from video 4 to 5 as far as the syntax is concerned, you have the sprites in 5 but did not start them in video 4.

Success-
Автор

I’m going to ask a few questions through out this most likely, so just letting you know.

gorgestboi
Автор

what's the difference between turtle.onkey and turtle.onkeypress, why is missile.move() always moving? shouldn't be moving only when's space key is pressed?

sebastianjulonchamana
Автор

in "self.fd(self.speed)" what does fd mean?

Ethan-xjhb
Автор

dude why is the animation like slow can I solve this problem ?

ahmadwehbe
Автор

For me it says stretch_len is a syntax error. My code is the same as yours but I will give it to you anyway

gorgestboi
Автор

Is there anyway to make it less laggy?

anthonyzhu
visit shbcf.ru