Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
help with game
#1
hi every one Smile

I'm trying to make a game , i need help to make the bullet start from the ship

i don't know how to use coords

this is the code

from tkinter import *
import random
import time

WIDTH=600
HEIGHT=400

tk=Tk()
c=Canvas(tk,width=WIDTH,height=HEIGHT)
c.pack()



def ship(a):
    c.create_polygon(a,a/2,a-20,a/2+30,a+20,a/2+30,fill="blue")
    return a


def bullet(i):
    c.create_oval(i,i,i+5,i+5,fill="blue")


#=================== KEYS =====================================
def go_left(event):
    c.move(1,-5,0)
    c.update()

def go_right(event):
    c.move(1,5,0)
    c.update()

def go_up(event):
    c.move(1,0,-5)
    c.update()

def go_down(event):
    c.move(1,0,5)
    c.update()

def spaceKey(event=0):
    c.move(2,0,-100)
    c.update()


ship=ship(300)
bult=bullet(298)

tk.bind("<Left>", go_left)
tk.bind("<Right>", go_right)
tk.bind("<Up>", go_up)
tk.bind("<Down>", go_down)
tk.bind('<space>',spaceKey)


tk.mainloop()
Reply
#2
See https://python-forum.io/Forum-Game-Tutorials
Reply
#3
(Nov-11-2017, 03:24 PM)heiner55 Wrote: See https://python-forum.io/Forum-Game-Tutorials

Thank you very much

But Iam looking for a canvas solution without pygame
Reply
#4
>> make the bullet start from the ship

def create_bullet(i):
    return c.create_oval(i,i/2,i+5,i/2+5,fill="blue")
Reply
#5
(Nov-11-2017, 05:25 PM)heiner55 Wrote: >> make the bullet start from the ship

def create_bullet(i):
    return c.create_oval(i,i/2,i+5,i/2+5,fill="blue")

Thank you, I'll try this tomorrow Smile
Reply
#6
I took the liberty of changing some names in your program

from tkinter import *
from time import time, sleep
import random

WIDTH =600
HEIGHT=400

tk=Tk()
cs=Canvas(tk,width=WIDTH,height=HEIGHT)
cs.pack()

def create_ship(x,y):
    return cs.create_polygon(x,y, x-20,y+30, x+20,y+30, fill="blue")

def create_bull(x,y):
    return cs.create_oval(x-3,y-3, x+3,y+3, fill="blue")

def go_left(event):
    cs.move(ship,-5,0)
    cs.update()

def go_right(event):
    cs.move(ship,5,0)
    cs.update()

def go_up(event):
    cs.move(ship,0,-5)
    cs.update()

def go_down(event):
    cs.move(ship,0,5)
    cs.update()

def spaceKey(event=0):
    cs.move(bult,0,-50)
    cs.update()

ship = create_ship(300,200)
bult = create_bull(300,200)

tk.bind("<Left>",  go_left)
tk.bind("<Right>", go_right)
tk.bind("<Up>",    go_up)
tk.bind("<Down>",  go_down)
tk.bind('<space>', spaceKey)

tk.mainloop()
Reply
#7
(Nov-13-2017, 06:13 AM)heiner55 Wrote: I took the liberty of changing some names in your program

from tkinter import *
from time import time, sleep
import random

WIDTH =600
HEIGHT=400

tk=Tk()
cs=Canvas(tk,width=WIDTH,height=HEIGHT)
cs.pack()

def create_ship(x,y):
    return cs.create_polygon(x,y, x-20,y+30, x+20,y+30, fill="blue")

def create_bull(x,y):
    return cs.create_oval(x-3,y-3, x+3,y+3, fill="blue")

def go_left(event):
    cs.move(ship,-5,0)
    cs.update()

def go_right(event):
    cs.move(ship,5,0)
    cs.update()

def go_up(event):
    cs.move(ship,0,-5)
    cs.update()

