【发布时间】:2020-09-14 16:22:38
【问题描述】:
我正在尝试移动我的角色。我就像在教程中一样写了它(https://www.youtube.com/watch?v=FfWpgLFMI7w)。它不起作用。我的角色就是动弹不得。我也没有看到任何错误!我是否忘记在我的代码中添加一些内容 - 在 KEYDOWN 部分?所以整个代码都在这里,但问题可能出在KEYDOWN 部分或玩家坐标中。代码如下:
import pygame
from pygame.locals import *
pygame.init()
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 600
SURFACE = pygame.HWSURFACE|pygame.DOUBLEBUF|pygame.RESIZABLE
win = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT), SURFACE)
win.fill((0, 180, 210))
pygame.display.set_caption("Baloon War!")
icon = pygame.image.load("Baloon war icon.png")
pygame.display.set_icon(icon)
centre_point = (WINDOW_WIDTH//2, WINDOW_HEIGHT//2)
playerImg = pygame.image.load("RobiS.png")
playerX = 370
playerY = 500
playerX_change = 0
def player(x,y):
win.blit(playerImg, (x, y))
class button():
def __init__(self, color, x, y, width, height, text=''):
self.color = color
self.x = x
self.y = y
self.width = width
self.height = height
self.text = text
def draw(self, win, outline=None):
# Call this method to draw the button on the screen
if outline:
pygame.draw.rect(win, outline, (self.x - 2, self.y - 2, self.width + 4, self.height + 4), 0)
pygame.draw.rect(win, self.color, (self.x, self.y, self.width, self.height), 0)
if self.text != '':
font = pygame.font.SysFont('comicsans', 60)
text = font.render(self.text, 1, (0, 0, 0))
win.blit(text, (self.x + (self.width / 2 - text.get_width() / 2), self.y + (self.height / 2 - text.get_height() / 2)))
def isOver(self, pos):
# Pos is the mouse position or a tuple of (x,y) coordinates
if pos[0] > self.x and pos[0] < self.x + self.width:
if pos[1] > self.y and pos[1] < self.y + self.height:
return True
return False
def rescale(self):
new_size = int(WINDOW_WIDTH * self.scale_factor)
x, y = self.rect.center
self.image = pygame.transform.smoothscale(self.original, (new_size, new_size))
self.rect = self.image.get_rect()
self.rect.center = (x, y)
def resize_button(self, WINDOW_WIDTH, WINDOW_HEIGHT, x, y):
self.width = WINDOW_WIDTH
self.height = WINDOW_HEIGHT
self.x = x
self.y = y
def redrawMenuWindow():
win.fill((0, 255, 110))
greenButton.draw(win, (0, 0, 0))
redButton.draw(win, (0, 0, 0))
cyanButton.draw(win, (0, 0, 0))
def redrawGameWindow():
win.fill((0, 150, 210))
pygame.draw.rect(win, (0, 250, 110), (0, 450, 800, 250))
win.blit(playerImg, (370, 400))
def redrawShopWindow():
win.fill((200, 100, 30))
greenButton = button((0, 255, 0), 275, 285, 250, 80, "Start")
redButton = button((255, 0, 0), 275, 475, 250, 80, "Quit")
cyanButton = button((20, 210, 180), 275, 380, 250, 80, "Shop")
game_state = "menu"
run = True
while run:
if game_state == "menu":
redrawMenuWindow()
elif game_state == "game":
redrawGameWindow()
elif game_state == "shop":
redrawShopWindow()
pygame.display.update()
for event in pygame.event.get():
pos = pygame.mouse.get_pos()
if event.type == pygame.QUIT:
run = False
pygame.quit()
quit()
elif event.type == pygame.VIDEORESIZE:
WINDOW_WIDTH = event.w
WINDOW_HEIGHT = event.h
win = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT), SURFACE)
greenButton.resize_button(event.w*0.3, event.h*0.3, event.w/2, event.h/2)
if event.type == pygame.MOUSEBUTTONDOWN:
if greenButton.isOver(pos):
print("clicked the button")
game_state = "game"
if redButton.isOver(pos):
print("clicked the 2button")
run = False
pygame.quit()
quit()
if cyanButton.isOver(pos):
print("clicked the 3button")
game_state = "shop"
if event.type == pygame.MOUSEMOTION:
if greenButton.isOver(pos):
greenButton.color = (105, 105, 105)
else:
greenButton.color = (0, 255, 0)
if redButton.isOver(pos):
redButton.color = (105, 105, 105)
else:
redButton.color = (255, 0, 0)
if cyanButton.isOver(pos):
cyanButton.color = (105, 105, 105)
else:
cyanButton.color = (20, 210, 180)
if event.type == pygame.KEYDOWN:
playerX_change = -0.3
print("Left arrow is pressed")
if event.key == pygame.K_RIGHT:
playerX_change = 0.3
print("Right arrow is pressed")
if event.type == pygame.KEYUP:
if event.key == pygame.K_a or event.key == pygame.K_d:
playerX_change = 0.1
print("Keystroke has been released")
pygame.display.update()
playerX += playerX_change
player(playerX, playerY)
pygame.display.update()
请告诉我代码。如果你只是告诉我问题是什么,我可能不知道如何解决它。最好能告诉我哪里出了问题,然后编写正确的代码(不一定是整个代码,只是我需要的部分)。如果真正的问题是我因为我忘记了什么或打错了,那么我很抱歉问这个问题。
【问题讨论】:
-
玩家只能向右移动吗?看起来您缺少
playerX_change = -0.3上方的 if 语句。像if event.key == pygame.K_LEFT:这样的东西你有可能同时左右移动角色(-0.3 + 0.3 == 0.0) -
您能否解释一下您遇到了哪些错误,以及您想用这段代码实现什么?
-
你有错误的缩进并且你在
if event.type == pygame.MOUSEMOTION:里面有if event.type == pygame.KEYDOWN:。所以它仅在您移动鼠标时检查key,但此时event.type不能是KEYDOWN,因为它是MOUSEMOTION。KEYUP也有同样的问题。您必须使用较小的缩进将KEYDOWN/KEYUP移出if event.type == pygame.MOUSEMOTION: -
我现在编辑了它。也许它更好地解释,请回答,不要评论。如果你回答它,请重写整个代码。
-
别指望我们会为你编写所有代码。
标签: python pygame resize window character