我的总结性项目遇到了很多麻烦,你必须使用pygame在python中制作游戏。我目前仍在努力为我的游戏制作一个菜单屏幕,问题是我无法显示我的按钮,然后在点击开始后如何更改为我的游戏地图。我一直得到类型错误“TypeError:button()需要6到7个位置参数,但9个被给出”如果你们中的任何人可以帮助那意味着很多。感谢。
#army man man
import pygame
from pygame import *
from tkinter import *
import time
import random
win = pygame.display.set_mode((1280,720))
pygame.display.set_caption("Armymanman")
walkright = [pygame.image.load('walk1.png'),pygame.image.load('walk2.png'),pygame.image.load('walk3.png'),pygame.image.load('walk4.png'),pygame.image.load('walk5.png'),pygame.image.load('walk6.png'),pygame.image.load('walk7.png'),pygame.image.load('walk8.png'),pygame.image.load('walk9.png')]
walkleft = [pygame.image.load('walk1L.png'),pygame.image.load('walk2L.png'),pygame.image.load('walk3L.png'),pygame.image.load('walk4L.png'),pygame.image.load('walk5L.png'),pygame.image.load('walk6L.png'),pygame.image.load('walk7L.png'),pygame.image.load('walk8L.png'),pygame.image.load('walk9L.png')]
backg = pygame.image.load('pythonbg.png')
#resize char image
char = pygame.image.load('stand.png')
char = pygame.transform.scale(char,(64,64))
x=0
y=720
w=1280
h=720
black=(0,0,0)
red=(255,0,0)
blue=(0,0,255)
green=(0,255,0)
white=(255,255,255)
clock = pygame.time.Clock()
#plays music does not work?
#def playmusic():
pygame.init()
pygame.mixer.pre_init(22050, -16, 2, 2048) # setup mixer
pygame.mixer.init()
pygame.mixer.music.load('bgmusic.ogg')
pygame.mixer.music.play(-1)
#playmusic()
##song=pygame.mixer.sound('bgmusic.ogg')
##
##song.play()
class player(object):
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.move = 5
self.isjump = False
## self.isjump=0
self.jumpcount = 10
self.left = False
self.right = False
self.walkcount = 0
def draw(self,win):
if self.walkcount + 1 >=27:
self.walkcount = 0
if self.left:
win.blit(walkleft[self.walkcount//3], (self.x,self.y))
self.walkcount += 1
elif self.right:
win.blit(walkright[self.walkcount//3], (self.x,self.y))
self.walkcount += 1
else:
win.blit(char, (self.x,self.y))
def redrawwindow():
win.blit(backg, (0,0))
man.draw(win)
pygame.display.update()
startsc=pygame.image.load('armymanbg.jpg')
startsc=pygame.transform.scale(startsc,[1280,720])
win.blit(startsc,(0,0))
pygame.display.flip()
DIS=1
def button(x,y,w,h,ic,ac,action=None):
mouse=pygame.mouse.get_pos()
click= pygame.mouse.get_pressed()
if x+w>mouse[0] > x and y+h > mouse[1] > y:
global DIS
global intro
pygame.draw.rect(win,ac,(x,y,w,h))
if click[0] == 1 and action!= None:
if action=="START":
pygame.display.update()
time.sleep(0.5)
run= False
DIS=2
elif action=="EXIT":
pygame.quit()
quit()
elif action=="Menu":
DIS=1
else:
return True
else:
pygame.draw.rect(window, ic, (x,y,w,h))
text = pygame.font.SysFont('freesonsbold.ttf',25)
win.blit(text.render("BECOME THE ARMYMANMAN!", True,(black)), (x+(w/3.1), y+(h/2.5)))
win.blit(text.render("Start",True,(black)), (x+(w/3.1), y+(h/2.5)))
win.blit(text.render("Exit",True,(black)), (x+(w/3.1), y+(h/2.5)))
#MAIN LOOP
man = player(0, 656, 64, 64)
run = True
while run:
clock.tick(27)
if DIS==1:
for event in pygame.event.get():
if event.type==pygame.QUIT:
quit()
win.fill((0,0,0))
button("START",400, 500, 150, 75,green,blue,"START")
button("EXIT", 400,650,150,75,green,red,"EXIT" )
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
if (event.type==pygame.KEYDOWN and event.key == K_RIGHT)and man.x < 1280 - man.width - man.move:
man.x += man.move
man.right = True
man.left = False
elif (event.type==pygame.KEYDOWN and event.key == K_LEFT)and man.x > man.move:
man.x -= man.move
man.left = True
man.right = False
else:
man.right= False
man.left = False
man.walkcount = 0
if (event.type==pygame.KEYDOWN and event.key == K_SPACE):
man.isjump = True
man.right = False
man.left = False
man.walkcount = 0
else:
if(man.isjump==True):
if man.jumpcount >= -10:
neg = 1
if man.jumpcount < 0:
neg = -1
man.y -= (man.jumpcount ** 2) *0.5 * neg
man.jumpcount -= 1
else:
man.isjump = False
man.jumpcount = 10
redrawwindow()
print(man.y)
pygame.quit
答案 0 :(得分:0)
对于初学者来说,你真的不应该from pygame import *
,因为它会用你不想要的很多东西污染你的命名空间,from pygame.locals import *
更正常。您提到的TypeError
是由于您提供的button()
参数太多。您可以看到定义它时需要六个或七个参数(def button(x,y,w,h,ic,ac,action=None):
)。当你调用它时,你试图传递太多(button("START",400, 500, 150, 75,green,blue,"START")
)。我认为最初的"START"
参数是多余的。以下是我注意到的其他一些事情:
if(man.isjump==True):
可以替换为if man.isjump:
。似乎有很多类似的多余括号。你不需要比较它们,除非它像if x and (a or b)
之类的东西,它会产生影响。大多数函数参数也不需要它们。这个带有(black)
的按钮绘制线就是一个例子。pygame.quit
是不必要的,无论如何都不会有用(如果你需要它,你不会,pygame.quit()
。pygame.draw.rect(window, ic, (x,y,w,h))
)中,您绘制为window
而不是win
,这会打破它。pygame.quit
之后的代码的整段代码)。if (event.type==pygame.KEYDOWN and event.key == K_RIGHT)
可以替换为if pygame.key.get_pressed()[K_RIGHT]
,左侧可以替换为redrawwindow()
。这些以及for event in pygame.event.get():
应该在// "obj" has the same type of *this, it's just another string object
string_base<T>(const string_base<T> &obj)
: len(obj.length()), cap(obj.capacity()) {
raw_data = new T[cap];
for (unsigned i = 0; i < cap; i++)
raw_data[i] = obj.data()[i];
raw_data[len] = 0x00;
}
循环之外。