Python Game Programming Tutorial: SpaceWar 7

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

For Windows sound I have found using winsound to work well. Import winsound then call it with winsound.PlaySound("sound.wav", winsound.SND_FILENAME | winsound.SND_ASYNC)

Innerlight
Автор

when I do the sound effect on my windows, and when I shoot, it makes the windows startup sound and not the laser sound I saved.

rohantummala
Автор

I really tried to make my sound work on Windows, but couldn't for now. Temp. solution I found is this command.
winsound.Beep(frequency, duration)
Beep the PC’s speaker. The frequency parameter specifies frequency, in hertz, of the sound, and must be in the range 37 through 32, 767. The duration parameter specifies the number of milliseconds the sound should last. If the system is not able to beep the speaker, RuntimeError is raised.
So in my code for fire I put roughly
winsound.Beep(1000, 90)

it worked pretty well using computer speaker.

maxim_mahadeva
Автор

import os
import random

#Import the Turtle module
import turtle
#Required by Windows to show the window
turtle.fd(0)
#Set the animations speed to the maximum
turtle.speed(0)
#Change the background color
turtle.bgcolor("black")
#Hide the default turtle
turtle.ht()
#This saves memory
turtle.setundobuffer(1)
#This speeds up drawing
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.lt(60)

if self.xcor() < -290:
self.xcor(-290)
self.lt(60)

if self.ycor() > 290:
self.sety(290)
self.lt(60)

if self.ycor() < -290:
self.sety(-290)
self.lt(60)

