Python Forum
drawing, moving, and collision problems (pygame)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
drawing, moving, and collision problems (pygame)
#1
I'm making this game and do far it's been good but now I have a problem. Tje problem is my character does not appear on the screen. It did what it was supposed to before I added the enemy. Can someone tell me what I'm doing wrong. The error I'm getting is about "gD.fill(white)", on line 67 in Main.py and appeared after I pressed quit on pygame. Thx in advance.

DestinationFunc.py
import pygame

class Screen_Display():
    def text_objects(text, font, color):
        textSurface = font.render(text, True, color)
        return textSurface, textSurface.get_rect()

    def message_display(gD, text, size, color, centerX, centerY):
        font = pygame.font.SysFont('arial', size)
        textSurf, TextRect = Screen_Display.text_objects(text, font, color)
        TextRect.center = ((centerX),(centerY))
        gD.blit(textSurf, TextRect)

    def Button(gD, Butx, Buty, Butx2, Buty2, Butcolor, ShadowColor, text, textsize, textcolor, textFont, command=None, command2=None):
        mouse = pygame.mouse.get_pos()
        click = pygame.mouse.get_pressed()
        smallText = pygame.font.SysFont((textFont), textsize)
        textSurf, textRect = Screen_Display.text_objects(text, smallText, textcolor)
        textRect.center = ((Butx + (Butx2/2)), Buty + (Buty2/2))
        pygame.draw.rect(gD, Butcolor, (Butx, Buty, Butx2, Buty2))
        gD.blit(textSurf, textRect)
        if Butx + Butx2 > mouse[0] > Butx and Buty + Buty2 > mouse[1] > Buty: 
            pygame.draw.rect(gD, ShadowColor, (Butx, Buty, Butx2, Buty2))
            gD.blit(textSurf, textRect)
            if click[0] == 1:
                if command != None:
                    command()
                if command2 != None:
                    command2()
        else:
            pygame.draw.rect(gD, Butcolor, (Butx, Buty, Butx2, Buty2))
            gD.blit(textSurf, textRect)

class Updates():
    def windowRedraw(win, color):
        if color != None:
            win.fill(color)
        pygame.display.update()
Main.py
import pygame
import random
import time
from pygame.locals import *
from DestinationFunc import Screen_Display as SD
from DestinationFunc import Updates as ud

width = 1000
height = 800
pygame.init()
gD = pygame.display.set_mode((width, height))

#Files
WoodBat = pygame.image.load("C:/Users/Chadd/Desktop/Ayden Stuff/WoodenBat.png").convert()
SpikeBat = pygame.image.load("C:/Users/Chadd/Desktop/Ayden Stuff/Spiked_Bat.png").convert()
IronBat = pygame.image.load("C:/Users/Chadd/Desktop/Ayden Stuff/MetalBat.png").convert()
IronSword = pygame.image.load("C:/Users/Chadd/Desktop/Ayden Stuff/IronSword.png").convert()
DiamondSword = pygame.image.load("C:/Users/Chadd/Desktop/Ayden Stuff/DiamondSword.png").convert()

#Colors
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
darker_red = (200,0,0)
green = (0,255,0)
lightest_blue = (0,0,255)
lighter_blue = (0,0,200)
light_blue = (0,0,160)
blue = (0,0,120)
brown = (165,42,42)
light_brown = (139,69,19)
CustomColor1 = (48,150,140)
CustomColor2 = (36,112.5,105)
Unique_Color = (190,140,210)
ColorList = [black, red, green, blue, white, brown, lightest_blue, lighter_blue, light_blue]

#GameData
signInList = {'Sheepposu' : 'rachl032078', 'gg' : 'rachl1979', 'Tallish Walk' : '092404Aw'}
UserArmor = {'Sheepposu' : light_blue, 'gg' : brown, 'Tallish Walk' : brown}
UserWeap = {'Sheepposu' : DiamondSword, 'gg' : WoodBat, 'Tallish Walk' : WoodBat}
UserLvl = {'Sheepposu' : '1', 'gg' : '1', 'Tallish Walk' : '1'}

