Reputation: 31
I am new to pygame but not so much to programming, I am having difficulty to have a projectile appear anytime after I press the button. If I hold the button it will work but otherwise I can't get it to display. I do know it is adding it to the coins list because if I put use the print function I can see the coins list but I can't see the objects when drawn otherwise. I cant figure out what I am doing wrong.
import pygame
pygame.init()
WIDTH = 800
HEIGHT = 600
window = pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption("Why U NO WORK?")
class throw_coin(object):
def __init__(self, color, x, y, radius):
self.color = color
self.x = x
self.y = y
self.radius = radius
self.hitbox = (self.x-15, self.y-15, self.radius*2, self.radius*2)
self.vel = 8
def draw(self, win):
self.x += self.vel
pygame.draw.circle(win, self.color, (self.x, self.y), self.radius)
#update hitbox if coin moves
self.hitbox = (self.x-15, self.y-15, self.radius*2, self.radius*2)
# draw the hitbox remove later after debug
#pygame.draw.rect(win, (white), self.hitbox, 2)
class player(object):
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.vel = 10
self.hitbox = (self.x, self.y, 64, 64)
self.coins = []
def draw(self, win):
pygame.draw.rect(window, (255,0,0), [self.x, self.y, self.width, self.height])
# update hitbox if player moves
self.hitbox = (self.x, self.y, 64, 64)
# draw the hitbox remove later after debug
#pygame.draw.rect(win, (white), self.hitbox, 2)
def move(self):
# create a variable that is assigned to any keypress
keys = pygame.key.get_pressed()
if keys[pygame.K_a] and self.x > self.vel:
self.x -= self.vel
if keys[pygame.K_d] and self.x < WIDTH - self.width - self.vel:
self.x += self.vel
if keys[pygame.K_w] and self.y > self.vel:
self.y -= self.vel
if keys[pygame.K_s] and self.y < HEIGHT - self.height - self.vel:
self.y += self.vel
if keys[pygame.K_g]:
if len(self.coins) < 100:
self.coins.append(throw_coin((0,255,0), 500, 300, 15))
for gold in self.coins:
gold.draw(window)
user = player(30, 300-32, 64,64)
run = True
while run:
pygame.time.delay(100)
# exit the program
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
window.fill((0,0,0))
user.draw(window)
user.move()
pygame.display.update()
pygame.quit()
Upvotes: 0
Views: 42
Reputation: 31
Just figuered it out. Here is the new code. Thanks for everyone who stopped by and tried to help.
import pygame
pygame.init()
WIDTH = 800
HEIGHT = 600
window = pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption("Why U NO WORK?")
def redraw_game():
window.fill((0,0,0))
for gold in user.coins:
gold.draw(window)
user.draw(window)
user.move()
pygame.display.update()
class throw_coin(object):
def __init__(self, color, x, y, radius):
self.color = color
self.x = x
self.y = y
self.radius = radius
self.hitbox = (self.x-15, self.y-15, self.radius*2, self.radius*2)
self.vel = 8
def draw(self, win):
self.x += self.vel
pygame.draw.circle(win, self.color, (self.x, self.y), self.radius)
#update hitbox if coin moves
self.hitbox = (self.x-15, self.y-15, self.radius*2, self.radius*2)
# draw the hitbox remove later after debug
#pygame.draw.rect(win, (white), self.hitbox, 2)
class player(object):
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.vel = 10
self.hitbox = (self.x, self.y, 64, 64)
self.coins = []
def draw(self, win):
pygame.draw.rect(window, (255,0,0), [self.x, self.y, self.width, self.height])
# update hitbox if player moves
self.hitbox = (self.x, self.y, 64, 64)
# draw the hitbox remove later after debug
#pygame.draw.rect(win, (white), self.hitbox, 2)
def move(self):
# create a variable that is assigned to any keypress
keys = pygame.key.get_pressed()
if keys[pygame.K_a] and self.x > self.vel:
self.x -= self.vel
if keys[pygame.K_d] and self.x < WIDTH - self.width - self.vel:
self.x += self.vel
if keys[pygame.K_w] and self.y > self.vel:
self.y -= self.vel
if keys[pygame.K_s] and self.y < HEIGHT - self.height - self.vel:
self.y += self.vel
if keys[pygame.K_g]:
if len(self.coins) < 100:
self.coins.append(throw_coin((0,255,0), 500, 300, 15))
user = player(30, 300-32, 64,64)
run = True
while run:
pygame.time.delay(100)
# exit the program
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
redraw_game()
pygame.quit()
Upvotes: 2