def is_collision(self, other):
if (self.xcor() >= (other.xcor() - 20)) and \
(self.xcor() <= (other.xcor() + 20)) and \
(self.ycor() >= (other.ycor() - 20)) and \
(self.ycor() <= (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.rt(45)

def accelerate(self):
self.speed += 1

def decelerate(self):
self.speed -= 1

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 Ally(Sprite):
def __init__(self, spriteshape, color, startx, starty):
Sprite.__init__(self, spriteshape, color, startx, starty)
self.speed = 8
self.setheading(random.randint(0, 360))

class Missile(Sprite):
def __init__(self, spriteshape, color, startx, starty):
Sprite.__init__(self, spriteshape, color, startx, starty)
self.shapesize(stretch_wid=0.3, stretch_len=0.4, outline=None)
self.speed = 20
self.status = "ready"
self.goto(-1000, 1000)

def fire(self):
if self.status == "ready":
self.goto(player.xcor(), player.ycore())

self.status = "firing"

def move(self):

if self.status == "ready":
self.goto(-1000, 1000)


if self.status == "firing":
self.fd(self.speed)

#Border check
if self.xcor() < -290 or self.xcor() > 290 or \
self.ycor()< -290 or self.ycor()> 290:
self.goto(-1000, 1000)
self.status = "ready"


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("yellow")
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()

#Create game object
game = Game()

#Draw the game border
game.draw_border()




#Create my sprites
player = Player("circle", "yellow", 0, 0)
enemy = Enemy("triangle", "red", -100, 0)
missile = Missile("circle", "white", 0, 0)
ally = Ally("circle", "blue", 0, 0)

#Keyboard bindings
turtle.onkey(player.turn_left, "Left")
turtle.onkey(player.turn_right, "Right")
turtle.onkey(player.accelerate, "Up")
turtle.onkey(player.decelerate, "Down")
turtle.onkey(missile.fire, "space")
turtle.listen()


#Main game loop
while True:
player.move()
enemy.move()
missile.move()
ally.move()

#Check for a collision
if player.is_collision(enemy):
x = random.randint(-250, 250)
y = random.randint(-250, 250)
enemy.goto(x, y)

#Check for a collision between the missile and the enemy
if player.is_collision(enemy):
x = random.randint(-250, 250)
y = random.randint(-250, 250)
enemy.goto(x, y)
missile.status = "ready"

#Check for a collision between the missile and the ally
if player.is_collision(ally):
x = random.randint(-250, 250)
y = random.randint(-250, 250)
ally.goto(x, y)
missile.status = "ready"


delay = raw_input("Press enter to finish. > ")

I have some error when i run and hit space.


TypeError: TNavigator.xcor() takes 1 positional argument but 2 were given

TailsFoxyBoy
Автор

Hey, l am unable to add the sound, my python version is 3.7 and l have saved my sound files in the folder in which I have saved my programming... l have my sound file as MP3 file, still l am unable to get the sound...
Please can you tell me what can l do to get the sound??

sanjaytodkar
Автор

I'm getting an invalid syntax error.

#Creates the game object
game=Game()

It highlights game and says "invalid syntax".

jothejoker
Автор

Im sorry but how tf can we run a bg procces in win10 via cmd???

steliosfk
Автор

Do you have a good video to figure out how to get the sound for windows

ZEDsquad
Автор

Couldn't we have done pen.clear() instead of pen.undo() like in your Space Invaders tutorial?

mitchellmadden
Автор

1:22 how to replace the code with the python 3 format?
when i run the game using the same code you did, it print this
"Score:< __main__.Game object at 0x106e312e0"

muazhazimi
Автор

in windows winsound could be an option

keerthivarmanprakash
Автор

import turtle
import random
#Import the turtle module
import turtle
#Required by MacOSX to show the window
turtle.fd(0)
#Set the animations speed to maximum
turtle.speed(0)
#Change the background colour
turtle.bgcolor('black')
#Hide the default turtle
turtle.ht()
#This saves memory
turtle.setundobuffer(1)
#This speeds up drawing
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.ycor() >= (other.ycor() - 20)) and \
(self.ycor() <= (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.rt(45)

def accelerate(self):
self.speed +=1

def decelerate(self):
self.speed -=1

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)
self.shapesize(stretch_wid=0.3, stretch_len=0.4, outline= None)
self.speed = 20
self.status = "ready"
self.goto(-1000, 1000)

def fire(self):
if self.status == "ready":
self.goto(player.xcor(), player.ycor())

self.status ="firing"

def move(self):

if self.status == "ready":
self.goto(-1000, 1000)


if self.status == "firing":
self.fd(self.speed)

#Border check
if self.xcor() <-290 or self.xcor()>290 or \
self.ycor() <-290 or self.ycor()>290:
self.goto(-1000, 1000)
self.status = "ready"



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("Red")
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()

def show_status(self):
msg = "Score:%s" %(self.score)
self.pen.penup()
self.pen.goto(-300, 310)
self.pen.write(msg, font=("Arial", 16, "normal"))


#Create game objects
game = Game
#Draw the game border
game.draw_border()
#Show the game status
game.show_status()

#Create my sprites
player= Player("triangle", "white", 0, 0)
enemy= Enemy("circle", "red", -100, 0)
missile= Missile("triangle", "yellow", 0, 0)


#Keyboard binding
turtle.onkey(player.turn_left, "Left")
turtle.onkey(player.turn_right, "Right")
turtle.onkey(player.accelerate, "Up")
turtle.onkey(player.decelerate, "Down")
turtle.onkey(missile.fire, "space")
turtle.listen()



#Main game loop
while True:
player.move()
enemy.move()
missile.move()


#Check for a collision with the player
if player.is_collision(enemy):
x = random.randint(-250, 250)
y = random.randint(-250, 250)
enemy.goto(x, y)
#Check for collision between the missile and the enemy
if missile.is_collision(enemy):
x = random.randint(-250, 250)
y = random.randint(-250, 250)
enemy.goto(x, y)
missile.status = "ready"



delay=raw_input ("press enter to finish.>")

ERROR- draw_border() missing 1 required positional argument: 'self' and show_status() missing 1 required positional argument: 'self'

Sam-iqss
Автор

I have an invalid syntax error for some reason.

import os
import random

#Import the Turtle module
import turtle
turtle.fd(0)
turtle.speed(0)
turtle.bgcolor("black")
turtle.ht()
turtle.setundobuffer(1)
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.ycor() >= (other.ycor() - 20)) and \
(self.ycor() <= (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 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 Ally(Sprite):
def __init__(self, spriteshape, color, startx, starty):
Sprite.__init__(self, spriteshape, color, startx, starty)
self.speed = 6
self.setheading(random.randint(0, 360))

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

#Boundary detection
if self.xcor() > 290:
self.setx(290)
self.lt(60)

if self.xcor() < -290:
self.setx(-290)
self.lt(60)

if self.ycor() > 290:
self.sety(290)
self.lt(60)

if self.ycor() < -290:
self.sety(-290)
self.lt(60)



class Missile(Sprite):
def __init__(self, spriteshape, color, startx, starty):
Sprite.__init__(self, spriteshape, color, startx, starty)
self.shapesize(stretch_wid=0.3, stretch_len=0.4, outline=None)
self.speed = 20
self.status = "ready"
self.goto(-1000, 1000)

def fire(self):
if self.status == "ready":
self.goto(player.xcor(), player.ycor())

self.status = "firing"


def move(self):

if self.status == "ready":
self.goto(-1000, 1000)

if self.status == "firing":
self.fd(self.speed)

#Border check
if self.xcor() < -290 or self.xcor() > 290 or \
self.ycor()< -290 or self.ycor()> 290:
self.goto(-1000, 1000)
self.status = "ready"


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()

def show_status(self):
msg = "Score: %s" %s(self.score)
self.pen.penup()
self.pen.goto(-300, 310)
self.pen.write(msg, font=("Arial", 16, "normal")

#Create game object
game = Game()

#Draw the game border
game.draw_border()

#Show the game status
game.show_status()

#Create my sprites
player = Player("triangle", "white", 0, 0)
enemy = Enemy("circle", "red", -100, 0)
missile = Missile("triangle", "yellow", 0, 0)
ally = Ally("square", "blue", 0, 0)

#Keyboard bindings
turtle.onkey(player.turn_left, "Left")
turtle.onkey(player.turn_right, "Right")
turtle.onkey(player.accelerate, "Up")
turtle.onkey(player.decelerate, "Down")
turtle.onkey(missile.fire, "space")
turtle.listen()

#Main game loop
while True:
player.move()
enemy.move()
missile.move()
ally.move()


#Check for a collision with the player
if player.is_collision(enemy):
x = random.randint(-250, 250)
y = random.randint(-250, 250)
enemy.goto(x, y)

#Check for a collision between the missile and the enemy
if missile.is_collision(enemy):
x = random.randint(-250, 250)
y = random.randint(-250, 250)
enemy.goto(x, y)
missile.status = "ready"
#Increase the score
game.score += 100
game.show_status()

#Check for a collision between the missile and the ally
if missile.is_collision(ally):
x = random.randint(-250, 250)
y = random.randint(-250, 250)
ally.goto(x, y)
missile.status = "ready"
#Decrease the score
game.score -= 50
game.show_status()

theboiishere
visit shbcf.ru