Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
help with game
#15
Found no solution to tkinter annoying keypress. Just compare tkinter vs pysfml.
Here pysfml example. I would recommend pygame because of better docs.
from sfml import sf

class Bullet:
    def __init__(self, x, y, color, movement):
        self.bullet = sf.CircleShape(3, 18)
        self.bullet.position = x, y
        self.bullet.fill_color = color
        self.movement = movement
        
    def move(self, speed):
        vector = (self.movement[0] * speed + self.bullet.position.x,
                  self.movement[1] * speed + self.bullet.position.y)
        self.bullet.position = vector

class App:
    def __init__(self):
        self.window = sf.RenderWindow(sf.VideoMode(600, 400), "Invaders")
        
        self.bullets = []
        self.create_ship(300, 370, sf.Color(0,0,200))
        
    def create_ship(self, x, y, color):
        self.ship = sf.ConvexShape(3)
        self.ship.set_point(0, (0, 0))
        self.ship.set_point(1, (-20, 30))
        self.ship.set_point(2, ( 20, 30))
        self.ship.fill_color = color
        self.ship.position = x, y
        
    def loop(self):
        clock = sf.Clock()
        bullet_clock = sf.Clock()
        while self.window.is_open:            
            for event in self.window.events:
                if event.type == sf.Event.CLOSED:
                    self.window.close()
                elif event.type == sf.Event.KEY_PRESSED:
                    if sf.Keyboard.is_key_pressed(sf.Keyboard.ESCAPE):
                        self.window.close()
                        
            # create bullets
            if sf.Keyboard.is_key_pressed(sf.Keyboard.SPACE):
                # keeps bullet at steady intervals
                if bullet_clock.elapsed_time.milliseconds > 300:
                    bullet_clock.restart()
                    vector = self.ship.position
                    self.bullets.append(Bullet(vector.x, vector.y, sf.Color(0,0,200), (0,-2)))
                            
            # movement, speed keeps movement steady
            speed = 100 * clock.elapsed_time.seconds
            if sf.Keyboard.is_key_pressed(sf.Keyboard.LEFT):
                self.ship.move((-speed, 0))
            elif sf.Keyboard.is_key_pressed(sf.Keyboard.RIGHT):
                self.ship.move((speed, 0))
            elif sf.Keyboard.is_key_pressed(sf.Keyboard.UP):
                self.ship.move((0, -speed))
            elif sf.Keyboard.is_key_pressed(sf.Keyboard.DOWN):
                self.ship.move((0, speed))            
            
            self.window.clear()    
            # draw bullets
            bullet_remove = []
            for enum, bullet in enumerate(self.bullets):
                if bullet.bullet.position.y < 0:
                    bullet_remove.append(enum)
                else:
                    bullet.move(speed)                
                    self.window.draw(bullet.bullet)
                    
            for enum, index in enumerate(bullet_remove):
                self.bullets.pop(index - enum)
            
            self.window.draw(self.ship)
            self.window.display()
            clock.restart()
            sf.sleep(sf.milliseconds(5))

def main():
    app = App()
    app.loop()
    
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