#Config
fps = 60
pygame.display.set_caption('Destination')
clock = pygame.time.Clock()
gD.fill(blue)
clock.tick(15)

#Variables
Confirm = True
username = None

class Player():
    def __init__(self, x, y, width, height, color, weapType):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.rect = (x, y, width, height)
        self.color = color
        self.direct = None
        self.weapon = None
        self.weapType = weapType
        
    def draw(self):
        gD.fill(white)
        pygame.draw.rect(gD, self.color, (self.rect))
        #self.drawWeap()
        
        

    def drawWeap(self):
        if self.direct == 'Left':
            self.weapon = gD.blit(self.weapType, (self.x + 5, self.y + 25))
        if self.direct == 'Right':
            self.weapon = gD.blit(self.weapType, (self.x + 45, self.y + 25))
        if self.direct == 'Up':
            self.weapon = gD.blit(self.weapType, (self.x + 25, self.y + 5))
        if self.direct == 'Down':
            self.weapon = gD.blit(self.weapType, (self.x + 25, self.y + 45))

        self.update()
            
    def move(self):
        keyed = pygame.key.get_pressed()

        if keyed[pygame.K_LEFT] or keyed[pygame.K_a]:
            self.x -= 1
            self.direct = 'Left'
        if keyed[pygame.K_RIGHT] or keyed[pygame.K_d]:
            self.x += 1
            self.direct = 'Right'
        if keyed[pygame.K_UP] or keyed[pygame.K_w]:
            self.y -= 1
            self.direct = 'Up'
        if keyed[pygame.K_DOWN] or keyed[pygame.K_s]:
            self.y += 1
            self.direct = 'Down'

        self.update()

    def update(self):
        self.rect = (self.x, self.y, self.width, self.height)

    def getPos(self):
        return self.x, self.y

class Enemy():
    def __init__(self, x, y, width, height, color, weapType, plrX, plrY):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.rect = (x, y, width, height)
        self.color = color
        self.direct = None
        self.weapon = None
        self.weapType = weapType
        self.plrX = plrX
        self.plrY = plrY

    def draw(self):
        gD.fill(white)
        pygame.draw.rect(gD, self.color, (self.rect))
        #self.drawWeap()

    def drawWeap(self):
        if self.direct == 'Left':
            self.weapon = gD.blit(self.weapType, (self.x + 5, self.y + 25))
        if self.direct == 'Right':
            self.weapon = gD.blit(self.weapType, (self.x + 45, self.y + 25))
        if self.direct == 'Up':
            self.weapon = gD.blit(self.weapType, (self.x + 25, self.y + 5))
        if self.direct == 'Down':
            self.weapon = gD.blit(self.weapType, (self.x + 25, self.y + 45))

        self.update()

    def move(self):
        if self.x != None and self.y != None:

            if self.plrX > self.x:
                self.x += 1
            elif self.plrX < self.x:
                self.x -= 1

            if self.plrY > self.y:
                self.y += 1
            elif self.plrY < self.y:
                self.y -= 1
                
            self.update()

    def update(self):
        self.rect = (self.x, self.y, self.width, self.height)


def Confirm_Screen(username):
    global Confirm
    clock.tick(fps)
    while Confirm:
        
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                End_Game()
        gD.fill(blue)
        SD.message_display(gD, 'You\'re %s, correct?' %username, round(width/17), Unique_Color, round(width/2), height * .25)
        SD.Button(gD, round(width * .3), round(height * .7), round(width * .4), round(height * .2), CustomColor1, CustomColor2, 'Confirm!', round(width/20), blue, 'arial', Main)
        SD.Button(gD, round(width * .75), round(height * .8), round(width * .2), round(height * .1), red, darker_red, "No!", round(width/50), blue, 'arial', SignIn)

        ud.windowRedraw(gD, None)
        clock.tick(fps)