def go_down(event):
    cs.move(ship,0,5)
    cs.update()

def spaceKey(event=0):
    cs.move(bult,0,-50)
    cs.update()

ship = create_ship(300,200)
bult = create_bull(300,200)

tk.bind("<Left>",  go_left)
tk.bind("<Right>", go_right)
tk.bind("<Up>",    go_up)
tk.bind("<Down>",  go_down)
tk.bind('<space>', spaceKey)

tk.mainloop()

Thank you very much for your help

Your idea is good

But I'm looking for something else,

What i want is how to make it shoot like (space invaders), I don't know how to use coords, and I didn't found any good tutorial or explanation.


Thank you again Smile
Reply
#8
Quote:What i want is how to make it shoot like (space invaders), I don't know how to use coords, and I didn't found any good tutorial or explanation.
Thats because no one uses tkinter to make games. There is a better library to handle that kind of software and it called pygame. In fact, we have a tutorial on how to make a ship shoot like space invaders on this forum. Tkinter is a GUI library mainly for building window form apps.
Recommended Tutorials:
Reply
#9
This needs big changes.
A class for the ship and a class for the bullet.
Each class take care of the position x and y and has a draw/update function.
Then you need a function which is called every 1/100s.
This function calls all updates functions from your ship and from many bullets.

You should google for ping pong. If you understand ping pong,
you can also code your game.
See https://gist.github.com/calebrob6/4022622
Reply
#10
here an example
import tkinter as tk

SCREEN = 600, 400
        
class Bullet:
    def __init__(self, canvas, x, y, tk_fill, movement):
        self.bullet = canvas.create_oval(x -3 , y - 3, x + 3, y + 3, fill=tk_fill)
        self.movement = movement
        
    def move(self, canvas):
        canvas.move(self.bullet, self.movement[0], self.movement[1])

class App(tk.Frame):
    def __init__(self, master):
        # put canvas in frame so you can have other goodies
        tk.Frame.__init__(self, master)
        self.pack()
        self.canvas = tk.Canvas(self, width=SCREEN[0], height=SCREEN[1])
        self.canvas.pack()
        self.bullets = []# store bullets
        self.tick_loop() # start the tick loop
        self.ship = self.create_ship(300, 370, "blue")
        
        master.bind("<Left>", self.go_left)
        master.bind("<Right>", self.go_right)
        master.bind("<Up>", self.go_up)
        master.bind("<Down>", self.go_down)
        master.bind("<space>", self.space_key)
        
    def create_ship(self, x, y, tk_fill):
        return self.canvas.create_polygon(x, y, x - 20, y + 30, x + 20, y + 30, fill=tk_fill)
        
    def tick_loop(self):
        remove_list =
        for enum, bullet in enumerate(self.bullets):
            if self.canvas.coords(bullet.bullet)[1] < 0:
                remove_list.append(enum)
                self.canvas.delete(bullet.bullet)
            else:
                bullet.move(self.canvas)
                
        for index in remove_list:
            self.bullets.pop(index)
    
        # framerate per seconds 1000/30 = 30 frames roughly
        self.after(int(1000/30), self.tick_loop)        
        
    def go_left(self, event):
        self.canvas.move(self.ship, -5, 0)
        self.canvas.update()
        
    def go_right(self, event):
        self.canvas.move(self.ship, 5, 0)
        self.canvas.update()
        
    def go_up(self, event):
        self.canvas.move(self.ship, 0, -5)
        self.canvas.update()
        
    def go_down(self, event):
        self.canvas.move(self.ship, 0, 5)
        self.canvas.update()
        
    def space_key(self, event=0):
        # only want the first two coords
        x, y = self.canvas.coords(self.ship)[:2]
        # just having shoot straight up
        self.bullets.append(Bullet(self.canvas, x, y, "blue", (0, -10)))

def main():
    root = tk.Tk()
    app = App(root)
    app.mainloop()
    
