Python Forum
Adding a single player mode to my wxOthello game
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Adding a single player mode to my wxOthello game
#5
One way is to create a value for each play area.
You can have many values.
Corners would have best place value.
Change value. How many would change to your color.
And whatever else you can think of.

Then you can have computer choose the best option or second best option.
Depends how smart you want the computer to be.

(Apr-20-2019, 03:41 AM)keames Wrote: That's good advice. I haven't used pygame since 2014! How is it over there in pygamia? I'd be surprised if they didn't update it since then. Do you still have to write the event loop yourself or did they implement mainloop and bind functions?
Why would they need to change to widget event system. If you wanted that in pygame. It really easy to do.
Here one way of doing it.
import pygame

class State:
    def __init__(self):
        self._bind_event = {}

    def bind(self, event, callback):
        if self._bind_event.get(event, False):
            self._bind_event[event].append(callback)
        else:
            self._bind_event[event] = [callback]

    def draw(self, surface): pass
    def _event(self, event):
        group = self._bind_event.get(event.type, False)
        if group:
            for callback in group:
                callback(event)

    def update(self): pass

class StateMachine:
    @classmethod
    def setup(cls, caption, width, height):
        # Basic Pygame Setup
        pygame.display.set_caption(caption)
        cls.surface = pygame.display.set_mode((width, height))
        cls.rect = cls.surface.get_rect()
        cls.clock = pygame.time.Clock()
        cls.running = False
        cls.delta = 0
        cls.fps = 30

        # State Interface
        cls.states = {}
        cls.state = State()

    @classmethod
    def mainloop(cls):
        cls.running = True
        while cls.running:
            for event in pygame.event.get():
                cls.state._event(event)

            cls.state.update()
            cls.state.draw(cls.surface)
            pygame.display.flip()
            cls.delta = cls.clock.tick(cls.fps)

class Scene(State):
    def __init__(self):
        State.__init__(self)
        self.bind(pygame.MOUSEBUTTONDOWN, self.on_mousebuttondown)
        self.bind(pygame.QUIT, self.on_quit)

    def draw(self, surface):
        surface.fill(pygame.Color("navy"))

    def on_mousebuttondown(self, event):
        print(event.button)

    def on_quit(self, event):
        StateMachine.running = False

def main():
    pygame.init()
    StateMachine.setup("Example", 400, 300)
    StateMachine.state = Scene()
    StateMachine.mainloop()
    pygame.quit()

main()
99 percent of computer problems exists between chair and keyboard.
Reply


Messages In This Thread
RE: Adding a single player mode to my wxOthello game - by Windspar - Apr-20-2019, 11:47 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGame] adding mouse control to game flash77 7 614 May-21-2024, 01:05 AM
Last Post: XavierPlatinum
  get a game to run in full screen mode agencyclearly 1 441 May-12-2024, 11:23 AM
Last Post: menator01
  Creating a “Player” class, and then importing it into game onizuka 4 3,183 Sep-01-2020, 06:06 PM
Last Post: onizuka
  Adding an inventory and a combat system to a text based adventure game detkitten 2 7,041 Dec-17-2019, 03:40 AM
Last Post: detkitten
  Adding persistent multiple levels to game michael1789 2 2,497 Nov-16-2019, 01:15 AM
Last Post: michael1789
  Can a player play game created with Python without installing Python? hsunteik 3 5,437 Feb-23-2017, 10:44 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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