Im having trouble creating a zoom ability for my vtt - python-3.x

The main problem right now is that the player sprites disappear as soon as I zoom in or out the qrid scales correctly though. I had it working with the player sprites on screen then their movement stopped working. Got a little frustrated, then deleted the zoom altogether. Restarted, and now I'm here so I figured posting my first question because the internet didn't have the answers readily available.
import pygame as pg
class game():
def __init__(self):
self.WIDTH, self.HEIGHT = 1000, 700
self.win = pg.display.set_mode((self.WIDTH, self.HEIGHT), pg.RESIZABLE)
self.mapsurface = pg.surface.Surface((10000, 10000))
self.walls = pg.sprite.Group()
self.all_sprites = pg.sprite.Group()
self.player = Player("map assets/tokens/firegenasiwizard.png", self, 5, 5, self.mapsurface)
self.jplayer = Player("map assets/tokens/goliathbarbarian.png", self, 4, 4, self.mapsurface)
self.players = [self.player, self.jplayer]
def event_tracker(self):
global cellsize, spritesize
for event in pg.event.get():
if event.type is pg.QUIT:
pg.display.quit()
pg.quit()
sys.exit()
if event.type == pg.MOUSEBUTTONDOWN:
pos = pg.mouse.get_pos()
for play in self.players:
if play.rect.x <= pos[0] <= play.rect.x + spritesize and \
play.rect.y <= pos[1] <= play.rect.y + spritesize and play.selected is False:
play.selected = True
elif play.rect.x <= pos[0] <= play.rect.x + spritesize and \
play.rect.y <= pos[1] <= play.rect.y + spritesize and play.selected is True:
play.selected = False
if event.type == pg.KEYDOWN:
for play in self.players:
if event.key == pg.K_UP and play.selected is True:
play.move(dx=0, dy=-1)
if event.key == pg.K_DOWN and play.selected is True:
play.move(dx=0, dy=1)
if event.key == pg.K_LEFT and play.selected is True:
play.move(dx=-1, dy=0)
if event.key == pg.K_RIGHT and play.selected is True:
play.move(dx=1, dy=0)
if event.type == pg.MOUSEWHEEL:
if event.y > 0:
self.mapsurface = pg.transform.scale(self.mapsurface, (self.mapsurface.get_width() + 10,
self.mapsurface.get_height() + 10))
cellsize += 10
spritesize += 10
for play in self.players:
play.scale(10)
if event.y < 0:
self.mapsurface = pg.transform.scale(self.mapsurface, (self.mapsurface.get_width() - 10,
self.mapsurface.get_height() - 10))
cellsize -= 10
spritesize -= 10
for play in self.players:
play.scale(-10)
def draw_grid(self):
self.mapsurface.fill(white)
for x in range(0, self.mapsurface.get_width(), cellsize):
pg.draw.line(self.mapsurface, black, (x, 0), (x, 10000), 1)
for y in range(0, self.mapsurface.get_height(), cellsize):
pg.draw.line(self.mapsurface, black, (0, y), (10000, y), 1)
def draw_window(self):
self.draw_grid()
for play in self.players:
play.draw()
self.win.blit(self.mapsurface, (0, 0))
pg.display.update()

Related

Py-Game Connect4 - Minimax (Program Halting Recursively)