if __name__ == '__main__':
    main()
Don't like how keypress working but here it is.
Would be easier in pygame or pysfml.

fix poping bug. popping bug so far has no effect on above good. Just added little more stuff.
import tkinter as tk

SCREEN = 600, 400
        
class Bullet:
    def __init__(self, canvas, x, y, tk_fill, movement):
        self.bullet = canvas.create_oval(x -3 , y - 3, x + 3, y + 3, fill=tk_fill)
        self.movement = movement
        
    def move(self, canvas):
        canvas.move(self.bullet, self.movement[0], self.movement[1])

class App(tk.Frame):
    def __init__(self, master):
        # put canvas in frame so you can have other goodies
        tk.Frame.__init__(self, master)
        self.pack()
        self.canvas = tk.Canvas(self, width=SCREEN[0], height=SCREEN[1])
        self.canvas.pack()
        self.bullets = [] # store bullets
        self.tick_loop() # start the tick loop
        self.ship = self.create_ship(300, 370, "blue")
        
        master.bind("<Left>", self.go_left)
        master.bind("<Right>", self.go_right)
        master.bind("<Up>", self.go_up)
        master.bind("<Down>", self.go_down)
        master.bind("<space>", self.space_key)
        master.bind("<b>", self.go_b)
        master.bind("<v>", self.go_v)
        
    def create_ship(self, x, y, tk_fill):
        return self.canvas.create_polygon(x, y, x - 20, y + 30, x + 20, y + 30, fill=tk_fill)
        
    def tick_loop(self):
        remove_list = []
        for enum, bullet in enumerate(self.bullets):
            coords = self.canvas.coords(bullet.bullet)
            if coords[1] < 0 or coords[0] < 0 or coords[2] > SCREEN[0]:
                remove_list.append(enum)
                self.canvas.delete(bullet.bullet)
            else:
                bullet.move(self.canvas)
        
        # fix poping bug        
        for enum, index in enumerate(remove_list):
            self.bullets.pop(index - enum)
    
        # framerate per seconds 1000/30 = 30 frames roughly
        self.after(int(1000/30), self.tick_loop)        
        
    def go_left(self, event):
        self.canvas.move(self.ship, -5, 0)
        self.canvas.update()
        
    def go_right(self, event):
        self.canvas.move(self.ship, 5, 0)
        self.canvas.update()
        
    def go_up(self, event):
        self.canvas.move(self.ship, 0, -5)
        self.canvas.update()
        
    def go_down(self, event):
        self.canvas.move(self.ship, 0, 5)
        self.canvas.update()
        
    def space_key(self, event=0):
        # only want the first two coords
        x, y = self.canvas.coords(self.ship)[:2]
        # just having shoot straight up
        self.bullets.append(Bullet(self.canvas, x, y, "blue", (0, -10)))
    
    # spread shooting
    def go_b(self, event):
        # only want the first two coords
        x, y = self.canvas.coords(self.ship)[:2]        
        #up
        self.bullets.append(Bullet(self.canvas, x, y, "red", (0, -8)))
        #left
        self.bullets.append(Bullet(self.canvas, x, y, "red", (-8, 0)))
        #right
        self.bullets.append(Bullet(self.canvas, x, y, "red", (8, 0)))

    # triple shoot striaght up
    def go_v(self, event):
        # only want the first two coords
        x, y = self.canvas.coords(self.ship)[:2]        
        #up
        self.bullets.append(Bullet(self.canvas, x, y, "green", (0, -5)))
        self.bullets.append(Bullet(self.canvas, x - 50, y, "green", (0, -5)))
        self.bullets.append(Bullet(self.canvas, x + 50, y, "green", (0, -5)))

def main():
    root = tk.Tk()
    app = App(root)
    app.mainloop()
    
if __name__ == '__main__':
    main()
99 percent of computer problems exists between chair and keyboard.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020