我是pygame的新手,但对编程却没有那么多,按下按钮后,我很难随时出现弹丸。如果我按住该按钮,它将起作用,但否则无法显示。我确实知道它会将其添加到硬币列表中,因为如果使用打印功能,我可以看到硬币列表,但绘制时看不到物体。我不知道我在做什么错。
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()
答案 0 :(得分:1)
只是弄清楚了。这是新代码。感谢所有停下来尝试提供帮助的人。
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()