0

The problem is in class GameScreen: I use a while loop to make sure that when I click "Play Game" that the new screen appears. If this while loop isn't there, the gamescreen blits on the screen for just a second before my main screen once again reappears.

However, if the while loop is there, the while loop causes the imageList to keep running and the cards keep on refreshing because of the loop.

Is there any way to exclude my imageList from the while loop so that the images don't keep refreshing?

import sys, pygame
import tkinter as tk
import deuces
import random

class Controller:
    def __init__(self, location):
    # these are the rgb values
        white = (255, 255, 255)
        #i reference these multiple times throughout for buttons/images/other things
        display_width = 1200
        display_height = 700

        #initialize screen
        pygame.init()
        self.screen = pygame.display.set_mode((display_width, display_height))
        pygame.display.set_caption('Israeli Poker!')

        #initialize backgroundimages
        self.image = pygame.image.load('pokerBackground.png')
        self.screen.blit(self.image, (0, 0))

        self.image3 = pygame.image.load('image3.png')
        self.screen.blit(self.image3, (675, 150))

        # Displays the welcome text
        self.font = pygame.font.SysFont("Arial", 50)
        self.font.set_bold(True)
        self.text = self.font.render("Welcome to Israeli Poker!", 1, white)
        self.textpos = self.text.get_rect()
        self.textpos.centerx = self.screen.get_rect().centerx
        self.screen.blit(self.text, self.textpos)

        self.TextRect = self.font.render("Play Game!", True, white)
        self.screen.blit(self.TextRect, (200,200))

        self.TextRect = self.font.render("Instructions", True, white)
        self.screen.blit(self.TextRect, (200,300))

        self.TextRect = self.font.render("Quit Game", True, white)
        self.screen.blit(self.TextRect, (200,400))

        # Blit everything to the screen
        pygame.display.flip()

        for event in pygame.event.get():
            mouse = pygame.mouse.get_pos()
            click = pygame.mouse.get_pressed()

            #exits the game if user presses button to close window
            if event.type == pygame.QUIT: sys.exit()
            if event.type == pygame.MOUSEBUTTONDOWN:
                if 450 > mouse[0] > 200 and 240 > mouse[1] > 200:
                    print("Play Game! -", mouse)
                    GameScreen((300,300))
                if 450 > mouse[0] > 200 and 340 > mouse[1] > 300:
                    print("Instructions -", mouse)
                    root = tk.Tk()
                    photo = tk.PhotoImage(file= 'instructions.png')
                    root.title('Instructions')
                    label = tk.Label(root, image = photo)
                    label.pack()
                    root.mainloop()
                if 450 > mouse[0] > 200 and 440 > mouse[1] > 400:
                    print("Quit Game -", mouse)
                    sys.exit()

class GameScreen:
    def __init__(self, location):
        while True:
            white = (255, 255, 255)
            display_width = 1200
            display_height = 700
            self.screen = pygame.display.set_mode((display_width, display_height))
            pygame.display.set_caption('Israeli Poker')
            self.image = pygame.image.load('gameBackground.png')
            self.screen.blit(self.image, (0, 0))
            self.font = pygame.font.SysFont("Arial", 30)
            self.font.set_bold(True)
            self.TextRect = self.font.render("Back to Main Screen", True, white)
            self.screen.blit(self.TextRect, (10, 10))
            self.TextRect = self.font.render("Your hand", True, white)
            self.screen.blit(self.TextRect, (515, 565))
            self.TextRect = self.font.render("Their hand", True, white)
            self.screen.blit(self.TextRect, (510, 115))
            pygame.display.flip()

            for event in pygame.event.get():
                #exits the game if user presses button to close window
            mouse = pygame.mouse.get_pos()
                if event.type == pygame.QUIT: sys.exit()
                if event.type == pygame.MOUSEBUTTONDOWN:
                    print("Quit Game -", mouse)
                    if 300 > mouse[0] > 10 and 30 > mouse[1] > 10:
                        while 1:
                            Controller((300,300))

                imageList = ['2c', '2d', '2h', '2s', '3c', '3d', '3h', '3s', '4c', '4d', '4h', '4s', '5c', '5d', '5h', '5s', '6c', '6d', '6h', '6s', '7c', '7d', '7h', '7s', '8c', '8d', '8h', '8s', '9c', '9d', '9h', '9s', '10c', '10d', '10h', '10s', 'Jc', 'Jd', 'Jh', 'Js', 'Qc', 'Qd', 'Qh', 'Qs', 'Kc', 'Kd', 'Kh', 'Ks', 'Ac', 'Ad', 'Ah', 'As']
                newVar = random.choice(imageList) + '.png'
                newVar2 = random.choice(imageList) + '.png'
                newVar3 = random.choice(imageList) + '.png'
                newVar4 = random.choice(imageList) + '.png'
                newVar5 = random.choice(imageList) + '.png'
                newVar6 = random.choice(imageList) + '.png'
                newVar7 = random.choice(imageList) + '.png'
                newVar8 = random.choice(imageList) + '.png'
                newVar9 = random.choice(imageList) + '.png'
                newVar10 = random.choice(imageList) + '.png'

                self.image = pygame.image.load(newVar)
                self.image2 = pygame.image.load(newVar2)
                self.image3 = pygame.image.load(newVar3)
                self.image4 = pygame.image.load(newVar4)
                self.image5 = pygame.image.load(newVar5)
                self.image6 = pygame.image.load(newVar6)
                self.image7 = pygame.image.load(newVar7)
                self.image8 = pygame.image.load(newVar8)
                self.image9 = pygame.image.load(newVar9)
                self.image10 = pygame.image.load(newVar10)

                self.screen.blit(self.image, (350, 25))
                self.screen.blit(self.image2, (450, 25))
                self.screen.blit(self.image3, (550, 25))
                self.screen.blit(self.image4, (650, 25))
                self.screen.blit(self.image5, (750, 25))

                self.screen.blit(self.image6, (350, 600))
                self.screen.blit(self.image7, (450, 600))
                self.screen.blit(self.image8, (550, 600))
                self.screen.blit(self.image9, (650, 600))
                self.screen.blit(self.image10, (750, 600))

                pygame.display.flip()



def main():
    while 1:
        Controller((300,300))
main()
Thomas Hur
  • 11
  • 1
  • 1
    Read the SO help center, including https://stackoverflow.com/help/mcve – Terry Jan Reedy Dec 06 '17 at 23:23
  • It sounds like you want to implement different scenes (states). You could write two separate functions or classes with their own while and event loops, and the controller is responsible for switching the scenes (in another while loop). [Here's a good example](https://www.reddit.com/r/pygame/comments/3kghhj/simple_state_machine_example/) of a state machine which let's you switch between several scenes with a single while and event loop. – skrx Dec 06 '17 at 23:34
  • 1
    [Here's](https://stackoverflow.com/a/47460947/6220679) a minimal example with functions as scenes. – skrx Dec 06 '17 at 23:40
  • 1
    BTW, don't load your images from the hard disk in the while or event loop, because that's slow and unnecessary. Just load them in the global scope and reuse them in the functions/methods that need them. – skrx Dec 06 '17 at 23:51
  • 1
    @skrx your minimal example is nice – furas Dec 07 '17 at 00:05
  • @furas thanks. :) But I'd rather use classes. – skrx Dec 07 '17 at 00:06
  • @skrx yes, with classes it can be more usefull but this minimal version is so short and clean - it can be easier to understand for beginners so it is good example. – furas Dec 07 '17 at 00:14
  • Yes, that's why I wrote it. – skrx Dec 07 '17 at 00:15

0 Answers0