def SignIn():
    global signInList
    global username
    username = input('(Type "new" for new account) Type in username: ')

    if username == 'new':
        newUsername = input('Type your Username: ')
        newPassword = input('Type your Password: ')
        signInList.update({newUsername : newPassword})
        UserArmor.update({newUsername : brown})
        UserWeap.update({newUsername : light_brown})
        UserLvl.update({newUsername : '1'})
        username = newUsername
        Confirm_Screen(username)

    else:
        if username in signInList:
            password = input('Please type your password: ')
            if password == signInList[username]:
               Confirm_Screen(username)
            else:
                print('Invalid Password')
                SignIn()
        else:
            print('Invalid Username')
            SignIn()

def Main():
    global username
    global UserArmor
    global UserWeap
    armor = UserArmor[username]
    weap = UserWeap[username]
    p = Player(width/2, height/2, 50, 50, armor, weap)
    e = Enemy(0 ,0, 50, 50, brown, WoodBat, None, None)
    play = True
    clock.tick(fps)
    while play:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                End_Game()

        p.draw()
        p.move()
        plrPos = p.getPos()
        e = Enemy(0 ,0, 50, 50, brown, WoodBat, plrPos[0], plrPos[1])
        e.draw()
        e.move()
        ud.windowRedraw(gD, None)
    End_Game()

def End_Game():
    print(signInList)
    print(UserArmor)
    print(UserWeap)
    print(UserLvl)
    pygame.quit()

SD.message_display(gD, "Fill in info on console", round(int(width/20)), Unique_Color, width/2, height/2)
ud.windowRedraw(gD, None)
SignIn()
Here's the error -
Error:
Traceback (most recent call last): File "C:\Users\Chadd\Desktop\PyGame\Main.py", line 235, in <module> SignIn() File "C:\Users\Chadd\Desktop\PyGame\Main.py", line 194, in SignIn Confirm_Screen(username) File "C:\Users\Chadd\Desktop\PyGame\Main.py", line 169, in Confirm_Screen SD.Button(gD, round(width * .3), round(height * .7), round(width * .4), round(height * .2), CustomColor1, CustomColor2, 'Confirm!', round(width/20), blue, 'arial', Main) File "C:\Users\Chadd\Desktop\PyGame\DestinationFunc.py", line 27, in Button command() File "C:\Users\Chadd\Desktop\PyGame\Main.py", line 217, in Main p.draw() File "C:\Users\Chadd\Desktop\PyGame\Main.py", line 67, in draw gD.fill(white) pygame.error: display Surface quit
Reply


Messages In This Thread
drawing, moving, and collision problems (pygame) - by SheeppOSU - Apr-14-2019, 07:10 AM
RE: Drawing player and enemy (pygame) - by Windspar - Apr-14-2019, 10:25 AM
pygame problems - by SheeppOSU - Apr-18-2019, 11:27 PM
pygame problems - by SheeppOSU - Apr-19-2019, 11:59 PM
changing position errors - by SheeppOSU - Apr-22-2019, 03:09 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question [PyGame] Problem with collision of player and enemy Nekotrooper 1 665 Dec-08-2023, 03:29 PM
Last Post: deanhystad
  pygame installation problems Gheryk 5 8,571 Nov-29-2023, 08:49 PM
Last Post: E_Mohamed
  can't get collision detection to work in platform game chairmanme0wme0w 10 3,756 Aug-19-2022, 03:51 PM
Last Post: deanhystad
  [PyGame] Problems with jump code in pygame Joningstone 4 5,346 Aug-23-2021, 08:23 PM
Last Post: deanhystad
  [PyGame] drawing images onto pygame window djwilson0495 1 3,462 Feb-22-2021, 05:39 PM
Last Post: nilamo
  [PyGame] Collision in not happening onizuka 3 3,389 Sep-07-2020, 11:30 AM
Last Post: metulburr
  [PyGame] No collision detection onizuka 6 3,647 Aug-18-2020, 01:29 PM
Last Post: onizuka
  [PyGame] pygame, help with making a function to detect collision between player and enemy. Kris1996 3 3,313 Mar-07-2020, 12:32 PM
Last Post: Kris1996
  Problem with collision detection... michael1789 4 3,255 Nov-12-2019, 07:49 PM
Last Post: michael1789
  Pygame sprite not moving michael1789 1 2,824 Nov-10-2019, 03:54 AM
Last Post: michael1789

Forum Jump:

User Panel Messages

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