main.py
from Player import Player
import tkinter as tk
import pygame
import pygame_menu
import time
import colors
import Connect4 as cFour
import Minimax as mx
def text_format(option, textSize, textColor):
"""
Creates a text object to show in the main menu
"""
newFont = pygame.font.Font(pygame_menu.font.FONT_FRANCHISE, textSize)
newText = newFont.render(option, 0, textColor)
return newText
def load_screen():
"""
This initializes the window for pygame to use
"""
screen = pygame.display.set_mode((600, 600))
pygame.display.set_caption("Connect4")
return screen
def get_player_details(screen):
"""
Creates a tkinter object(button) that gets players names
"""
root = tk.Tk()
root.title("Player Names!")
tk.Label(root, text="Player One", fg="blue").grid(row=0)
tk.Label(root, text="Player Two", fg="red").grid(row=1)
p1 = tk.Entry(root, font=(None, 15))
p2 = tk.Entry(root, font=(None, 15))
p1.grid(row=0, column=1)
p2.grid(row=1, column=1)
tk.Button(root, text='Play!', command= lambda: play_game(p1.get(),p2.get(), root, screen)).grid(row=10, column=1, sticky=tk.W)
tk.mainloop()
def get_player_ai_details(screen):
"""
Creating the panel to allow the user to select a color and go against the AI
"""
options = ["Player 1", "Player 2"]
root = tk.Tk()
root.title("Player 1(Blue) or 2(Red)?")
colorChoice= tk.StringVar(root)
colorChoice.set(options[0])
tk.OptionMenu(root, colorChoice, *options).grid(row=3)
p1 = tk.Entry(root, font=(None, 15))
p1.grid(row=3, column=1)
tk.Button(root, text="Play Computer!", command=lambda: play_computer(colorChoice.get(), p1.get(), root, screen)).grid(row=10, column=1)
tk.mainloop()
def play_computer(colorChoice, playerName, root, screen):
"""
Connect4 play function (human v computer)
"""
root.destroy()
if colorChoice == "Player 1":
mx.Minimax(Player(playerName), Player("Ed"), screen).play_computer()
else:
mx.Minimax(Player("Ed"), Player(playerName), screen).play_computer()
def play_game(p1Name, p2Name, root, screen):
"""
Connect4 play function (human v human)
"""
root.destroy()
game = cFour.Connect4(Player(p1Name.strip()), Player(p2Name.strip()), screen).play()
if __name__ == "__main__":
pygame.init()
screen = load_screen()
features = [
("Player Vs Player", colors.yellow),
("Player Vs AI", colors.red),
("Quit", colors.gray)
]
iterator = 0
menu = True
while menu:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
#This if block makes it where the user doesnt have to click arrow key up/down if they have exhausted the possible options, it will loop you throughout options
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_DOWN:
iterator += 1
if iterator == len(features):
iterator = 0
if event.key == pygame.K_UP:
iterator -= 1
if iterator < 0:
iterator = len(features) - 1
if event.key == pygame.K_RETURN:
if selected == "Player Vs Player":
get_player_details(screen)
if selected == "Player Vs AI":
get_player_ai_details(screen)
if selected == "Quit":
pygame.quit()
quit()
selected = features[iterator][0]
screen.fill(colors.blue)
screen_rect = screen.get_rect()
for i in range(0, len(features)):
counter = -50 + (i * 90) # Equation that sets distance between each choice in main menu
if i == iterator:
text = text_format(features[i][0], 80, features[i][1])
else:
text = text_format(features[i][0], 80, colors.black)
player_rect = text.get_rect(center=screen_rect.center)
player_rect[1] = player_rect[1] + counter
screen.blit(text, player_rect)
pygame.display.update()
Connect4.py
import pygame
import colors
import tkinter as tk
import pygame_menu
# import pandas as pd
import random
class Connect4:
"""
Class used to represent connect4 game
"""
def __init__(self, player1, player2, screen):
# Use 1 version of the screen instead of trying to create a new one
self.screen = screen
# Circle Radius and Width
self.WIDTH = 0
self.CIRCLERADIUS = 25
# Game-Time Variables
self.player1 = player1
self.player2 = player2
self.moveNumber = 0
self.gameOver = False
self.COLUMNS = 7
self.ROWS = 6
self.EMPTY = 99
self.board = [[self.EMPTY for x in range(self.COLUMNS)] for y in range(self.ROWS)]
# The distance between where the window starts and the game board is placed
self.DISTANCE = 90
# Space between each circle
self.DISTANCEGAP = 70
# Setting rectangle default
self.LEFT = 50
self.TOP = 70
self.HEIGHT = 470
self.RECWIDTH = 500
#Creating new tkinterobject
self.root = tk.Tk()
self.scoreboard = {self.player1.name: 0, self.player2.name: 0, "ties": 0}
# Storing locations of available moves given a user clicks the window -- Tuple of locations
self.POSITIONS = [
(
self.DISTANCE + (self.DISTANCEGAP*column) - self.CIRCLERADIUS,
self.DISTANCE + (self.DISTANCEGAP*column) + self.CIRCLERADIUS
)
for column in range(0, self.COLUMNS)
]
def who_won(self, board, piece):
"""
Determines the state of the game and finds if there is a winner
"""
# Horizontal
for col in range(0, self.COLUMNS - 3):
for row in range(0, self.ROWS):
if board[row][col] == piece and board[row][col + 1] == piece and board[row][col + 2] == piece and board[row][col + 3] == piece:
return True
# Vertical
for col in range(0, self.COLUMNS):
for row in range(0, self.ROWS - 3):
if board[row][col] == piece and board[row + 1][col] == piece and board[row + 2][col] == piece and board[row + 3][col] == piece:
return True
# Up-Left/Down-Right
for col in range(3, self.COLUMNS):
for row in range(3, self.ROWS):
if board[row][col] == piece and board[row - 1][col - 1] == piece and board[row - 2][col - 2] == piece and board[row - 3][col - 3] == piece:
return True
# Up-Right/Down-Left
for col in range(0, self.COLUMNS - 3):
for row in range(3, self.ROWS):
if board[row][col] == piece and board[row - 1][col + 1] == piece and board[row - 2][col + 2] == piece and board[row - 3][col + 3] == piece:
return True
# A winning move is not found
return False
def is_legal_move(self, position, board):
"""
Validates if a move is available/legal
"""
if board[0][position] == self.EMPTY:
return True
return False
def display_board(self):
"""
Displaying the game board to the user
"""
# Function: rect(surface, color, rectangle object, optional width) -- First one forms the outline of the board
pygame.draw.rect(self.screen, colors.salmon, (self.LEFT, self.TOP, self.RECWIDTH, self.HEIGHT), 13)
# This forms inner-most rectangle that users play on
pygame.draw.rect(self.screen, colors.burlywood, (self.LEFT, self.TOP, self.RECWIDTH, self.HEIGHT))
for column in range(0, self.COLUMNS):
colEq = self.DISTANCE + (self.DISTANCEGAP * column)
for row in range(0, self.ROWS):
# 125 is used here to make a the board placed in the center of the board and helps finding a value for self.TOP easier
rowEq = 125 + (self.DISTANCEGAP * row)
if self.board[row][column] == self.EMPTY:
color = colors.white
elif self.board[row][column] == 0:
color = colors.realBlue
elif self.board[row][column] == 1:
color = colors.red
pygame.draw.circle(self.screen, color, (colEq, rowEq), self.CIRCLERADIUS, self.WIDTH)
pygame.display.flip()
def play(self):
"""
This is the game-loop
"""
while not self.gameOver:
self.display_board()
if self.moveNumber % 2 == 0:
userText, userRect = self.display_player_name(self.player1.name, colors.realBlue)
elif self.moveNumber % 2 == 1:
userText, userRect = self.display_player_name(self.player2.name, colors.red)
self.screen.blit(userText, userRect)
for event in pygame.event.get():
self.screen.fill(colors.aquamarine) # Set up background color
if event.type == pygame.QUIT:
self.gameOver = True
elif event.type == pygame.MOUSEBUTTONDOWN:
x, y = pygame.mouse.get_pos()
position = self.get_column_position(x)
if self.moveNumber % 2 == 0 and position != self.EMPTY:
if self.is_legal_move(position, self.board):
self.drop_piece_animation(position)
if self.who_won(self.board, 0):
self.gameOver = True
self.scoreboard[self.player1.name] = self.scoreboard.get(self.player1.name) + 1
userText, userRect = self.display_player_name(self.player1.name + " " + "Wins!!!", colors.dark_gray)
elif self.check_if_tie(self.board):
self.gameOver = True
self.scoreboard["ties"] = self.scoreboard.get("ties") + 1
userText, userRect = self.display_player_name("It is a TIE!!!", colors.dark_gray)
elif self.moveNumber % 2 == 1 and position != self.EMPTY:
if self.is_legal_move(position, self.board):
self.drop_piece_animation(position)
if self.who_won(self.board, 1):
self.gameOver = True
self.scoreboard[self.player2.name] = self.scoreboard.get(self.player2.name) + 1
userText, userRect = self.display_player_name(self.player2.name + " " + "Wins!!!", colors.dark_gray)
elif self.check_if_tie(self.board):
self.gameOver = True
self.scoreboard["ties"] = self.scoreboard.get("ties") + 1
userText, userRect = self.display_player_name("It is a TIE!!!", colors.dark_gray)
self.display_board()
self.screen.blit(userText, userRect)
pygame.display.flip()
self.display_scoreboard(False)
def display_scoreboard(self, isAi):
"""
This enables the tkinter object so I can display the user options after : Victory/Loss/Tie
"""
self.root.geometry('460x150+300+0')
self.reset()
self.root.title("Choices")
# This creates the feedback information screen that the user sees after a game
tk.Label(self.root, text="Close window to go to main menu", font=(None, 15, 'underline'), anchor='w', justify='left').grid(row=0, column=1, sticky="NSEW")
tk.Label(self.root, text=self.player1.name + ": " + str(self.scoreboard.get(self.player1.name)), font=(None, 15), anchor='w', justify='left').grid(row=1, column=1, sticky = "NSEW")
tk.Label(self.root, text=self.player2.name + ": " + str(self.scoreboard.get(self.player2.name)), font=(None, 15), anchor='w', justify='left').grid(row=2, column=1, sticky="NSEW")
tk.Label(self.root, text="Ties: " + str(self.scoreboard.get("ties")), font=(None, 15), anchor='w', justify='left').grid(row=3, column=1, sticky="NSEW")
# if isAi == True:
# # tk.Button(self.root, text='Rematch!', command=self.playAi, font=(None, 12), fg="blue").grid(row=4, column=1, sticky=tk.W)
# else:
tk.Button(self.root, text='Rematch!', command=self.play, font=(None, 12), fg="blue").grid(row=4, column=1, sticky=tk.W)
# tk.Button(self.root, text='Rematch with Swap!', command= lambda: self.swapPlayers(isAi), font=(None, 12), fg="red").grid(row=4, column=2, sticky=tk.W)
tk.Entry(self.root)
self.root.mainloop()
def check_if_tie(self, board):
"""
A possible game state : Checking for a tie
"""
totalPieces = 0
for col in range(0, self.COLUMNS):
for row in range(0, self.ROWS):
if board[row][col] == 0 or board[row][col] == 1:
totalPieces += 1
if totalPieces == 42:
return True
else:
return False
def display_player_name(self, name, color):
"""
A feature to help users know who's turn it is that gets displayed
"""
font = pygame.font.Font(pygame_menu.font.FONT_FRANCHISE, 60)
text = font.render(name, True, color)
textRect = text.get_rect()
textRect.center = (len(name) * 30, 20)
return text, textRect
def drop_piece_animation(self, position):
"""
Inserting a piece at a given position with the animation of a piece drop
"""
tmpRow = 5
while self.board[tmpRow][position] == 1 or self.board[tmpRow][position] == 0:
tmpRow -= 1
for i in range(0, tmpRow + 1):
self.board[i][position] = self.moveNumber % 2
self.display_board()
pygame.time.delay(200)
pygame.display.flip()
self.board[i][position] = self.EMPTY
self.board[tmpRow][position] = self.moveNumber % 2
self.moveNumber += 1
def get_column_position(self, position):
"""
Takes a X coordinate value dependent on a click and determines what column user clicked
"""
index = 0
for i in self.POSITIONS:
if position + self.CIRCLERADIUS/2 >= i[0] and position - self.CIRCLERADIUS/2 <= i[1]:
return index
index += 1
return self.EMPTY
def reset(self):
"""
Restoring the game in its original state
"""
self.moveNumber = 0
self.board = [[self.EMPTY for x in range(self.COLUMNS)] for y in range(self.ROWS)]
self.gameOver = False
def play_computer(self):
"""
This is the game-loop used for AI play
"""
# If/else block to distinguish the human/Ai because the ai cant mouse click events
if self.player1.name == "Ed": # Ed Watkins (Staten Island)
humanPlayer = 1
computerPlayer = 0
humanName = self.player2.name
computerName = self.player1.name
elif self.player2.name == "Ed":
humanPlayer = 0
computerPlayer = 1
humanName = self.player1.name
computerName = self.player2.name
while not self.gameOver:
self.display_board()
if self.moveNumber % 2 == 0:
userText, userRect = self.display_player_name(self.player1.name, colors.blue)
elif self.moveNumber % 2 == 1:
userText, userRect = self.display_player_name(self.player2.name, colors.red)
self.screen.blit(userText, userRect)
for event in pygame.event.get():
self.screen.fill(colors.aquamarine) # Set up background color
if event.type == pygame.QUIT:
self.gameOver = True
elif event.type == pygame.MOUSEBUTTONDOWN:
x, y = pygame.mouse.get_pos()
position = self.get_column_position(x)
if self.moveNumber % 2 == humanPlayer and position != self.EMPTY:
if self.is_legal_move(position, self.board):
self.drop_piece_animation(position)
if self.who_won(self.board, humanPlayer):
self.gameOver = True
self.scoreboard[humanName] = self.scoreboard.get(humanName) + 1
userText, userRect = self.display_player_name(humanName + " " + "Wins!!!", colors.dark_gray)
elif self.check_if_tie(self.board):
self.gameOver = True
self.scoreboard["ties"] = self.scoreboard.get("ties") + 1
userText, userRect = self.display_player_name("It is a TIE!!!", colors.dark_gray)
if self.moveNumber % 2 == computerPlayer and self.gameOver == False:
move = self.generate_move(self.board, 4, computerPlayer, humanPlayer, True, self.moveNumber)
self.drop_piece_animation(move)
if self.who_won(self.board, computerPlayer):
self.gameOver = True
self.scoreboard[computerName] = self.scoreboard.get(computerName) + 1
userText, userRect = self.display_player_name(computerName + " " + "Wins!!!", colors.dark_gray)
elif self.check_if_tie(self.board):
self.gameOver = True
self.scoreboard["ties"] = self.scoreboard.get("ties") + 1
userText, userRect = self.display_player_name("It is a TIE!!!", colors.dark_gray)
self.display_board()
self.screen.blit(userText, userRect)
pygame.display.flip()
Minimax.py
from Connect4 import Connect4
import random
from copy import copy, deepcopy
import pygame
class Minimax(Connect4):
def __init__(self, player1, player2, screen):
super().__init__(player1, player2, screen)
def is_game_over(self, board):
if self.who_won(board, 1) or self.who_won(board, 0):
return True
return False
def generate_move(self, board, depth, computerPlayer, humanPlayer, maximizingPlayer, moveNumber):
if depth == 0 or self.is_game_over(board) or self.check_if_tie(board):
if self.is_game_over(board):
if self.who_won(board, computerPlayer):
return 1000000
elif self.who_won(board, humanPlayer):
return -1000000
elif self.check_if_tie(board):
return 0
else:
return self.get_game_score(board, computerPlayer, humanPlayer)
if maximizingPlayer:
maxValue = -1000000
for move in range(0, self.COLUMNS):
tmpBoard = self.copyBoard(board)
if self.is_legal_move(move, tmpBoard):
self.drop_piece_computer(move, tmpBoard, moveNumber)
result = self.generate_move(tmpBoard, depth - 1, computerPlayer, humanPlayer, False, moveNumber + 1)
if result >= maxValue:
maxValue = result
bestMove = move
return bestMove
else:
minValue = 1000000
for move in range(0,self.COLUMNS):
tmpBoard = self.copyBoard(board)
if self.is_legal_move(move, tmpBoard):
self.drop_piece_computer(move, tmpBoard, moveNumber)
result = self.generate_move(tmpBoard, depth - 1, humanPlayer, humanPlayer, True, moveNumber + 1)
if result <= minValue:
minValue = result
thismove = move
return thismove
def copyBoard(self, board):
tmpList = [[self.EMPTY for x in range(self.COLUMNS)] for y in range(self.ROWS)]
for row in range(0, self.ROWS):
for col in range(0, self.COLUMNS):
tmpList[row][col] = board[row][col]
return tmpList
def drop_piece_computer(self, position, board, moveNumber):
"""
Inserting a piece at a given position with the animation of a piece drop
"""
tmpRow = 5
while board[tmpRow][position] == 1 or board[tmpRow][position] == 0:
tmpRow -= 1
board[tmpRow][position] = moveNumber % 2
# moveNumber += 1
def get_game_score(self, board, computerPlayer, humanPlayer):
totalScore = 0
totalScore += self.get_hori_score(board, computerPlayer, humanPlayer)
# totalScore += self.get_vert_score(board, computerPlayer, humanPlayer)
# totalScore += self.get_upright_score(board, computerPlayer, humanPlayer)
# totalScore += self.get_upleft_score(board, computerPlayer, humanPlayer)
return totalScore
def get_hori_score(self, board, computerPlayer, humanPlayer):
score = 0
# List to collect all the groupings of 4(Horizontally) out of the current game state
groupingFourList = []
for col in range(0, self.COLUMNS - 3):
for row in range(0, self.ROWS):
groupingFourList.append(board[row][col])
groupingFourList.append(board[row][col + 1])
groupingFourList.append(board[row][col + 2])
groupingFourList.append(board[row][col + 3])
computerPieces = self.count_player_pieces(groupingFourList, 1)
humanPieces = self.count_player_pieces(groupingFourList, 0)
emptyPieces = self.count_player_pieces(groupingFourList, self.EMPTY)
score += self.score_metric(computerPieces, humanPieces, emptyPieces)
groupingFourList = []
return score
def get_upright_score(self, board, computerPlayer, humanPlayer):
score = 0
# List to collect all the groupings of 4(Horizontally) out of the current game state
groupingFourList = []
for col in range(0, self.COLUMNS - 3):
for row in range(3, self.ROWS):
groupingFourList.append(board[row][col])
groupingFourList.append(board[row - 1][col + 1])
groupingFourList.append(board[row - 2][col + 2])
groupingFourList.append(board[row - 3][col + 3])
computerPieces = self.count_player_pieces(groupingFourList, 1)
humanPieces = self.count_player_pieces(groupingFourList, 0)
emptyPieces = self.count_player_pieces(groupingFourList, self.EMPTY)
score += self.score_metric(computerPieces, humanPieces, emptyPieces)
groupingFourList = []
return score
def get_upleft_score(self, board, computerPlayer, humanPlayer):
score = 0
# List to collect all the groupings of 4(Horizontally) out of the current game state
groupingFourList = []
for col in range(3, self.COLUMNS):
for row in range(3, self.ROWS):
groupingFourList.append(board[row][col])
groupingFourList.append(board[row - 1][col - 1])
groupingFourList.append(board[row - 2][col - 2])
groupingFourList.append(board[row - 3][col - 3])
computerPieces = self.count_player_pieces(groupingFourList, 1)
humanPieces = self.count_player_pieces(groupingFourList, humanPlayer)
emptyPieces = self.count_player_pieces(groupingFourList, self.EMPTY)
score += self.score_metric(computerPieces, humanPieces, emptyPieces)
groupingFourList = []
return score
def get_vert_score(self, board, computerPlayer, humanPlayer):
score = 0
# List to collect all the groupings of 4(Horizontally) out of the current game state
groupingFourList = []
for col in range(0, self.COLUMNS):
for row in range(0, self.ROWS -3):
groupingFourList.append(board[row][col])
groupingFourList.append(board[row + 1][col])
groupingFourList.append(board[row + 2][col])
groupingFourList.append(board[row + 3][col])
computerPieces = self.count_player_pieces(groupingFourList, computerPlayer)
humanPieces = self.count_player_pieces(groupingFourList, humanPlayer)
emptyPieces = self.count_player_pieces(groupingFourList, self.EMPTY)
score += self.score_metric(computerPieces, humanPieces, emptyPieces)
groupingFourList = []
return score
def count_player_pieces(self, groupingFourList, playerPiece):
totalPieces = 0
for piece in groupingFourList:
if piece == playerPiece:
totalPieces += 1
return totalPieces
def score_metric(self, computerPieces, humanPieces, emptyPieces):
score = 0
# Making bot prioritize playing defense than offense
# Thats why the score is lower when regarding the enemy: AI chooses highest scoring move
if (computerPieces == 4):
score += 100
elif (computerPieces == 3 and emptyPieces == 1):
score += 20
elif (computerPieces == 2 and emptyPieces == 2):
score += 10
if (humanPieces == 3 and emptyPieces == 1):
score -= 100
return score
colors.py
"""
Valid colors to use got it from this link : https://python-forum.io/Thread-PyGame-PyGame-Colors
"""
realBlue = (0,0,255)
white = (255,255,255)
green = (0,255,0)
black = (0,0,0)
orange = (255,100,10)
blue_green = (0,255,170)
marroon = (115,0,0)
lime = (180,255,100)
pink = (255,100,180)
purple = (240,0,255)
magenta = (255,0,230)
brown = (100,40,0)
forest_green = (0,50,0)
navy_blue = (0,0,100)
rust = (210,150,75)
dandilion_yellow = (255,200,0)
highlighter = (255,255,100)
sky_blue = (0,255,255)
light_gray = (200,200,200)
dark_gray = (50,50,50)
tan = (230,220,170)
coffee_brown = (200,190,140)
moon_glow = (235, 245, 255)
burlywood = (255, 211, 155)
salmon = (139, 76, 57)
aquamarine = (127, 255, 212)
#Colors used for menu
blue = (135, 206, 250)
yellow = (255, 255, 0)
red = (255,0,0)
gray = (128, 128, 128)
Player.py
class Player():
def __init__(self, name):
self.name = name
The solution is solved but stackoverflow wont allow me to remove the question. The reason I would like the question removed because the answer provided isnt the solution so it would just throw other people off.
Ok the solution is as follows:
In this section of code this is going through a portion of the search space and evaluating the game states based on the best moves for each player. Through this algorithm the AI knows the best moves for each player and can make a "good" move.
if maximizingPlayer:
maxValue = -1000000
for move in range(0, self.COLUMNS):
tmpBoard = self.copyBoard(board)
if self.is_legal_move(move, tmpBoard):
self.drop_piece_computer(move, tmpBoard, moveNumber)
result = self.generate_move(tmpBoard, depth - 1, computerPlayer, humanPlayer, False, moveNumber + 1)
if result >= maxValue:
maxValue = result
bestMove = move
return bestMove
else:
minValue = 1000000
for move in range(0,self.COLUMNS):
tmpBoard = self.copyBoard(board)
if self.is_legal_move(move, tmpBoard):
self.drop_piece_computer(move, tmpBoard, moveNumber)
result = self.generate_move(tmpBoard, depth - 1, humanPlayer, humanPlayer, True, moveNumber + 1)
if result <= minValue:
minValue = result
thismove = move
return thismove
However, if you look closely when I recursively call the function back to the AI in the !maximizing player function I have:
result = self.generate_move(tmpBoard, depth - 1, humanPlayer, humanPlayer, True, moveNumber + 1)
In words when a simulated human player was playing on a generic board to generate the ai move it was assuming that there were 2 human players, and thats why the halt would happen because you cannot have a game with 2 of the same player.
so changing:
result = self.generate_move(tmpBoard, depth - 1, humanPlayer, humanPlayer, True, moveNumber + 1)
to this:
result = self.generate_move(tmpBoard, depth - 1, computerPlayer, humanPlayer, True, moveNumber + 1)
Putting False instead of True in the if maximizingPlayer block and True instead of False in the else clause for the second to last argument to the recursive calls to generate_move seems to fix the game.
i.e. Change False on line 33 of Minimax.py to True and change the True on line 44 to False.

