Python中赋值前调用的变量
我有一个问题,我需要在多个函数中调用一些变量,但无法让它们在game_loop()函数中声明。变量在game_循环函数之外调用,但变量也被调用到其他函数中。我如何在game_loop函数和所有其他调用它们的函数中获取要调用的变量 请参阅下面的代码:Python中赋值前调用的变量,python,pygame,Python,Pygame,我有一个问题,我需要在多个函数中调用一些变量,但无法让它们在game_loop()函数中声明。变量在game_循环函数之外调用,但变量也被调用到其他函数中。我如何在game_loop函数和所有其他调用它们的函数中获取要调用的变量 请参阅下面的代码: import math import random import pygame from pygame import mixer
import math
import random
import pygame
from pygame import mixer
# Intialize the pygame
pygame.init()
# next setup the display
display_width = 800
display_height = 600
screen = pygame.display.set_mode((800, 600))
# Background
background = pygame.image.load('waterbackground.jpg')
# game clock to time frames per second
clock = pygame.time.Clock()
# Sound
mixer.music.load("ocean.wav")
mixer.music.play(-1)
# setup colors needed in the game
black = (0,0,0)
white = (255, 255, 255)
red = (200, 0, 0)
bright_red = (255,0,0)
block_color = (53,115,255)
green = (0,200,0)
bright_green = (0,255,0)
# Caption and Icon
pygame.display.set_caption("Pirate War")
icon = pygame.image.load('pirateship.png')
pygame.display.set_icon(icon)
# cannon
cannonImg = pygame.image.load('cannonball.png')
cannonX = 0
cannonY = 480
cannonX_change = 0
cannonY_change = 10
cannon_state = "ready"
# Player
playerImg = pygame.image.load('cannon.png')
playerX = 370
playerY = 480
playerX_change = 0
# Score
score_value = 0
# ship
shipImg = []
shipX = []
shipY = []
shipX_change = []
shipY_change = []
num_of_ships = 6
for i in range(num_of_ships):
shipImg.append(pygame.image.load('pirateship.png'))
shipX.append(random.randint(0, 736))
shipY.append(random.randint(50, 150))
shipX_change.append(4)
shipY_change.append(40)
font = pygame.font.Font('freesansbold.ttf', 32)
textX = 10
testY = 10
# Game Over
over_font = pygame.font.Font('freesansbold.ttf', 64)
# text object function called by message display function
def text_objects(text, font):
textSurface = font.render(text, True, black)
return textSurface, textSurface.get_rect()
def show_score(x, y):
score = font.render("Score : " + str(score_value), True, (255, 255, 255))
screen.blit(score, (x, y))
def game_over_text():
over_text = over_font.render("GAME OVER", True, (255, 255, 255))
screen.blit(over_text, (200, 250))
def player(x, y):
screen.blit(playerImg, (x, y))
def ship(x, y, i):
screen.blit(shipImg[i], (x, y))
def fire_cannon(x, y):
global cannon_state
cannon_state = "fire"
screen.blit(cannonImg, (x + 16, y + 10))
def isCollision(shipX, shipY, cannonX, cannonY):
distance = math.sqrt(math.pow(shipX - cannonX, 2) + (math.pow(shipY - cannonY, 2)))
if distance < 27:
return True
else:
return False
def button(msg, x, y, w, h, ic, ac, action=None):
mouse = pygame.mouse.get_pos() # returns a list of [x,y]
click = pygame.mouse.get_pressed()
if x+w > mouse[0] > x and y+h > mouse[1] > y: #check is mouse over button
# redraw the rectange with active color when mouseover
pygame.draw.rect(screen, ac, (x, y, w, h))
#check for a click
if click[0] == 1 and action!=None:
action()
else:
pygame.draw.rect(screen, ic, (x, y, w, h))
# now display text on top of button that was just redrawn
smallText = pygame.font.Font('freesansbold.ttf', 20)
TextSurf, TextRect = text_objects(msg, smallText)
TextRect.center = ((x+(w/2)), (y+(h/2)))
screen.blit(TextSurf, TextRect)
def quitgame():
pygame.quit()
quit()
# start screen code
def game_intro():
intro = True
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
screen.fill((0, 0, 0))
# Background Image
screen.blit(background, (0, 0))
largeText = pygame.font.Font('freesansbold.ttf', 115)
TextSurf, TextRect = text_objects("Pirate War", largeText)
TextRect.center = ((display_width/2), (display_height/2))
screen.blit(TextSurf, TextRect)
#add buttons to start screen
button("Go!",150, 450, 100, 50, green, bright_green, game_loop)
button("Quit",550, 450, 100, 50, red, bright_red, quitgame)
pygame.display.update()
clock.tick(15)
def game_loop():
# Game Loop
running = True
while running:
# RGB = Red, Green, Blue
screen.fill((0, 0, 0))
# Background Image
screen.blit(background, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# if keystroke is pressed check whether its right or left
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX_change = -5
if event.key == pygame.K_RIGHT:
playerX_change = 5
if event.key == pygame.K_SPACE:
if cannon_state == "ready":
cannonSound = mixer.Sound("cannon_x.wav")
cannonSound.play()
# Get the current x cordinate of the spaceship
cannonX = playerX
fire_cannon(cannonX, cannonY)
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 0
# 5 = 5 + -0.1 -> 5 = 5 - 0.1
# 5 = 5 + 0.1
playerX += playerX_change
if playerX <= 0:
playerX = 0
elif playerX >= 736:
playerX = 736
# ship Movement
for i in range(num_of_ships):
# Game Over
if shipY[i] > 440:
for j in range(num_of_ships):
shipY[j] = 2000
game_over_text()
break
shipX[i] += shipX_change[i]
if shipX[i] <= 0:
shipX_change[i] = 1 #######SHIP MOVEMENT
shipY[i] += shipY_change[i]
elif shipX[i] >= 736:
shipX_change[i] = -1 ######SHIP MOVEMENT
shipY[i] += shipY_change[i]
# Collision
collision = isCollision(shipX[i], shipY[i], cannonX, cannonY)
if collision:
explosionSound = mixer.Sound("explosion.wav")
explosionSound.play()
cannonY = 480
cannon_state = "ready"
score_value += 1
shipX[i] = random.randint(0, 736)
shipY[i] = random.randint(50, 150)
ship(shipX[i], shipY[i], i)
# cannon Movement
if cannonY <= 0:
cannonY = 480
cannon_state = "ready"
if cannon_state == "fire":
fire_cannon(cannonX, cannonY)
cannonY -= cannonY_change
player(playerX, playerY)
show_score(textX, testY)
pygame.display.update()
game_intro()
导入数学
随机输入
导入pygame
从pygame导入混合器
#初始化游戏
pygame.init()
#下一步设置显示器
显示宽度=800
显示高度=600
screen=pygame.display.set_模式((800600))
#背景
background=pygame.image.load('waterbackground.jpg')
#游戏时钟到每秒时间帧
clock=pygame.time.clock()
#声音
混音器。音乐。加载(“ocean.wav”)
混音器。音乐。播放(-1)
#设置游戏中需要的颜色
黑色=(0,0,0)
白色=(255,255,255)
红色=(200,0,0)
鲜红色=(255,0,0)
块颜色=(53115255)
绿色=(0200,0)
亮绿色=(0255,0)
#标题和图标
pygame.display.set_标题(“海盗战争”)
icon=pygame.image.load('pirateship.png')
pygame.display.set_图标(图标)
#大炮
cannonImg=pygame.image.load('cannonball.png')
cannonX=0
cannonY=480
cannonX_变化=0
cannonY_变化=10
cannon_state=“就绪”
#玩家
playerImg=pygame.image.load('cannon.png')
playerX=370
playerY=480
playerX_change=0
#得分
分值=0
#船
shipImg=[]
shipX=[]
shipY=[]
shipX_变更=[]
shipY_change=[]
船舶数量=6艘
对于范围内的i(船舶数量):
append(pygame.image.load('pirateship.png'))
shipX.append(random.randint(0736))
shipY.append(random.randint(50150))
发货更改。附加(4)
shipY_change.append(40)
font=pygame.font.font('freesansbold.ttf',32)
textX=10
暴躁=10
#游戏结束
over_font=pygame.font.font('freesansbold.ttf',64)
#消息显示函数调用的文本对象函数
def text_对象(文本、字体):
textSurface=font.render(文本,真,黑色)
返回textSurface,textSurface.get_rect()
def显示_分数(x,y):
score=font.render(“score:+str(score_值),True,(255,255,255))
屏幕blit(分数,(x,y))
def game_over_text():
over_text=over_font.render(“游戏结束”,True,(255,255,255))
屏幕。blit(超过文本(200250))
def播放器(x,y):
屏幕光点(playerImg,(x,y))
def装运(x、y、i):
屏幕显示(shipImg[i],(x,y))
def火力加农炮(x,y):
全球加农奴州
cannon_state=“开火”
屏幕blit(cannonImg,(x+16,y+10))
def isCollision(shipX、shipY、cannonX、cannonY):
距离=math.sqrt(math.pow(shipX-cannonX,2)+(math.pow(shipY-cannonY,2)))
如果距离小于27:
返回真值
其他:
返回错误
def按钮(消息、x、y、w、h、ic、ac、操作=无):
mouse=pygame.mouse.get_pos()#返回[x,y]的列表
click=pygame.mouse.get_pressed()
如果x+w>鼠标[0]>x和y+h>鼠标[1]>y:#检查鼠标是否在按钮上方
#鼠标悬停时,使用活动颜色重新绘制矩形
pygame.draw.rect(屏幕,ac,(x,y,w,h))
#检查是否有单击
如果单击[0]==1并执行操作=无:
行动()
其他:
pygame.draw.rect(屏幕,ic,(x,y,w,h))
#现在在刚刚重新绘制的按钮顶部显示文本
smallText=pygame.font.font('freesansbold.ttf',20)
TextSurf,TextRect=text\u对象(msg,smallText)
TextRect.center=((x+(w/2)),(y+(h/2)))
screen.blit(TextSurf,TextRect)
def quitgame():
pygame.quit()
退出
#启动屏幕代码
def game_intro():
简介=正确
而简介:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
pygame.quit()
退出
屏幕填充((0,0,0))
#背景图像
屏幕光点(背景,(0,0))
largeText=pygame.font.font('freesansbold.ttf',115)
TextSurf,TextRect=text_对象(“海盗战争”,largeText)
text rect.center=((显示宽度/2),(显示
x = 0
def inc() :
global x
x += 1
inc()
print(x)
def game_loop():
global playerX
global playerX_change
global cannonX
global cannonY
global cannon_state