Python 尝试使用Pygame制作一个石头布剪刀游戏
我想做一把pygame石头剪刀,但我不知道如何从玩家1切换到玩家2。我希望顶部的player1文本改为player2,代码用来存储第一个玩家选择的内容。问题是,我只是测试它的岩石,只有当你按住鼠标左键并将鼠标放在正确的位置时,它才会改变,而不是只需点击一次Python 尝试使用Pygame制作一个石头布剪刀游戏,python,pygame,Python,Pygame,我想做一把pygame石头剪刀,但我不知道如何从玩家1切换到玩家2。我希望顶部的player1文本改为player2,代码用来存储第一个玩家选择的内容。问题是,我只是测试它的岩石,只有当你按住鼠标左键并将鼠标放在正确的位置时,它才会改变,而不是只需点击一次 import pygame import time pygame.init() # Set up the drawing window screen = pygame.display.set_mode([1000, 600]) # Run
import pygame
import time
pygame.init()
# Set up the drawing window
screen = pygame.display.set_mode([1000, 600])
# Run until the user asks to quit
running = True
while running:
# Did the user click the window close button?
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
#Variables
pygame.font.init()
myfont = pygame.font.SysFont('Comic Sans MS', 50)
rockColour = (0, 0, 0)
rockLight = (100, 100, 100)
paperColour = (255, 255, 255)
paperColourBorder = (0,0,0)
paperBorderLight = (100, 100, 100)
scissorColour = (0, 0, 0)
scissorLight = (100, 100, 100)
P1Choice = ()
Player1 = True
Player2 = False
white = (250, 250, 250)
black = (0, 0, 0)
#P1 Text
textsurface = myfont.render('Player 1', False, black)
screen.blit(textsurface,(400,25))
# Fill the background with white
screen.fill(white)
#mouse
mousepos = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
mouseclick = click [0]
mouserock = 125+150 > mousepos [0] > 125 and 225+150 > mousepos [1] > 225
mousepaper = 400+150 > mousepos [0] > 400 and 225+150 > mousepos [1] > 225
mousescissors = 850 > mousepos [0] > 700 and 375 > mousepos [1] > 255
#ICONS UPDATES
if Player1 == True and Player2 == False:
if mouserock == True:
pygame.draw.circle(screen,rockColour, (200, 300), (75))
textsurface = myfont.render('ROCK', False, black)
screen.blit(textsurface,(137,150))
else:
pygame.draw.circle(screen,rockLight, (200, 300), (75))
if mousepaper == True:
pygame.draw.rect(screen,paperColourBorder, (400, 225, 150, 150))
pygame.draw.rect(screen,paperColour, (410, 235, 130, 130))
textsurface = myfont.render('PAPER', False, black)
screen.blit(textsurface,(400,150))
else:
pygame.draw.rect(screen,paperBorderLight, (400, 225, 150, 150))
pygame.draw.rect(screen,paperColour, (410, 235, 130, 130))
if mousescissors == True:
pygame.draw.line(screen, scissorColour, (700,375), (850,225), 20)
pygame.draw.line(screen, scissorColour, (850,375), (700,225), 20)
textsurface = myfont.render('SCISSORS', False, black)
screen.blit(textsurface,(635,150))
else:
pygame.draw.line(screen, scissorLight, (700,375), (850,225), 20)
pygame.draw.line(screen, scissorLight, (850,375), (700,225), 20)
#MouseClick
if mouserock == True and mouseclick == 1:
Player1 = False
Player2 = True
if 400+150 > mousepos [0] > 400 and 225+150 > mousepos [1] > 225 and click [0] == 1:
P1Choice = ("paper")
if 850 > mousepos [0] > 700 and 375 > mousepos [1] > 255 and click [0] == 1:
P1Choice = ("scissor")
#Player 1 text
if Player1 == False and Player2 == True:
textsurface = myfont.render('Player 1', False, (255, 255, 255))
screen.blit(textsurface,(400,25))
textsurface = myfont.render('Player 2', False, (0, 0, 0))
screen.blit(textsurface,(400,25))
#Flip the display
pygame.display.flip()
# Done! Time to quit.
pygame.quit()
首先,所有这些变量都需要是全局变量,而不是在主游戏循环中(因此,在运行时变量应该在上面),否则当你增加它们,然后通过循环返回时,它们都将重置为初始实现的位置,循环将继续作用于它们。如果没有一个近乎全新的重新启动或严格的编辑,我不能简单地向您展示使这项工作正常运行所需要做的一切。我建议您选择一个教程,指导您制作不同的游戏,学习制作游戏所需的语法和部分,然后回到您的RPS游戏。一旦您了解制作游戏功能所需的部分,正如@Pmac1687所指出的,代码将重新设置主循环中的所有状态变量,因此,状态在每次循环迭代时都会丢失 此外,代码不是获取单个鼠标事件,而是依赖于鼠标的当前状态。这可能会在缓慢的人释放鼠标按钮之前多次循环执行“click has ocked”(单击已发生)代码。将此更改为在事件上触发可修复此问题 屏幕油漆的顺序也有点错误。在屏幕充满白色之前,一些东西正在被写入屏幕 无论如何,我重新安排了代码,并为鼠标添加了事件处理。屏幕布局和点击现在似乎做了一些事情。希望这能给你一个正确的方向
import pygame
import time
pygame.init()
# Set up the drawing window
screen = pygame.display.set_mode([1000, 600])
#Variables
pygame.font.init()
myfont = pygame.font.SysFont('Comic Sans MS', 50)
rockColour = (0, 0, 0)
rockLight = (100, 100, 100)
paperColour = (255, 255, 255)
paperColourBorder = (0,0,0)
paperBorderLight = (100, 100, 100)
scissorColour = (0, 0, 0)
scissorLight = (100, 100, 100)
P1Choice = ()
Player1 = True
Player2 = False
white = (250, 250, 250)
black = (0, 0, 0)
# Run until the user asks to quit
running = True
while running:
mouseclick = False # reset all these
mouserock = False
mousepaper = False
mousescissors = False
click = [ False, False ] # Mouse button clicks
for event in pygame.event.get():
# Did the user click the window close button?
if event.type == pygame.QUIT:
running = False
elif ( event.type == pygame.MOUSEBUTTONDOWN ):
mouseclick = True
mousepos = pygame.mouse.get_pos()
click[ event.button-1 ] = True # which button was pressed
mouserock = 125+150 > mousepos [0] > 125 and 225+150 > mousepos [1] > 225
mousepaper = 400+150 > mousepos [0] > 400 and 225+150 > mousepos [1] > 225
mousescissors = 850 > mousepos [0] > 700 and 375 > mousepos [1] > 255
if 400+150 > mousepos [0] > 400 and 225+150 > mousepos [1] > 225 and click [0] == 1:
P1Choice = ("paper")
if 850 > mousepos [0] > 700 and 375 > mousepos [1] > 255 and click [0] == 1:
P1Choice = ("scissor")
# Fill the background with white
screen.fill(white)
#P1 Text
textsurface = myfont.render('Player 1', False, black)
screen.blit(textsurface,(400,25))
#mouse
#ICONS UPDATES
if Player1 == True and Player2 == False:
if mouserock == True:
pygame.draw.circle(screen,rockColour, (200, 300), (75))
textsurface = myfont.render('ROCK', False, black)
screen.blit(textsurface,(137,150))
else:
pygame.draw.circle(screen,rockLight, (200, 300), (75))
if mousepaper == True:
pygame.draw.rect(screen,paperColourBorder, (400, 225, 150, 150))
pygame.draw.rect(screen,paperColour, (410, 235, 130, 130))
textsurface = myfont.render('PAPER', False, black)
screen.blit(textsurface,(400,150))
else:
pygame.draw.rect(screen,paperBorderLight, (400, 225, 150, 150))
pygame.draw.rect(screen,paperColour, (410, 235, 130, 130))
if mousescissors == True:
pygame.draw.line(screen, scissorColour, (700,375), (850,225), 20)
pygame.draw.line(screen, scissorColour, (850,375), (700,225), 20)
textsurface = myfont.render('SCISSORS', False, black)
screen.blit(textsurface,(635,150))
else:
pygame.draw.line(screen, scissorLight, (700,375), (850,225), 20)
pygame.draw.line(screen, scissorLight, (850,375), (700,225), 20)
#MouseClick
if mouserock == True and mouseclick == 1:
Player1 = False
Player2 = True
#Player 1 text
if Player1 == False and Player2 == True:
textsurface = myfont.render('Player 1', False, (255, 255, 255))
screen.blit(textsurface,(400,25))
textsurface = myfont.render('Player 2', False, (0, 0, 0))
screen.blit(textsurface,(400,25))
#Flip the display
pygame.display.flip()
# Done! Time to quit.
pygame.quit()