I made a game where blocks fall and you have to dodge it but can't get the hitboxes right

The hitbox seems to be on the left side of the cube, so it feels like the game is ending without me even hitting anything. I want to make it that the hitbox is exactly on the red rectangle, so that it's obvious that the black rectangle had hit you.
the up and down controls are just for future movement option if needed.
here's the code
import pygame as pg,pygame
import random
import sys
pygame.init()
WIDTH = 1000
HEIGHT = 800
# Positions, sizes
SPEED = 10
MIDDLE = [500, 400]
score = 0
player_size = 90
player_pos = [WIDTH/2, HEIGHT-2*player_size]
playerX_move = 0
playerY_move = 0
enemy_size = 50
enemy_pos = [random.randint(0,WIDTH - enemy_size), 30]
enemy_list = [enemy_pos]
# Colors
WHITE = [255, 255, 255]
GREY = [25, 25, 25]
BLUE = [65,105,225]
BLACK = [0,0,0]
GREEN = [65,255,105]
RED = [255,0,0]
# -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
screen = pygame.display.set_mode((WIDTH,HEIGHT))
game_over = False
clock = pygame.time.Clock()
def drop_enemies(enemy_list):
delay = random.random()
if len(enemy_list) < 10 and delay < 0.1:
x_pos = random.randint(0,WIDTH-enemy_size)
y_pos = 30
enemy_list.append([x_pos, y_pos])
def draw_enemies(enemy_list):
for enemy_pos in enemy_list:
pygame.draw.rect(screen,BLACK, (enemy_pos[0], enemy_pos[1], enemy_size, enemy_size))
def update_enemy_positions(enemy_list, score):
for idx, enemy_pos in enumerate(enemy_list):
if enemy_pos[1] >= 0 and enemy_pos[1] < HEIGHT:
enemy_pos[1] += SPEED
else:
enemy_list.pop(idx)
score += 1
return score
def collision_check(enemy_list, player_pos):
for enemy_pos in enemy_list:
if detect_collision(enemy_pos, player_pos):
return True
return False
def detect_collision(player_pos, enemy_pos):
p_x = player_pos[0]
p_y = player_pos[1]
e_x = enemy_pos[0]
e_y = enemy_pos[1]
if (e_x >= p_x and e_x < (p_x + player_size)) or (p_x >= e_x and p_x < (e_x+enemy_size)):
if (e_y >= p_y and e_y < (p_y + player_size)) or (p_y >= e_y and p_y < (e_y+enemy_size)):
return True
return False
while not game_over:
player_pos[0] += playerX_move
player_pos[1] += playerY_move
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX_move = -5
if event.key == pygame.K_RIGHT:
playerX_move = 5
# if event.key == pygame.K_UP:
# playerY_move = -5
# if event.key == pygame.K_DOWN:
# playerY_move = 5
if event.key == pygame.K_a:
playerX_move = -15
if event.key == pygame.K_d:
playerX_move = 15
# if event.key == pygame.K_w:
# playerY_move = -15
# if event.key == pygame.K_s:
# playerY_move = 15
if event.key == pg.K_SPACE:
print("SPACE")
if event.type == pygame.KEYUP:
if event.key == pygame.K_a or event.key == pygame.K_d or event.key == pygame.K_s or event.key == pygame.K_w:
playerX_move = 0
playerY_move = 0
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT or event.key == pygame.K_DOWN or event.key == pygame.K_UP:
playerX_move = 0
playerY_move = 0
screen.fill(BLACK)
drop_enemies(enemy_list)
score = update_enemy_positions(enemy_list,score)
if collision_check(enemy_list, player_pos):
game_over = True
draw_enemies(enemy_list)
pygame.draw.rect(screen, RED, (player_pos[0], player_pos[1], 90, 90))
clock.tick(30)
pygame.display.update()
I would recommend using a list of pygame.Rects instead of a List with the positions of your enemies. This would also allow you to use pygame.Rect.colliderect() instead of your detect_collision function.
I've also linked the Pygame Docs.

