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)
#4
I have this game. It works fine with logging in and the confirm screen. The problem is in the main loop. The player is no where to be seen. Moving the player does nothing. The screen is just plain yellow. 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 windowRedraw(win, color):
        if color != None:
            win.fill(color)
        pygame.display.update()

class Buttons():
    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)
Main.py
import pygame
import random
import time
import threading
from pygame.locals import *
from DestinationFunc import Screen_Display as SD
from DestinationFunc import Buttons as BT

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]

class GameData():
    def __init__(self):
        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'}
        self.signInList = signInList
        self.UserArmor = UserArmor
        self.UserWeap = UserWeap
        self.UserLvl = UserLvl

    def getListings(self):
        return self.signInList, self.UserArmor, self.UserWeap, self.UserLvl

        

#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):
        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, x, y, width, height, color, weapType):
        p = Player(x, y, width, height, color, weapType)
        plrPos = p.getPos()
        plrX = plrPos[0]
        plrY = plrPos[1]

        if self.x != None and self.y != None:

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

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

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

def Confirm_QUIT():
    global Confirm
    Confirm = False

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)
        BT.Button(gD, round(width * .3), round(height * .7), round(width * .4), round(height * .2), CustomColor1, CustomColor2, 'Confirm!', round(width/20), blue, 'arial', Confirm_QUIT)
        BT.Button(gD, round(width * .75), round(height * .8), round(width * .2), round(height * .1), red, darker_red, "No!", round(width/50), blue, 'arial', SignIn)

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

def SignIn():
    global username
    GD = GameData()
    listings = GD.getListings()
    signInList = listings[0]
    UserArmor = listings[1]
    UserWeap = listings[2]
    UserLvl = listings[3]
    
    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

    else:
        if username in signInList:
            password = input('Please type your password: ')
            if password == signInList[username]:
                pass
            else:
                print('Invalid Password')
                time.sleep(2)
                SignIn()
        else:
            print('Invalid Username')
            time.sleep(2)
            SignIn()

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

        
        t1 = threading.Thread(target=p.draw)
        t2 = threading.Thread(target=p.move)
        t3 = threading.Thread(target=e.draw)
        t4 = threading.Thread(target=e.move(width/2, height/2, 50, 50, armor, weap))
        t1.start()
        t2.start()
        t3.start()
        t4.start()
        t1.join()
        t2.join()
        t3.join()
        t4.join()
        SD.windowRedraw(gD, pygame.Color('yellow'))
    End_Game()

def End_Game():
    listings = GameData.getListings()
    print(listings[0])
    print('')
    print(listings[1])
    print('')
    print(listings[2])
    print('')
    print(listings[3])

SD.message_display(gD, "Fill in info on console", round(int(width/20)), Unique_Color, width/2, height/2)
SD.windowRedraw(gD, None)
Main()
pygame.quit()
Reply


Messages In This Thread
RE: Drawing player and enemy (pygame) - by Windspar - Apr-14-2019, 10:25 AM
Drawing enemy but not player (pygame) - by SheeppOSU - Apr-14-2019, 03:44 PM
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 720 Dec-08-2023, 03:29 PM
Last Post: deanhystad
  pygame installation problems Gheryk 5 8,694 Nov-29-2023, 08:49 PM
Last Post: E_Mohamed
  can't get collision detection to work in platform game chairmanme0wme0w 10 3,889 Aug-19-2022, 03:51 PM
Last Post: deanhystad
  [PyGame] Problems with jump code in pygame Joningstone 4 5,423 Aug-23-2021, 08:23 PM
Last Post: deanhystad
  [PyGame] drawing images onto pygame window djwilson0495 1 3,524 Feb-22-2021, 05:39 PM
Last Post: nilamo
  [PyGame] Collision in not happening onizuka 3 3,450 Sep-07-2020, 11:30 AM
Last Post: metulburr
  [PyGame] No collision detection onizuka 6 3,703 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,385 Mar-07-2020, 12:32 PM
Last Post: Kris1996
  Problem with collision detection... michael1789 4 3,316 Nov-12-2019, 07:49 PM
Last Post: michael1789
  Pygame sprite not moving michael1789 1 2,871 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