Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
help with game
#18
@metulburr yeah for some reason i thought showing how it works in tkinter would be better. Instead showing what possible with pygame and pysfml. Still looking for a way to do it in tkinter. If there one. Unless it a linux side problem.

@hammza
Hope this help to show you has basic this is. Added more comments.
Anything you think is still advanced let me know. I show you how basic it is.
import tkinter as tk

# this makes a tuple (600, 400). tuples can be index
SCREEN = 600, 400

# Object
class Bullet:
    # movement takes a tuple (x, y)
    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

    #object method. self refers to object
    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
        # this is from inheriting tk.Frame and calls __init__.
        # so the App gains all variables and methods from tk.Frame
        tk.Frame.__init__(self, master)
        self.pack()
        # self.canvas is an object variable
        self.canvas = tk.Canvas(self, width=SCREEN[0], height=SCREEN[1])
        self.canvas.pack()
        # just a python list
        self.bullets = [] # store bullets
        # calls method. App.tick_loop(object)
        self.tick_loop() # start the tick loop
        # calls method. object.ship = App.create_ship(object, 300, 370, "blue")
        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 = []
        # upacking iterable [(0, object(Bullet)), (1, object(Bullet)), etc]
        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 popping 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()

# __name__ only equals '__main__' if it is execute
# if import then it equals module name
if __name__ == '__main__':
    main()
99 percent of computer problems exists between chair and keyboard.
Reply


Messages In This Thread
help with game - by hammza - Oct-30-2017, 04:34 PM
RE: help with game - by heiner55 - Nov-11-2017, 03:24 PM
RE: help with game - by hammza - Nov-11-2017, 03:29 PM
RE: help with game - by heiner55 - Nov-11-2017, 05:25 PM
RE: help with game - by hammza - Nov-12-2017, 12:56 AM
RE: help with game - by heiner55 - Nov-13-2017, 06:13 AM
RE: help with game - by hammza - Nov-13-2017, 09:12 AM
RE: help with game - by metulburr - Nov-13-2017, 01:01 PM
RE: help with game - by heiner55 - Nov-13-2017, 01:46 PM
RE: help with game - by hammza - Nov-13-2017, 07:23 PM
RE: help with game - by Windspar - Nov-13-2017, 05:16 PM
RE: help with game - by metulburr - Nov-13-2017, 08:52 PM
RE: help with game - by Windspar - Nov-13-2017, 09:11 PM
RE: help with game - by heiner55 - Nov-14-2017, 05:56 AM
RE: help with game - by Windspar - Nov-14-2017, 02:06 PM
RE: help with game - by Windspar - Nov-14-2017, 08:40 PM
RE: help with game - by metulburr - Nov-14-2017, 09:23 PM
RE: help with game - by hammza - Dec-06-2017, 02:37 PM
RE: help with game - by Windspar - Nov-14-2017, 10:15 PM
RE: help with game - by Windspar - Nov-18-2017, 02:50 AM
RE: help with game - by hammza - Dec-06-2017, 01:13 PM
RE: help with game - by Windspar - Dec-06-2017, 02:47 PM
RE: help with game - by hammza - Dec-07-2017, 01:07 AM
RE: help with game - by Windspar - Dec-07-2017, 03:37 AM
RE: help with game - by hammza - Dec-07-2017, 06:00 AM
RE: help with game - by Windspar - Dec-07-2017, 03:48 PM
RE: help with game - by hammza - Dec-07-2017, 05:30 PM
RE: help with game - by Windspar - Dec-07-2017, 09:24 PM
RE: help with game - by hammza - Dec-08-2017, 02:10 AM
RE: help with game - by Windspar - Dec-08-2017, 02:44 AM
RE: help with game - by hammza - Dec-08-2017, 12:45 PM
RE: help with game - by Windspar - Dec-08-2017, 01:37 PM
RE: help with game - by hammza - Dec-08-2017, 03:09 PM
RE: help with game - by Windspar - Dec-08-2017, 03:17 PM
RE: help with game - by hammza - Dec-08-2017, 03:22 PM
RE: help with game - by Windspar - Dec-08-2017, 03:28 PM
RE: help with game - by hammza - Dec-08-2017, 03:30 PM
RE: help with game - by Windspar - Dec-08-2017, 03:39 PM
RE: help with game - by Windspar - Dec-08-2017, 11:17 PM
RE: help with game - by hammza - Dec-09-2017, 01:05 AM
RE: help with game - by Windspar - Dec-09-2017, 01:19 AM
RE: help with game - by hammza - Dec-09-2017, 01:25 AM

Forum Jump:

User Panel Messages

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