Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
rect object not loading
#1
Nothing is loading, and all I get is a blank surface. Please help.
#touhou rectangle
import os
import pygame
pygame.init()


win = pygame.display.set_mode((1000, 600))
pygame.display.set_caption("pesto anchovy")
class player() :
    x = 10
    y = 10
    height = 60
    width = 20
    currentlevel = 1
    projectiles = []
    hitbox = [y-5, y+5, x-5, x+5]
    hp = 30
    align = 'good'

    def draw(self, win):
        keys = pygame.key.get_pressed()

        if keys [pygame.K_SPACE] and self.projectiles == [] :
            self.projectiles.append(projectile(self.x-10, self.y, -2, 0, 20, (239, 229, 93)))
            self.projectiles.append(projectile(self.x, self.y, -2, 0, 20, (239, 229, 93)))
            self.projectiles.append(projectile(self.x+10, self.y, -2, 0, 20, (239, 229, 93)))
            self.projectiles.append(projectile(self.x+20, self.y, -2, 0, 20, (239, 229, 93)))
            self.projectiles.append(projectile(self.x+30, self.y, -2, 0, 20, (239, 229, 93)))
#projectiles
        if keys [pygame.K_UP] :
            self.y -= 10
        if keys [pygame.K_DOWN] :
            self.y += 10
        if keys [pygame.K_RIGHT] :
            self.x += 10
        if keys [pygame.K_LEFT] :
            self.x -= 10

        pygame.draw.rect(win, (210, 0, 63), (10, 940, self.hp*10, 20))
        pygame.draw.rect(win, (69, 218, 24), (self.x, self.y, self.width, self.height))
#movement

class projectile():
    def __init__(self, x, y, damage, xmove, ymove, color):
        self.x = x
        self.y = y
        self.damage = damage
        self.xmove = xmove
        self.ymove = ymove
        self.color = color
    def checkcollision(self, owner, collider) :
        if collider.hitbox[0] > self.y-5 and collider.hitbox[1] < self.y + 5 and collider.hitbox[2] < self.x-5 and collider.hitbox[3] < self.x+5 and owner.align != collider.align :
            collider.hp -= damage
            owner.projectiles.pop(index(self))
        if self.x > 1000 or self.x < 0 or self.y > 600 or self.y < 0 :
            owner.projectiles.pop(index(self))
    def draw(self, win) :
        self.x += self.xmove
        self.y += self.ymove
        pygame.draw.circle(win, self.color, (self.x, self.y), 5)

class bluecircle():
    actproj = False
    def __init__(self, going) :
        if going == 'r':
            self.goingright = True
            self.goingleft = False
        else:
            self.goingright = False
            self.goingleft = True
    projectiles = []
    actcount = 1
    health = 15
    y = 10
    align = 'bad'
    def __init__(self, x):
        self.x = x
    #hitbox format: up, down, left, right
    def draw(self, win) :
        if not self.actproj:
            self.actproj = True
            self.projectiles.append(projectile(self.x, self.y+10, 0, 10, 1, (218, 69, 143)))
            if self.goingright:
                self.projectiles.append(projectile(self.x, self.y+10, 10, 10, 1, (218, 69, 143)))
                self.projectiles.append(projectile(self.x, self.y+10, 10, 0, 1, (218, 69, 143)))
                self.projectiles.append(projectile(self.x, self.y+10, 5, 10, 1, (218, 69, 143)))
                self.projectiles.append(projectile(self.x, self.y+10, 10, 5, 1, (218, 69, 143)))
            else:
                self.projectiles.append(projectile(self.x, self.y+10, -10, 10, 1, (218, 69, 143)))
                self.projectiles.append(projectile(self.x, self.y+10, -10, 0, 1, (218, 69, 143)))
                self.projectiles.append(projectile(self.x, self.y+10, -5, 10, 1, (218, 69, 143)))
                self.projectiles.append(projectile(self.x, self.y+10, -10, 5, 1, (218, 69, 143)))
        elif self.goingright :
            self.x += 10
            if x >= 980 :
                goingright = False
                goingleft = True
        else:
            self.x -= 10
            if self. x <= 20:
                goingleft = False
                goingright = True
        pygame.draw.circle(win, (69, 188, 218), (self.x, self.y), 10)
#behaviors
def save (player) :
    if os.path.exists('save.txt') :
        os.remove('save.txt')
    o = open('save.txt', 'w+')
    o.write(player.currentlevel)

def load (player) :
    if os.path.exists('save.txt') :
        e = open('save.txt', 'r')
        if e.mode == 'r' :
            level = e.read()
            player.currentlevel = int(level)
    else :
        player.currentlevel = 1

def level(level, player):
    for waves in level:
        intro = 50
        while intro > 0 :
            win.fill((0, 0, 0))
            font = pygame.font.SysFont("timesnewroman", 50, False)
            text = font.render("Level {}: Wave {}".format(player.currentlevel, level.index(waves)+1), 1, (0, 0, 0))
            win.blit(text, (500, 300))
            player.draw(win)
            for projectiles in player.projectiles :
                projectiles.draw(win)
                projectiles.checkcollision(player, player)
        currentenemyhealth = 1
        check = 1
        while currentenemyhealth > 0:
            currentenemyhealth = 0
            win.fill((0, 0, 0))
            player.draw(win)
            for enemies in waves:
                currentenemyhealth += enemies.health
                enemies.draw(win)
                if check == 1:
                    check -= 1
                    currentenemyhealth -= 1
                for bullets in enemies.projectiles :
                    bullets.draw(win)
                    bullets.checkcollision(enemies, player)
            for projectiles in player.projectiles :
                projectiles.draw(win)
                for enemies in waves:
                    projectiles.checkcollision(player, enemies)
    player.currentlevel += 1
    save(player)
load(player)

clock = pygame.time.Clock()
p = player()
run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
    clock.tick(27)
    p.draw(win)
    for bullet in p.projectiles:
        bullet.draw(win)
    pygame.display.update

pygame.quit()
Sorry for the poor commenting.
Reply
#2
line 166 change from
Quote: pygame.display.update
to this
    pygame.display.update()
Recommended Tutorials:
Reply
#3
OH, I'm an idiot, sorry!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGame] Rect object penetrates wall on one side Jan_97 4 2,463 Dec-30-2021, 11:08 PM
Last Post: Jan_97
  [PyGame] printing integers on pygame.draw.rect Shyckh 1 2,475 Aug-22-2020, 11:44 AM
Last Post: metulburr
  [PyGame] pygame.draw.rect function stretches across the screen instead of moving BubblesTheGiraffe 2 3,577 Jun-11-2019, 08:32 PM
Last Post: metulburr
  [PyGame] assigning rect to sprite pfaber11 1 2,147 May-18-2019, 05:39 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

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