jeudi 4 mai 2017

Testing for object collision in a Canvas Tkinter Python, and variable is not updating

I am creating a game where the player is a ship that has to dodge meteors that fall. I have 2 classes, the ship and the meteors. The meteors "fall" by having their canvas objects moved down their y axis and their y coordinates are subtracted by the number as the move function. I have an if statement that detects whether the meteors have fallen passed the border of the canvas, and it deletes those meteors, and creates new meteors at the top of the screen, thus making it seem that there are multiple meteors that are falling. The ship class has a similar function that detects if the ship has gone passed the sides of the canvas, which triggers the death function. I have a function in the meteor class that is supposed to detect if the ship and meteors collide. it first detects if they are on the same y value. That works. If they are an if statement detects if they have the same x value. Here is where i'm having my problem. to make it detect if any part of the ship has the meteor on it I coded this: if self.hitbox >= ship1.hitbox3 and self.hitbox <=ship1.hitbox2: This only works occasionally, and I'm not sure how else to implement this. My other problem is in a function that is made to make the game more difficult. I have an if statement that if this variable is less that 3, add 1 to the variable. If it is 3, add 1 to the speed of the meteors. The problem is the speed won't go passed 2, and I can't figure out why. it's not making a separate local value or creating a variable out of the class. Code:

from random import *
from tkinter import *
from time import *
from winsound import *
from lol_u_died import*
root=Tk()
c = Canvas(width=800,height=600,bg="#37061a")
c.pack()
m1=0
m2=0
m3=0
m4=0
m5=0
m6=0
m7=0
m8=0
direction=0
speed=0
cont=True
class ship:
    def __init__(self,x1,y1,x2,y2):
        self.x1=x1
        self.y1=y1
        self.x2=x2
        self.y2=y2       
        self.hitbox=280
        self.hitbox2=420 + x1
        self.hitbox3=353 + x1
        self.shape=c.create_polygon(353+x1,380+y1,387.5+x1,310+y1,
                                    420+x1,380+y1,fill="Blue")
    def move(self):
     global direction
     if direction=="L":
      self.x1 = self.x1-5
      self.hitbox3 = self.hitbox3-5
      self.hitbox2 = self.hitbox2-5
      c.move(self.shape,-5,0)
      sleep(0.01)
      root.update()
     if direction=="R":
      self.x1 = self.x1+5
      self.hitbox3 = self.hitbox3+5
      self.hitbox2 = self.hitbox2+5
      c.move(self.shape,5,0)
      root.update()
      self.test_lost_in_space()
      sleep(0.01)
    def death(self):
     death_sound()
    def test_lost_in_space(self):
        if self.hitbox3<=0:
            self.death()
        if self.hitbox3 >=800:
            self.death()
class meteor:
 def __init__(self,x1,y1):
     self.x1=x1
     self.y1=y1
     self.hitbox=89+x1
     self.speed=1
     self.shape =c.create_polygon(1+x1,50+y1,34+x1,23+y1,67+x1,23+y1,
                                  89+x1,57+y1,64+x1,71+y1,27+x1,71+y1,fill="brown")
 def meteor_return(self):
     global m1
     global m2
     global m3
     global m4
     global m5
     global m6
     global m7
     global m8
     global speed
     if self.y1 >=600:
      c.delete(self)
      m1=meteor(randrange(0,720),randrange(6,12))
      m2=meteor(randrange(0,720),randrange(6,12))
      m3=meteor(randrange(0,720),randrange(6,12))
      m4=meteor(randrange(0,720),randrange(6,12))
      m5=meteor(randrange(0,720),randrange(6,12))
      m6=meteor(randrange(0,720),randrange(6,12))
      m7=meteor(randrange(0,720),randrange(6,12))
      m8=meteor(randrange(0,720),randrange(6,12))
      if speed!=3:
         speed=speed +1
     if speed==3:
         if self.speed!=5:
          self.speed=self.speed +1
          speed=0
 def meteor_fall(self):
     global speed
     self.y1 = self.y1 + 3
     c.move(self.shape,0,3)
     root.update()
     self.meteor_return()
     self.ship_explode()
 def ship_explode(self):
        if self.y1==ship1.hitbox:
            if self.hitbox >= ship1.hitbox3 and self.hitbox <=ship1.hitbox2:
             print(11)
def ship_move(event):
    global direction
    if event.keysym=="a":
     direction="L"
     ship1.move()
    if event.keysym=="d":
     direction="R"
     ship1.move()
ship1 =ship(0,0,0,0)
m1=meteor(randrange(0,200),randrange(6,12))
m2=meteor(randrange(200,400),randrange(6,12))
m3 =meteor(randrange(400,600),randrange(6,12))
m4=meteor(randrange(600,800),randrange(6,12))
m5 =meteor(randrange(400,600),randrange(6,12))
m6=meteor(randrange(600,800),randrange(6,12))
m7 =meteor(randrange(400,600),randrange(6,12))
m8=meteor(randrange(600,800),randrange(6,12))
c.bind_all("<KeyPress-a>",ship_move)
c.bind_all("<KeyPress-d>",ship_move)
while cont ==True:
    m1.meteor_fall()
    m2.meteor_fall()
    m3.meteor_fall()
    m4.meteor_fall()
    m5.meteor_fall()
    m6.meteor_fall()
    m7.meteor_fall()
    m8.meteor_fall()
c.bind_all("<KeyPress-a>",ship_move)
c.bind_all("<KeyPress-d>",ship_move)
ship1.death()

Aucun commentaire:

Enregistrer un commentaire