make sprite crouch in current position python

Hi i'm making a fighting game to get some practice with pygame but I have run into a problem with crouching/ducking. When I press the down button it goes back to its original location then ducks. If you need more info to help i will provide.
import pygame
import random
display_height = 600
display_width = 1000
dis_screen = pygame.display.set_mode((display_width, display_height))
FPS = 30
clock = pygame.time.Clock()
img = pygame.image.load('foo.png')
crouchimg = pygame.image.load('crouchimg.png')
# Simple player object
class Player(object):
def __init__(self, x, y, image):
self.x = x
self.y = y
self.image = image
# Method to draw object
def draw(self):
dis_screen.blit(self.image, (self.x, self.y))
# Method to move object
def move(self, speedx, speedy):
self.x += speedx
self.y += speedy
class MainRun(object):
def __init__(self, displayw, displayh):
self.dw = displayw
self.dh = displayh
self.Main()
def Main(self):
# Put all variables up here
stopped = False
x_move = 0
y_move = 0
p1_y_loc = 200
p1_x_loc = 200
x = pygame.Rect().x
greg = Player(p1_x_loc, p1_y_loc, img)
# Main Loop
while not stopped:
print(x)
dis_screen.fill((255, 255, 255)) # Tuple for filling display... Current is white
# Event Tasking
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
y_move = 0
x_move = 5
elif event.key == pygame.K_LEFT:
y_move = 0
x_move = -5
elif event.key == pygame.K_UP:
y_move = -5
x_move = 0
elif event.key == pygame.K_DOWN:
p1_y_loc = 300
p1_x_loc = 0
greg = Player(p1_x_loc, p1_y_loc, crouchimg)
if event.type == pygame.KEYUP:
if event.key == pygame.K_DOWN:
p1_y_loc = 200
greg = Player(p1_x_loc, p1_y_loc, img)
if event.key == pygame.K_UP:
y_move = 0
if event.key == pygame.K_RIGHT or event.key == pygame.K_LEFT:
x_move = 0
greg.move(x_move, y_move)
greg.draw()
pygame.display.update()
clock.tick(FPS)
pygame.quit()
quit()
run = MainRun(display_width, display_height)
run.Main()
That's because when you're pressing the down key you're creating a new player at the x-position p1_x_loc (which you've set to 0) and y-position p1_y_loc (which you've set to 300). So when the player crouches it also moves it to (0, 300) no matter the position the player currently is.
A way to solve this would be to just change the player's image, and not create a completely new player. You can do it this way, player.image = crouchimg instead of greg = Player(p1_x_loc, p1_y_loc, crouchimg). And when the player gets up you just change the image again: player.image = img
And if you have to change the players' y-position when crouching you could do it the same way: player.y = 200 and player.y = 300.

TypeError, having issues defining certain things

Ok So, I'm making a simple game in python and pygame, where you're a spaceship and have to dodge meteors. But after adding in the meteor and writing the code to get it to spawn randomly and move down the screen. I had issues using the code tags as well, I'd do Ctrl + K and post my code inside the space provided and it gave me the code error. Sorry about that, here's my links (not the best way to do it I know, I couldn't get the code tags to work.)
My Error:
Traceback (most recent call last):
File "C:\Users\fredd_000\Desktop\Pygame_Script.py", line 127, in <module>
game_loop()
File "C:\Users\fredd_000\Desktop\Pygame_Script.py", line 98, in game_loop
things(thing_startx, thing_starty, thing_width, thing_height, black)
File "C:\Users\fredd_000\Desktop\Pygame_Script.py", line 28, in things
pygame.draw.circle(gameDisplay, color,[thingx, thingy, thingw, thingh])
TypeError: function takes at least 4 arguments (3 given)
My code where the error occurs:
def things(thingx, thingy, thingw, thingh, color):
pygame.draw.circle(gameDisplay, color,[thingx, thingy, thingw, thingh])
Full code:
import pygame
import time
import random
pygame.init()
display_width = 1200
display_height = 800
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('A Game By Freddie')
black = (0,0,0)
white = (255,255,255)
blue = (25,0,255)
red = (255,0,0)
car_width = 195
car_height = 1
clock = pygame.time.Clock()
gameExit = False
shipImg = pygame.image.load('ship.png')
def things(thingx, thingy, thingw, thingh, color):
pygame.draw.circle(gameDisplay, color,[thingx, thingy, thingw, thingh])
def ship(x,y):
gameDisplay.blit(shipImg, (x,y))
def text_objects(text, font):
textSurface = font.render(text, True, red)
return textSurface, textSurface.get_rect()
def message_display(text):
largeText = pygame.font.Font('freesansbold.ttf',120)
TextSurf, TextRect = text_objects(text, largeText)
TextRect.center = ((display_width/2),(display_height/2))
gameDisplay.blit(TextSurf, TextRect)
pygame.display.update()
time.sleep(2)
game_loop()
def crash():
message_display('You Died')
def game_loop():
x = (display_width * 0.375)
y = (display_height * 0.65)
x_change = 0
y_change = 0
thing_startx = random.randrange(0, display_width)
thing_starty = -600
thing_speed = 7
thing_width = 100
thing_height = 100
gameExit = False
while not gameExit:
gameDisplay.fill(blue)
shipImg = pygame.image.load('ship.png')
ship(x,y)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -5
elif event.key == pygame.K_RIGHT:
x_change = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
x += x_change
things(thing_startx, thing_starty, thing_width, thing_height, black)
thing_starty += thing_speed
if x > display_width - car_width or x < 0:
crash()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
y_change = -5
elif event.key == pygame.K_DOWN:
y_change = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
y_change = 0
y += y_change
if y > display_height - car_height or y < 0:
crash()
pygame.display.update()
clock.tick(60)
game_loop()
pygame.quit()
quit()
The problem with your code is that your using pygame.draw.circle() wrong. The syntax for using the function is: pygame.draw.circle(screen, color, (x, y), diameter). As you can probably tell, the way you did it was pygame.draw.circle(screen, color, [x, y, w, h]). This is incorrect, a circle does not have a width and height. Change your code to: pygame.draw.circle(gameDisplay, color, (thingx, thingy), thingw). thingw will be the diameter of your circle.
You should also change your function paramters to def things(thingx, thingy, thingw, color).
If your still new to Pygame or confused on how to draw shapes to the screen, i highly suggest reading the Pygame documentation on drawing, as Idos linked to in the comments.

Why does the display of text decrease my pygame FPS?

I'm trying to write this simple game that replicates a few aspects of Asteroids and have had some problems with the FPS. I have not finished the game yet, but was concerned about the FPS drop as I have a lot more text that I wish to display, but am unable to display for fear the game will suffer so much frame drop. I have only noticed this frame drop with text being displayed on screen. My code can be found below.
import os
import pickle
import pygame
from pygame.locals import *
import random
import sys
import time
pygame.init()
pygame.display.init()
common_drops = ['Janus', 'Peace of Mind', 'Second Chance']
rare_drops = ['Invincibility', 'Shield', 'Bonus Credits', 'Weapon']
ultra_rare_drops = []
janus_count = 0
peace_of_mind_count = 0
bonus_lives_count = 0 #Second Chance
invincibility_count = 0
shield_count = 0
weapon_count = 0
credit_count = 30
high_score = 0
def save():
loot_out = open('./nec_files/user_data/player.pickle', 'wb')
pickle.dump(player_loot_data, loot_out)
loot_out.close()
if os.path.isfile('./nec_files/user_data/player.pickle') == True:
loot_in = open('./nec_files/user_data/player.pickle', 'rb')
loot_dict = pickle.load(loot_in)
player_loot_data = {
'janus_count' : loot_dict['janus_count'],
'peace_of_mind_count' : loot_dict['peace_of_mind_count'],
'bonus_lives_count' : loot_dict['bonus_lives_count'], #Second Chance
'invincibility_count' : loot_dict['invincibility_count'],
'shield_count' : loot_dict['shield_count'],
'weapon_count' : loot_dict['weapon_count'],
'credit_count' : loot_dict['credit_count'],
'high_score' : loot_dict['high_score']
}
loot_in.close()
save()
else:
player_loot_data = {
'janus_count' : janus_count,
'peace_of_mind_count' : peace_of_mind_count,
'bonus_lives_count' : bonus_lives_count, #Second Chance
'invincibility_count' : invincibility_count,
'shield_count' : shield_count,
'weapon_count' : weapon_count,
'credit_count' : credit_count,
'high_score' : high_score
}
save()
display_width = 1280
display_height = 720
black = (0,0,0)
white = (255,255,255)
blue = (0, 102, 204)
bright_blue = (102, 178, 255)
red = (204, 0, 0)
bright_red = (255, 51, 51)
yellow = (204, 204, 0)
bright_yellow = (255, 255, 102)
gray = (169, 169, 169)
game_title = 'Asteroids: Reimagined'
paused = False
alpha = True
gameDisplay = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption(game_title)
print(pygame.display.get_driver())
clock = pygame.time.Clock()
playerImg = pygame.image.load('./nec_files/graphics/player_image.png')
asteroidImg = pygame.image.load('./nec_files/graphics/asteroid_image.png')
current_drops = []
def open_common_drop():
global current_drops
current_drops.clear()
if player_loot_data['credit_count'] >= 5:
player_loot_data['credit_count'] -= 5
for x in range(3):
rand_num = random.randint(0, 50)
if rand_num < 49:
drops = random.choice(common_drops)
elif rand_num >= 49:
drops = random.choice(rare_drops)
if drops == 'Janus':
player_loot_data['janus_count'] += 1
elif drops == 'Peace of Mind':
player_loot_data['peace_of_mind_count'] += 1
elif drops == 'Second Chance':
player_loot_data['bonus_lives_count'] += 1
elif drops == 'Bonus Credits':
bonus_credits = random.randint(1, 50)
player_loot_data['credit_count'] += bonus_credits
elif drops == 'Invincibility':
player_loot_data['invincibility_count'] += 1
elif drops == 'Shield':
player_loot_data['shield_count'] += 1
elif drops == 'Weapon':
player_loot_data['weapon_count'] += 1
current_drops.append(drops)
save()
def open_rare_drop():
global current_drops
current_drops.clear()
if player_loot_data['credit_count'] >= 10:
player_loot_data['credit_count'] -= 10
for x in range(3):
rand_num = random.randint(0, 50)
if rand_num < 36:
drops = random.choice(common_drops)
elif rand_num >= 36:
drops = random.choice(rare_drops)
if drops == 'Janus':
player_loot_data['janus_count'] += 1
elif drops == 'Peace of Mind':
player_loot_data['peace_of_mind_count'] += 1
elif drops == 'Second Chance':
player_loot_data['bonus_lives_count'] += 1
elif drops == 'Bonus Credits':
bonus_credits = random.randint(1, 50)
player_loot_data['credit_count'] += bonus_credits
elif drops == 'Invincibility':
player_loot_data['invincibility_count'] += 1
elif drops == 'Shield':
player_loot_data['shield_count'] += 1
elif drops == 'Weapon':
player_loot_data['weapon_count'] += 1
current_drops.append(drops)
save()
def player(player_x, player_y):
gameDisplay.blit(playerImg, (player_x, player_y))
def asteroid(thingx, thingy):
gameDisplay.blit(asteroidImg, (thingx, thingy))
def game_display_text(display_msg, display_x, display_y, text_size):
font = pygame.font.SysFont(None, text_size)
text = font.render(str(display_msg), True, black)
gameDisplay.blit(text, (display_x, display_y))
def title(msg):
largeText = pygame.font.SysFont(None, 75)
TextSurf, TextRect = text_objects(msg, largeText)
TextRect.center = ((display_width / 2), (display_height * 0.10))
gameDisplay.blit(TextSurf, TextRect)
def button(x, y, w, h, ic, ac, action = None):
global paused
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x + w > mouse[0] > x and y + h > mouse[1] > y:
pygame.draw.rect(gameDisplay, ac, (x, y, w, h))
if click[0] == 1 and action == Game:
Game()
if click[0] == 1 and action == quitgame:
sys.exit()
if click[0] == 1 and action == None:
paused = False
if click[0] == 1 and action == StartScreen:
save()
StartScreen()
if click[0] == 1 and action == LootScreen:
LootScreen()
if click[0] == 1 and action == open_common_drop:
open_common_drop()
if click[0] == 1 and action == open_rare_drop:
open_rare_drop()
else:
pygame.draw.rect(gameDisplay, ic, (x, y, w, h))
def things(thingx, thingy, thingw, thingh, color):
pygame.draw.rect(gameDisplay, color, [thingx, thingy, thingw, thingh])
def things2(thingx, thingy, thingw, thingh, color):
pygame.draw.rect(gameDisplay, color, [thingx, thingy, thingw, thingh])
def text_box(box_x, box_y, box_w, box_h, color):
pygame.draw.rect(gameDisplay, color, [box_x, box_y, box_w, box_h])
def text_objects(text, font):
textSurface = font.render(text, True, black)
return textSurface, textSurface.get_rect()
def message_display(text):
largeText = pygame.font.Font('freesansbold.ttf', 50)
TextSurf, TextRect = text_objects(text, largeText)
TextRect.center = ((display_width/2),(display_height/2))
gameDisplay.blit(TextSurf, TextRect)
pygame.display.update()
time.sleep(2)
def reset():
message_display('Out of Bounds: Player Location Reset')
def quitgame():
pygame.quit()
sys.exit()
def StartScreen():
intro = True
settings_x = 1230
settings_y = 670
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
save()
pygame.quit()
sys.exit()
gameDisplay.fill(gray)
title(game_title)
button(420, 250, 125, 50, blue, bright_blue, Game)
button(720, 250, 125, 50, red, bright_red, quitgame)
button(570, 250, 125, 50, yellow, bright_yellow, LootScreen)
game_display_text('Start', 450, 260, 40)
game_display_text('Quit', 750, 260, 40)
game_display_text('Loot', 600, 260, 40)
game_display_text('Licensed by: #1024MBStudio', 925, 690, 35)
pygame.display.update()
clock.tick(30)
def LootScreen():
global current_drops
loot = True
while loot:
for event in pygame.event.get():
if event.type == pygame.QUIT:
save()
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_t:
open_common_drop()
elif event.key == pygame.K_y:
open_rare_drop()
if event.key == pygame.K_ESCAPE:
StartScreen()
gameDisplay.fill(gray)
title('Loot Chests!')
button(400, 150, 260, 50, blue, bright_blue, None)
button(695, 150, 260, 50, red, bright_red, None)
button(display_width * 0.42, display_height / 1.15, 255, 50, red, bright_red, StartScreen)
game_display_text('Open Common Chest (T)', 407, 165, 30)
game_display_text('Open Rare Chest (Y)', 725, 165, 30)
game_display_text('You Got: %s' % current_drops, 50, display_height / 2, 35)
game_display_text('Credits: %.2f' % player_loot_data['credit_count'], 15, 15, 35)
game_display_text('Main Menu', display_width * 0.47, display_height / 1.13, 35)
game_display_text('Janus\': %.2f' % player_loot_data['janus_count'] , 1025, 500, 35)
game_display_text('Peace of Minds: %.2f' % player_loot_data['peace_of_mind_count'], 1025, 535, 35)
pygame.display.update()
clock.tick(30)
def PauseScreen():
global paused
paused = True
pausebox_x = 0
pausebox_y = 625
pausebox_width = display_width
pausebox_height = display_height - 625
while paused:
for event in pygame.event.get():
if event.type == pygame.QUIT:
save()
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
paused = False
gameDisplay.fill(gray)
title('Paused')
button(560, 130, 173, 50, blue, bright_blue, None)
button(560, 205, 173, 50, red, bright_red, StartScreen)
game_display_text('Resume', 590, 140, 40)
game_display_text('Quit', 615, 218, 40)
text_box(pausebox_x, pausebox_y, pausebox_width, pausebox_height, blue)
game_display_text('Janus\': %s' % player_loot_data['janus_count'] , 5, 630, 35)
game_display_text('Peace of Minds: %s' % player_loot_data['peace_of_mind_count'], 5, 665, 35)
game_display_text('Bonus Lives: %s' % player_loot_data['bonus_lives_count'], 250, 630, 35)
pygame.display.update()
clock.tick(30)
def DeadScreen():
current_score = 0
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
save()
pygame.quit()
sys.exit()
gameDisplay.fill(gray)
title('You Died')
game_display_text('You earned %s' % credit_gain + ' credits that game!', display_width * 0.33, display_height * 0.40, 40)
button(520, 120, 250, 55, blue, bright_blue, Game)
button(520, 190, 250, 55, red, bright_red, StartScreen)
game_display_text('Play Again?', 560, 132, 40)
game_display_text('Main Menu', 569, 205, 40)
pygame.display.update()
clock.tick(30)
def Game():
global death_counter, attempt_counter, credit_gain
player_x = (display_width * 0.5)
player_y = (display_height * 0.5)
player_speed = 5.5
playerHeight = 50
x_change = 0
y_change = 0
enemyWidth = 165
thing_startx = 1500
thing2_startx = 1500
thing_speed = -6
thing2_speed = -5.5
thing_starty = random.randrange(75, display_height - enemyWidth)
thing2_starty = random.randrange(75, display_height - enemyWidth)
dead = False
janus = False
peace_of_mind = False
invincibility = False
full_screen = False
earnable_credits = 0.125
current_score = 0
credit_gain = 0
current_lives = 0
RESETEVENT = pygame.USEREVENT + 1
DISABLEJANUS = pygame.USEREVENT + 5
textbox_x = 0
textbox_y = 0
textbox_width = 1280
textbox_height = 60
while not dead:
if pygame.display.get_active() == True:
for event in pygame.event.get():
pygame.time.set_timer(RESETEVENT, 275)
if peace_of_mind == True:
thing_original_speed = thing_speed
thing2_original_speed = thing2_speed
if event.type == RESETEVENT:
current_score += 1
pygame.time.set_timer(RESETEVENT, 275)
if event.type == DISABLEJANUS:
janus = False
if event.type == pygame.QUIT:
save()
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if janus == True:
if event.key == pygame.K_LEFT or event.key == pygame.K_a:
x_change = -player_speed
elif event.key == pygame.K_RIGHT or event.key == pygame.K_d:
x_change = player_speed
if event.key == pygame.K_UP or event.key == pygame.K_w:
y_change = -player_speed
elif event.key == pygame.K_DOWN or event.key == pygame.K_s:
y_change = player_speed
if event.key == pygame.K_u and player_loot_data['janus_count'] > 0 and janus == False:
pygame.time.set_timer(DISABLEJANUS, 15000)
player_loot_data['janus_count'] -= 1
janus = True
elif event.key == pygame.K_i and player_loot_data['peace_of_mind_count'] > 0 and peace_of_mind == False:
player_loot_data['peace_of_mind_count'] -= 1
peace_of_mind = True
elif event.key == pygame.K_o and player_loot_data['bonus_lives_count'] > 0:
player_loot_data['bonus_lives_count'] -= 1
current_lives += 1
if event.key == pygame.K_ESCAPE:
PauseScreen()
elif event.key == pygame.K_F4:
sys.exit()
elif event.key == pygame.K_F11:
if full_screen == False:
pygame.display.set_mode((display_width, display_height), pygame.FULLSCREEN)
full_screen = True
PauseScreen()
elif full_screen == True:
pygame.display.set_mode((display_width, display_height))
PauseScreen()
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT or event.key == pygame.K_a or event.key == pygame.K_d:
x_change = 0
elif event.key == pygame.K_UP or event.key == pygame.K_DOWN or event.key == pygame.K_w or event.key == pygame.K_s:
y_change = 0
elif event.key == pygame.K_SPACE:
y_change = 0
if thing_startx < 0 - enemyWidth:
thing_startx = 1500
thing_starty = random.randrange(75, display_height - enemyWidth)
thing_speed += -0.05
player_loot_data['credit_count'] += earnable_credits
credit_gain += earnable_credits
if thing2_startx < 0 - enemyWidth:
thing2_startx = 1500
thing2_starty = random.randrange(75, display_height - enemyWidth)
thing2_speed += -0.1
player_loot_data['credit_count'] += earnable_credits
credit_gain += earnable_credits
player_x += x_change
player_y += y_change
thing_startx += thing_speed
thing2_startx += thing2_speed
if player_loot_data['high_score'] < current_score:
player_loot_data['high_score'] = current_score
if player_y > display_height:
player_y = textbox_height
if player_y < 10:
player_y = display_height - playerHeight
if player_x < 0 - playerHeight:
player_x = (display_width * 0.5)
if player_x > display_width:
player_x = (display_width * 0.5)
if player_y < thing_starty + enemyWidth and player_y + playerHeight > thing_starty:
if player_x > thing_startx and player_x < thing_startx + enemyWidth or player_x + playerHeight > thing_startx and player_x + playerHeight < thing_startx + enemyWidth:
if current_lives > 0:
current_lives -= 1
player_x = (display_width * 0.5)
player_y = (display_height * 0.5)
thing_startx = 1500
thing2_startx = 1500
else:
dead = True
if player_y < thing2_starty + enemyWidth and player_y + playerHeight > thing2_starty:
if player_x > thing2_startx and player_x < thing2_startx + enemyWidth or player_x + playerHeight > thing2_startx and player_x + playerHeight < thing2_startx + enemyWidth:
if current_lives > 0:
current_lives -= 1
player_x = (display_width * 0.5)
player_y = (display_height * 0.5)
thing_startx = 1500
thing2_startx = 1500
else:
dead = True
else:
crossover = 'null'
gameDisplay.fill(gray)
player(player_x, player_y)
asteroid(thing_startx, thing_starty)
asteroid(thing2_startx, thing2_starty)
text_box(textbox_x, textbox_y, textbox_width, textbox_height, blue)
game_display_text('High Score: %s' % player_loot_data['high_score'], 5, 5, 30)
game_display_text('Current Score: %s' % current_score, 5, 35, 30)
game_display_text('Current Chances: %s' % current_lives, 200, 5, 30)
if janus == True:
game_display_text('Janus Enabled', 850, 5, 30)
if peace_of_mind == True:
game_display_text('Peace of Mind Enabled', 850, 35, 30)
if invincibility == True:
game_display_text('Invincibility Enabled', 950, 5, 30)
if alpha == True:
game_display_text('FPS: %s' % clock.get_fps(), 5, 635, 30)
pygame.display.update()
clock.tick()
else:
PauseScreen()
DeadScreen()
if __name__ == '__main__':
StartScreen()
sys.exit()
You're doing two things wrong with your text rendering.
The first (and probably major) one is that you load the font again and again every time you want to display some text:
def game_display_text(display_msg, display_x, display_y, text_size):
font = pygame.font.SysFont(None, text_size)
...
You should create the font object once, so you don't load it every time from disk.
The second issue is that the rendering of the text to a Surface is a rather expensive operation:
def game_display_text(display_msg, display_x, display_y, text_size):
...
text = font.render(str(display_msg), True, black)
...
A better method is to cache the already created surfaces, and reuse them.
A very simple cache could look like this:
text_font = pygame.font.SysFont("whatever", 14)
cache={}
def get_msg(msg):
if not msg in cache:
cache[msg] = text_font.render(msg, 1 , text_color)
return cache[msg]
and then you would use the get_msg method to create your text surfaces. Or use something like e.g. the lru_cache decorator.

Resources