Python/Pygame:不确定在哪里添加新的代码行
我刚刚得到了我的最新代码,但现在我需要添加播放器点击框/矩形,我以前在输入新代码时把它搞砸了,所以我不知道在哪里插入它 这是我的代码和需要输入的内容: 这是我的代码:Python/Pygame:不确定在哪里添加新的代码行,python,pygame,Python,Pygame,我刚刚得到了我的最新代码,但现在我需要添加播放器点击框/矩形,我以前在输入新代码时把它搞砸了,所以我不知道在哪里插入它 这是我的代码和需要输入的内容: 这是我的代码: # This just imports all the Pygame modules import pygame import time pygame.init() screen = pygame.display.set_mode((640, 480)) pygame.display.set_caption('St.P
# This just imports all the Pygame modules
import pygame
import time
pygame.init()
screen = pygame.display.set_mode((640, 480))
pygame.display.set_caption('St.Patrick game')
clock = pygame.time.Clock()
pygame.mixer.init(44100, -16,2,2048)
pygame.mixer.music.load('Jake Kaufman - Shovel Knight - Specter of Torment OST - 02 From the Shadows (Plains of Passage).mp3')
pygame.mixer.music.play(-1, 0.0)
image = pygame.image.load('Sprite-01.png')
# initialize variables
image_x = 0
image_y = 0
while 1:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
pygame.mixer.music.stop(282)
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
pygame.quit()
pygame.mixer.music.stop(282)
image_x += 0
key = pygame.key.get_pressed()
if key[pygame.K_LEFT]:
image_x -= 10
if key[pygame.K_RIGHT]:
image_x += 10
if key[pygame.K_UP]:
image_y -= 10
if key[pygame.K_DOWN]:
image_y += 10
screen.fill((200, 200, 200))
screen.blit(image, (image_x, image_y))
pygame.display.flip()
对您的问题的快速而直接的回答是,在使用类之前,可以在代码中的任何位置添加类(通常在开始时不嵌套在任何其他项下),然后您可以稍后在代码中(但在游戏循环之外)通过调用
player=player()
创建类
看起来您可能是从编程开始,第一次使用OO。至于更长的答案,我不能告诉你“新代码去哪里”的一般公式,但从你问题的性质来看,我或多或少同意你可能应该阅读一些关于类和面向对象编程的现有评论。当涉及到编写代码时,每种情况都是细致入微和不同的。面向对象编程(类)的主题有很多深度,但要有效地使用类,了解所有内容并不是必需的。如果没有关于类、实例和OO如何工作的基本知识,您将很容易犯很多错误或模仿约定,而不是可靠地设计或复制新代码
当你刚开始的时候,参考文档可能会让你有点不知所措。我建议你只需在谷歌上搜索“Python OO速成课程”之类的东西,就可以让你的脚湿透。你会发现更高层次的东西,比如这样:在看了一些代码并阅读了上面提到的一些东西之后,我得出了以下答案:
# This just imports all the Pygame modules
import pygame
import time
class Player(pygame.sprite.Sprite):
def __init__(self, *groups):
super(Player, self).__init__(*groups)
self.image = pygame.image.load('Sprite-01.png')
self.rect = pygame.rect.Rect((320, 240), self.image.get_size())
def update(self):
key = pygame.key.get_pressed()
if key[pygame.K_LEFT]:
self.rect.x -= 10
if key[pygame.K_RIGHT]:
self.rect.x += 10
if key[pygame.K_UP]:
self.rect.y -= 10
if key[pygame.K_DOWN]:
self.rect.y += 10
pygame.init()
screen = pygame.display.set_mode((640, 480))
pygame.display.set_caption('St.Patrick game')
clock = pygame.time.Clock()
pygame.mixer.init(44100, -16,2,2048)
pygame.mixer.music.load('Jake Kaufman - Shovel Knight - Specter of Torment OST - 02 From the Shadows (Plains of Passage).mp3')
pygame.mixer.music.play(-1, 0.0)
image = pygame.image.load('Sprite-01.png')
# initialize variables
image_x = 320
image_y = 240
while 1:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
pygame.mixer.music.stop(282)
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
pygame.quit()
pygame.mixer.music.stop(282)
image_x += 0
key = pygame.key.get_pressed()
if key[pygame.K_LEFT]:
image_x -= 10
if key[pygame.K_RIGHT]:
image_x += 10
if key[pygame.K_UP]:
image_y -= 10
if key[pygame.K_DOWN]:
image_y += 10
screen.fill((200, 200, 200))
screen.blit(image, (image_x, image_y))
pygame.display.flip()
如果您不知道在何处插入代码,则意味着您不知道代码在做什么。我知道代码的作用,但不确定在何处插入新代码。如果您不知道在何处插入代码,则必须返回并重新学习。不要将带有代码或错误消息的屏幕截图放在文本中-始终将其放在文本中