Hello guest, if you read this it means you are not registered. Click here to register in a few simple steps, you will enjoy all features of our Forum.
Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Pygame code help
#1
Here is my current code, i am trying to make a pause event so when the user presses P the game will pause and when the user presses C the game will continue, can someone please show me how they would add onto this code?


import turtle
import os
import math
import random

#set up the screen
wn = turtle.Screen()
wn.bgcolor("black")
wn.title("Space invaders")
wn.bgpic("space_invaders_backgrounds.gif")

#Register the shapes
turtle.register_shape("ship.gif")
turtle.register_shape("Enemy.gif")

#Drawing my border
border_pen = turtle.Turtle()
border_pen.speed(0)
border_pen.color("white")
border_pen.penup()
border_pen.setposition(-300,-300)
border_pen.pendown()
border_pen.pensize(3)
for side in range(4):
    border_pen.fd(600)
    border_pen.lt(90)
border_pen.hideturtle()

#Set the score to 0
score = 0

#Draw the score
score_pen = turtle.Turtle()
score_pen.speed(0)
score_pen.color("white")
score_pen.penup()
score_pen.setposition(-290, 280)
scorestring = "Score: %s" %score
score_pen.write(scorestring, False, align="left", font=("Arial", 14, "normal"))
score_pen.hideturtle()


#create the player turtle
player = turtle.Turtle()
player.color("blue")
player.shape("ship.gif")
player.penup()
player.speed(0)
player.setposition(0, -250)
player.setheading(90)

playerspeed = 18

#Choose a number of enemies
number_of_enemies = 6
#Create and empty list of enemies
enemies = []

#Add enemies to the list
for i in range(number_of_enemies):
    #Create the enemy
    enemies.append(turtle.Turtle())
    

for enemy in enemies:
    enemy.color("red")
    enemy.shape("Enemy.gif")
    enemy.penup()
    enemy.speed(0)
    x = random.randint(-200, 200)
    y = random.randint(100, 250)
    enemy.setposition(x,y)

enemyspeed = 3

#Create the player's bullet
bullet = turtle.Turtle()
bullet.color("yellow")
bullet.shape("triangle")
bullet.penup()
bullet.speed(0)
bullet.setheading(90)
bullet.shapesize(0.5, 0.5)
bullet.hideturtle()

bulletspeed = 25

#Define bullet state
#ready - ready to fire
#fire - bullet is firing
bulletstate = "ready"


#Move the player left and right
def move_left():
    x = player.xcor()
    x -= playerspeed
    if x < -280:
        x = - 280
    player.setx(x)

def move_right():
    x = player.xcor()
    x += playerspeed
    if x > 280:
        x = 280
    player.setx(x)

def fire_bullet():
    #Declare bulletstate as a global if needs changing
    global bulletstate
    if bulletstate == "ready":
        bulletstate = "fire"
        #Move the bullet just above the player
        x = player.xcor()
        y = player.ycor() + 10
        bullet.setposition(x,y)
        bullet.showturtle()

def isCollision(t1, t2):
    distance = math.sqrt(math.pow(t1.xcor()-t2.xcor(),2)+math.pow(t1.ycor()-t2.ycor(),2))
    if distance < 15:
        return True
    else:
        return False




#Create keyboard bindings
turtle.listen()
turtle.onkey(move_left,"Left")
turtle.onkey(move_right,"Right")
turtle.onkey(fire_bullet,"space")

#Main game loop
while True:

    for enemy in enemies:
        #Move the enemy
        x = enemy.xcor()
        x += enemyspeed
        enemy.setx(x)

        #Move the enemy back and down
        if enemy.xcor() > 280:
            #Move all enemies down
            for e in enemies:
                y = e.ycor()
                y -= 40
                e.sety(y)
            #Change the enemy direction
            enemyspeed *= -1
            
        if enemy.xcor() < -280:
            #Move all enemies down
            for e in enemies:             
                y = e.ycor()
                y -= 40
                e.sety(y)
            #Change enemy direction
            enemyspeed *= -1

        #Check for a collision between the bullet and the enemy
        if isCollision(bullet, enemy):
            #Reset the bullet
            bullet.hideturtle()
            bulletstate = "ready"
            bullet.setposition(0, -400)
            #Reset the enemy
            x = random.randint(-200, 200)
            y = random.randint(100, 250)
            enemy.setposition(x,y)
            #Update the score
            score += 10
            scorestring = "Score: %s" %score
            score_pen.clear()
            score_pen.write(scorestring, False, align="left", font=("Arial", 14, "normal"))

        if isCollision(player, enemy):
            player.hideturtle()
            enemy.hideturtle()
            print("Game over")
            break

    #Move the bullet
    if bulletstate == "fire":
        
        y = bullet.ycor()
        y += bulletspeed
        bullet.sety(y)

    #Check to see if the bullet has gone to the top
    if bullet.ycor() > 275:
        bullet.hideturtle()
        bulletstate = "ready"



delay = input("press enter to finish")





Quote
#2
Quote:
#Main game loop
while True:

Before your while loop, you'd need to set event handlers that are bound to those keys, using Turtle.onkeypress: https://docs.python.org/3.6/library/turt...onkeypress
And then you'd also want to have some sort of state machine to determine what you should be doing.  Something like:
class State:
    Running = 0
    Paused = 1

current_state = State.Running

def on_pause():
    global current_state
    current_state = State.Paused

def on_continue():
    global current_state
    current_state = State.Running

turtle.onkeypress(on_pause, "k")
turtle.onkeypress(on_continue, "c")

while True:
    if current_state == State.Running:
        # your current while loop goes here, so it only does anything if the game is running
It's dangerous to go alone!  Take this: https://docs.python.org/3/library/index.html
Kevin Flynn is my spirit animal.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  [PyGame] Problem importing pygame / installing pygame Klar 4 164 Dec-16-2017, 05:48 PM
Last Post: Klar
  [PyGame] How much python should I learn before starting Pygame jakegold98 4 124 Dec-06-2017, 10:29 PM
Last Post: Larz60+
  Python Pygame help pause event Trajme 0 120 Dec-06-2017, 09:14 PM
Last Post: Trajme
  Appropriately delay this PyGame code kleynah22 2 155 Nov-09-2017, 02:00 PM
Last Post: Windspar
  [Pygame] Avoid SentDex pygame tutorials georgecoopers 1 622 Apr-09-2017, 12:40 PM
Last Post: metulburr

Forum Jump:


Users browsing this thread: 1 Guest(s)