【发布时间】:2015-12-15 00:18:56
【问题描述】:
我们正在制作的游戏存在一些问题。问题是玩家 1 和玩家 2 只是跳跃,而不是像我们为他们编程的那样向左或向右移动。有人可以看看我们的代码,看看我们哪里出错了吗?
谢谢你 楚格利迪德
主游戏代码:
#The Battle of...
import pygame, sys, random
from livewires import games, color
from classes import *
from process import *
from pygame.locals import *
pygame.init()
SCREENWIDTH = 800
SCREENHEIGHT = 640
screen = pygame.display.set_mode((SCREENWIDTH, SCREENHEIGHT), 0, 32)
clock = pygame.time.Clock()
FPS = 24
background = pygame.image.load("background1.png")
bug_list = ["Bob.jpg", "Sophia.png", "Raxster.png", "Tortle.png"]
width_list = [193, 60, 218, 60]
height_list = [277, 94, 246, 100]
x = random.randrange(0, len(bug_list), 1)
y = random.randrange(0, len(bug_list), 1)
Player1 = Bug(0,
SCREENHEIGHT - height_list[x],
SCREENWIDTH,
SCREENHEIGHT,
bug_list[x])
Player2 = Bug(SCREENWIDTH - width_list[y],
SCREENHEIGHT - height_list[y],
SCREENWIDTH,
SCREENHEIGHT,
bug_list[y])
def game():
while True:
Player1.motion(SCREENWIDTH, SCREENHEIGHT)
process1(Player1)
Player2.motion(SCREENWIDTH, SCREENHEIGHT)
process2(Player2)
BaseClass.allsprites.draw(screen)
pygame.display.flip()
screen.blit(background, (0,0))
clock.tick(FPS)
def main():
if __name__ == "__main__":
pygame.display.set_caption('Game Menu')
menu_items = ('Start', 'Quit')
funcs = {'Start': game,
'Quit': sys.exit}
gm = GameMenu(screen, funcs.keys(), funcs)
gm.run()
main()
类代码:
import pygame, random, sys
pygame.init()
WHITE = (255, 255, 255)
RED = (255, 0, 0)
BLACK = (0, 0 , 0)
class MenuItem(pygame.font.Font):
def __init__(self, text, font=None, font_size=30,
font_color=(WHITE), (pos_x, pos_y)=(0, 0)):
pygame.font.Font.__init__(self, font, font_size)
self.text = text
self.font_size = font_size
self.font_color = font_color
self.label = self.render(self.text, 1, self.font_color)
self.width = self.label.get_rect().width
self.height = self.label.get_rect().height
self.dimensions = (self.width, self.height)
self.pos_x = pos_x
self.pos_y = pos_y
self.position = pos_x, pos_y
self.is_selected = False
def is_mouse_selection(self, (posx, posy)):
if (posx >= self.pos_x and posx <= self.pos_x + self.width) and \
(posy >= self.pos_y and posy <= self.pos_y + self.height):
return True
return False
def set_position(self, x, y):
self.position = (x, y)
self.pos_x = x
self.pos_y = y
def set_font_color(self, rgb_tuple):
self.font_color = rgb_tuple
self.label = self.render(self.text, 1, self.font_color)
class GameMenu():
def __init__(self,
screen,
items,
funcs,
bg_color = BLACK,
font = None,
font_size = 30,
font_color = WHITE):
self.screen = screen
self.scr_width = self.screen.get_rect().width
self.scr_height = self.screen.get_rect().height
self.bg_color = bg_color
self.clock = pygame.time.Clock()
self.funcs = funcs
self.items = []
for index, item in enumerate(items):
menu_item = MenuItem(item, font, font_size, font_color)
# t_h: total height of text block
t_h = len(items) * menu_item.height
pos_x = (self.scr_width / 2) - (menu_item.width / 2)
pos_y = (self.scr_height / 2) - (t_h / 2) + \
((index * 2) + index * menu_item.height)
menu_item.set_position(pos_x, pos_y)
self.items.append(menu_item)
self.mouse_is_visible = True
self.cur_item = None
def set_mouse_visibility(self):
if self.mouse_is_visible:
pygame.mouse.set_visible(True)
else:
pygame.mouse.set_visible(False)
def set_item_selection(self, key):
"""Marks the MenuItem chosen via up and down keys."""
for item in self.items:
#Return all to neutral
item.set_italic(False)
item.set_font_color(WHITE)
if self.cur_item is None:
self.cur_item = 0
else:
#Find the chosen item
if key == pygame.K_UP and self.cur_item > 0:
self.cur_item -= 1
elif key == pygame.K_UP and self.cur_item == 0:
self.cur_item = len(self.items) - 1
elif key == pygame.K_DOWN and \
self.cur_item < len(self.items) - 1:
self.cur_item += 1
elif key == pygame.K_DOWN and \
self.cur_item == len(self.items) - 1:
self.cur_item = 0
self.items[self.cur_item].set_italic(True)
self.items[self.cur_item].set_font_color(RED)
#Finally check if Enter or Space is pressed
if key == pygame.K_SPACE or key == pygame.K_RETURN:
text = self.items[self.cur_item].text
selfs.funcs[text]()
def set_mouse_selection(self, item, mpos):
"""Marks the MenuItem the mouse cursor hovers on."""
if item.is_mouse_selection(mpos):
item.set_font_color(RED)
item.set_italic(True)
else:
item.set_font_color(WHITE)
item.set_italic(False)
def run(self):
mainloop = True
while mainloop:
#Limit frame speed to 50 FPS
self.clock.tick(50)
mpos = pygame.mouse.get_pos()
for event in pygame.event.get():
if event.type == pygame.QUIT:
mainloop = False
if event.type == pygame.KEYDOWN:
self.mouse_is_visible = False
self.set_item_selection(event.key)
if event.type == pygame.MOUSEBUTTONDOWN:
for item in self.items:
if item.is_mouse_selection(mpos):
self.funcs[item.text]()
if pygame.mouse.get_rel() != (0, 0):
self.mouse_is_visible = True
self.cur_item = None
#Is the mouse visible?
self.set_mouse_visibility()
#Redraw the background
self.screen.fill(self.bg_color)
for item in self.items:
if self.mouse_is_visible:
self.set_mouse_selection(item, mpos)
self.screen.blit(item.label, item.position)
pygame.display.flip()
class BaseClass(pygame.sprite.Sprite):
allsprites = pygame.sprite.Group()
def __init__(self, x, y, width, height, image_string):
pygame.sprite.Sprite.__init__(self)
BaseClass.allsprites.add(self)
self.image = pygame.image.load(image_string)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.width = width
self.height = height
class Bug(BaseClass):
List = pygame.sprite.Group()
def __init__(self, x, y, width, height, image_string):
BaseClass.__init__(self, x, y, width, height, image_string)
self.velx, self.vely = 0, 5
self.jumping, self.go_down = False, False
def motion(self, SCREENWIDTH,SCREENHEIGHT):
keys = pygame.key.get_pressed()
predicted_location = self.rect.x + self.velx
if predicted_location < 0:
self.velx = 0
elif predicted_location + self.width > SCREENWIDTH:
self.velx = 0
self.rect.x += self.velx
self.__jump(SCREENHEIGHT)
def __jump(self, SCREENHEIGHT):
max_jump = 75
if self.jumping:
if self.rect.y < max_jump:
self.go_down = True
if self.go_down:
self.rect.y += self.vely
predicted_location = self.rect.y + self.vely
if predicted_location + self.height > SCREENHEIGHT:
self.jumping = False
self.go_down = False
if predicted_location + self.height < 0:
self.jumping = False
self.go_down = False
else:
self.rect.y -= self.vely
处理代码:
import pygame, sys
def process1(bug):
#PROCESSING
keys = pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if keys[pygame.K_d]:
bug.velx = 5
if keys[pygame.K_a]:
bug.velx = -5
if keys[pygame.K_w]:
bug.jumping = True
#attacks
if keys[pygame.K_1]:
bug.velx = 50
if keys[pygame.K_2]:
bug.velx = -50
else:
bug.velx = 0
def process2(bug):
keys = pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if keys[pygame.K_d]:
bug.velx = 5
if keys[pygame.K_a]:
bug.velx = -5
if keys[pygame.K_w]:
bug.jumping = True
#attacks
if keys[pygame.K_1]:
bug.velx = 50
if keys[pygame.K_2]:
bug.velx = -50
else:
bug.velx = 0
【问题讨论】:
-
“为什么我的代码不起作用?”仅当您提供清晰的问题陈述和重现问题所需的最少代码量时,才能接受问题。不要只是粘贴整个代码,没有人会阅读它。
-
您好,感谢您告诉我们!不幸的是,我们不知道问题出在哪里,因为我们已经检查了好几次。所以我们不知道要发布哪个“最少的代码”,因为据我们所知,它可能在任何地方。
-
那么这是学习调试的绝佳机会。包括打印语句并尝试在代码中找到问题开始的地方。丢弃与错误无关的所有内容。
-
@timgeb 请帮帮我,因为我被允许这是我们代码中的内容。
-
如果玩家一直在跳跃,那么您可能在某些地方忘记了
bug.jumping = False。使用print()跟踪变量并找到bug.jumping = False的正确位置
标签: python python-2.7 pygame