Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """ I WOULD FINISH THIS BUT SINCE ITS IMPOSSIBLE TO DELETE TURTLES, THE GAME GETS EXTREMELY LAGGY. I WILL NOW WORK ON PACMAN. """
- from turtle import *
- import time
- import re
- from tkinter import messagebox
- import tkinter
- # Name Definition
- name = input("What is your name?")
- while ":" in name:
- name = input("Sorry, please do not use a colon in your name.")
- # Keybind Setup (Quite inefficient)
- def wasdSetup():
- global leftBind
- global rightBind
- global keybinds
- leftBind = "a"
- rightBind = "d"
- keybinds = "WD"
- tk.destroy()
- def arrowSetup():
- global leftBind
- global rightBind
- global keybinds
- leftBind = "Left"
- rightBind = "Right"
- keybinds = "ARROWS"
- tk.destroy()
- # Button Widgets
- tk = tkinter.Tk()
- tk.title("Choose your input method.")
- tk.minsize(500, 100)
- tk.resizable(False, False)
- frame = tkinter.Frame(tk)
- frame.pack()
- option1 = tkinter.Button(frame, text = "WD", command = wasdSetup)
- option1.pack(side = tkinter.LEFT)
- option2 = tkinter.Button(frame, text = "Arrows", command = arrowSetup)
- option2.pack(side = tkinter.RIGHT)
- tk.mainloop()
- # Window Setup
- window = Screen()
- def windowSetup(): # Function so it can be called at end of game
- width = 600
- height = 600
- window.setup(width, height)
- window.tracer(0) # Turn off automatic window updates
- window.bgcolor("#000000")
- window.title("Space Invaders")
- windowSetup()
- # Player setup
- player = Turtle()
- player.speed(0)
- player.facing = "idle" # Start without moving
- player.shape("square")
- player.color("#ffffff")
- player.penup()
- player.goto(0, -280)
- # Enemy variables
- eVar = Turtle()
- eVar.movementsNeeded = 4
- eVar.movementsComplete = 0
- eVar.movementDirection = "right"
- # Shape registry
- window.register_shape("bullet.gif")
- # Score turtle
- currentScore = Turtle()
- currentScore.hideturtle()
- bullets = []
- enemies = []
- delay = 0.075
- score = 0
- def left():
- player.facing = "left"
- def right():
- player.facing = "right"
- def shoot():
- bullet = Turtle()
- bullet.speed(0)
- bullet.shape("bullet.gif")
- bullet.penup()
- bullet.goto(player.xcor(), player.ycor() + 20)
- bullets.append(bullet)
- def cheat():
- xPos = -200
- yPos = -200
- for i in range(54):
- bullet = Turtle()
- bullet.speed(0)
- bullet.shape("bullet.gif")
- bullet.penup()
- bullet.goto(xPos, yPos)
- bullets.append(bullet)
- xPos += 30
- if xPos > 250:
- yPos += 30
- xPos = -250
- # Keybind listener
- window.listen()
- window.onkeypress(left, leftBind)
- window.onkeypress(right, rightBind)
- window.onkeypress(shoot, "space")
- window.onkeypress(cheat, "c")
- def createEnemies(amount):
- xPos = -200
- yPos = 270
- for i in range(amount):
- enemy = Turtle()
- enemy.speed(0)
- enemy.shape("square")
- enemy.color("#ffffff")
- enemy.penup()
- enemy.downRequired = False
- enemy.movementDir = "right"
- enemy.movedDown = False
- enemy.goto(xPos, yPos)
- enemies.append(enemy)
- xPos += 30
- if xPos > 200:
- yPos -= 30
- xPos = -200
- def movePlayer():
- if player.facing == "left":
- player.setx(player.xcor() - 20)
- if player.facing == "right":
- player.setx(player.xcor() + 20)
- def moveBullets():
- for bullet in bullets:
- if bullet.ycor() < 320:
- bullet.sety(bullet.ycor() + 20)
- else:
- bullets.remove(bullet)
- def moveEnemies():
- print(score)
- for e in enemies:
- if eVar.movementDirection == "right":
- e.setx(e.xcor() + 20)
- elif eVar.movementDirection == "left":
- e.setx(e.xcor() - 20)
- eVar.movementsComplete += 1
- createEnemies(42)
- enemyMovementDelay = 10
- enemyMovementCounter = 0
- hasMovedCounter = 0
- # Constant loop
- while True:
- window.update()
- if len(enemies) == 0:
- createEnemies(42)
- score += 1000
- if enemyMovementDelay != 3:
- enemyMovementDelay -= 1
- enemyMovementCounter = 0
- eVar.movementsNeeded = 4
- eVar.movementsComplete = 0
- eVar.movementDirection = "right"
- # Write score
- currentScore.clear()
- currentScore = Turtle()
- currentScore.pencolor("#ffffff")
- currentScore.hideturtle()
- currentScore.penup()
- currentScore.speed(0)
- currentScore.goto(-270, -270)
- currentScore.write(score, font=("Arial", 25, "normal"))
- if player.xcor() > 290:
- player.goto(player.xcor() - 580, player.ycor())
- elif player.xcor() < -290:
- player.goto(player.xcor() + 580, player.ycor())
- movePlayer()
- moveBullets()
- if enemyMovementCounter == enemyMovementDelay:
- if eVar.movementsComplete == eVar.movementsNeeded:
- for e in enemies:
- e.sety(e.ycor() - 20)
- eVar.movementsComplete = 0
- eVar.movementsNeeded = 8
- enemyMovementCounter = 0
- if eVar.movementDirection == "right":
- eVar.movementDirection = "left"
- else:
- eVar.movementDirection = "right"
- else:
- moveEnemies()
- enemyMovementCounter = 0
- for e in enemies:
- for b in bullets:
- if e.distance(b) < 20:
- enemies.remove(e)
- e.goto(-1500, -1500)
- bullets.remove(b)
- b.reset()
- score += 10
- enemyMovementCounter += 1
- hasMovedCounter += 1
- time.sleep(delay)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement