Python Pygame:尽管';而';环
我正在做一个简单的游戏(Dodger游戏的半拷贝),游戏运行了,但没有显示任何内容。我运行了一个while循环,为什么什么都没有显示?这是一个空间问题,图像本身,还是我只是忽略了什么Python Pygame:尽管';而';环,python,python-2.7,pygame,Python,Python 2.7,Pygame,我正在做一个简单的游戏(Dodger游戏的半拷贝),游戏运行了,但没有显示任何内容。我运行了一个while循环,为什么什么都没有显示?这是一个空间问题,图像本身,还是我只是忽略了什么 import pygame,sys,random, os from pygame.locals import * pygame.init() #This One Works!!!!!!! WINDOWHEIGHT = 1136 WINDOWWIDTH = 640 FPS = 40 TEXTCOLOR = (25
import pygame,sys,random, os
from pygame.locals import *
pygame.init()
#This One Works!!!!!!!
WINDOWHEIGHT = 1136
WINDOWWIDTH = 640
FPS = 40
TEXTCOLOR = (255,255,255)
BACKGROUNDCOLOR = (0,0,0)
PLAYERMOVEMENT = 6
HARVEYMOVEMENT = 5
TJMOVEMENT = 7
LASERMOVEMENT = 10
ADDNEWBADDIERATE = 8
COLOR = 0
TJSIZE = 65
HARVEYSIZE = 65
#Check the sizes for these
def terminate():
if pygame.event() == QUIT:
pygame.quit()
def startGame():
while True:
for event in pygame.event.get():
if event.type == QUIT:
terminate()
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
terminate()
return
def playerHasHitBaddies(playerRect,TjVirus,HarVirus):
for b in TjVirus and HarVirus:
if playerRect.colliderect(b['rect']):
return True
return False
def drawText(text,font,surface, x, y):
textobj = font.render(text, 1, TEXTCOLOR)
textrect = textobj.get_rect()
textrect.topleft = (x, y)
surface.blit(textobj, textrect)
mainClock = pygame.time.Clock()
WindowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
pygame.mouse.set_visible(False)
pygame.display.set_caption('Virus')
#Player Images
# Check the name of the .png file
TjImage = pygame.image.load('Virus_TJ_edited-1.png')
TjRect = TjImage.get_rect()
#chanhe this part from the baddies variable in the 'baddies' area
playerImage = pygame.image.load('Tank_RED.png')
playerRect = playerImage.get_rect()
LaserImage = pygame.image.load('laser.png')
LaserRect = LaserImage.get_rect()
pygame.display.update()
startGame()
while True:
TjVirus = []#the red one / make a new one for the blue one
HarVirus = []#The BLue one / Need to create a new dictionary for this one
playerRect.topleft = (WINDOWWIDTH / 2, WINDOWHEIGHT - 50)
moveLeft = moveRight = moveUp = moveDown = laser = False
baddieAddCounter = 0
while True:
for event in pygame.event.get():
if event.type == QUIT:
terminate()
if event.type == KEYDOWN:
if event.key == ord('a'):
moveRight = False
moveLeft = True
if event.key == ord('d'):
moveLeft = False
moveRight = True
if event.key == ord('w'):
moveDown = False
moveUp = True
if event.key == ord('s'):
moveUp = False
moveDown = True
if event.key == K_SPACE:
lasers = True
if event.type == KEYUP:
if evnet.type == K_ESCAPE:
terminate()
if event.key == K_LEFT or event.key == ord('a'):
moveLeft = False
if event.key == K_RIGHT or event.key == ord('d'):
moveRight = False
if event.key == K_UP or event.key == ord('w'):
moveUp = False
if event.key == K_DOWN or event.key == ord('s'):
moveDown = False
if event.key == K_SPACE:
LaserImage.add(LaserRect)
if event.key == ord('j'):
COLOR = 2
if event.key == ord('k'):
if COLOR == 2:
COLOR = 1
playerImage = pygame.image.load('Tank_RED.png')
if COLOR == 1:
COLOR = 2
playerImage = pygame.image.load('Tank_BLUE.png')
if event.type == MOUSEMOTION:
playerRect.move_ip(event.pos[0] - playerRect.centerx, event.pos[1] - playerRect.centery)
if baddieAddCounter == ADDNEWBADDIERATE:
baddieAddCounter = 0
#Dict for TJ(RED) VIRUS
baddieSize = (TJSIZE)
NewTjVirus = {'rect':pygame.Rect(random.rantint(0,WINDOWWIDTH - TJSIZE),0 - TJSIZE,TJSIZE,TJSIZE),
'speed':(TJMOVEMENT),
'surface':pygame.transform.scale(TJImage,(TJSIZE,TJSIZE)),
}
TjVirus.append(NewTjVirus)
#Dict for Harvey(BLUE) virus
baddieSize = (HARVEYSIZE)
NewHarveyVirus = {'rect':pygame.Rect(random.randint(0,WINDOWWIDTH - HARVEYSIZE),0 - HARVEYSIZE,HARVEYSIZE,HARVEYSIZE),
'speed':(HARVEYMOVEMENT),
'surface':pygame.transform.scale(HARVEYSIZE,(HARVEYSIZE,HARVEYSIZE))
}
HarVirus.append(NewHarveyVirus)
#Player Movement
if moveLeft and playerRect.left >0:
playerRect.move_ip(-1*PLAYERMOVEMENT,0)
if moveRight and playerRect.right < WINDOWWIDTH:
playerRect.move_ip(PLAYERMOVEMENT,0)
if moveUp and playerRect.top >0:
playerRect.move_ip(0,-1*PLAYERMOVEMENT)
if moveDown and playerRect.bottom < WINDOWHEIGHT:
playerRect.move_ip(0,PLAYERMOVEMENT)
pygame,mouse.set_pos(playerRect.centerx,playerRect.centery)
#Need to change for each individual virus
for b in HarVirus and TjVirus:
b['rect'].move_ip(0,b['speed'])
for b in HarVirus and TjVirus:
if b['rect'].top > WINDOWHEIGHT:
baddies.remove(b)
windowSurface.fill(pygame.image.load('Background_Proto copy.png'))
for b in HarVirus and TjVirus:
windowSurface.blit(b['surface'],b['rect'])
pygame.display.update()
if playerHasHitBaddies(playerRect,HarVirus,TjVirus):
break
for b in TjVirus and HarVirus[:]:
if b['rect'].top < WINDOWHEIGHT:
HarVirus.remove(b)
TjVirus.remove(b)
mainClock.tick(FPS)
导入pygame、sys、random、os
从pygame.locals导入*
pygame.init()
#这一个有效!!!!!!!
窗高=1136
窗宽=640
FPS=40
TEXTCOLOR=(255255)
背景颜色=(0,0,0)
玩家移动=6
HARVEYMOVEMENT=5
TJMOVEMENT=7
激光运动=10
addnewadderate=8
颜色=0
TJSIZE=65
HARVEYSIZE=65
#检查这些的尺寸
def terminate():
如果pygame.event()=退出:
pygame.quit()
def startGame():
尽管如此:
对于pygame.event.get()中的事件:
如果event.type==退出:
终止()
如果event.type==KEYDOWN:
如果event.key==K_转义:
终止()
返回
def playerHasHitBaddies(playerRect、TjVirus、HarVirus):
对于TjVirus和HarVirus中的b:
如果playerRect.colliderect(b['rect']):
返回真值
返回错误
def drawText(文本、字体、曲面、x、y):
textobj=font.render(text,1,TEXTCOLOR)
textrect=textobj.get_rect()
textrect.topleft=(x,y)
surface.blit(textobj,textrect)
mainClock=pygame.time.Clock()
WindowSurface=pygame.display.set_模式((窗口宽度、窗口高度))
pygame.mouse.set_可见(False)
pygame.display.set_标题('病毒')
#玩家形象
#检查.png文件的名称
TjImage=pygame.image.load('Virus\u TJ\u edited-1.png')
TjRect=TjImage.get_rect()
#chanhe此部分来自“baddies”区域中的baddies变量
playerImage=pygame.image.load('Tank\u RED.png'))
playerRect=playerImage.get_rect()
LaserImage=pygame.image.load('laser.png')
LaserRect=LaserImage.get_rect()
pygame.display.update()
startGame()
尽管如此:
TjVirus=[]#红色的/为蓝色的做一个新的
HarVirus=[]#蓝色的/需要为此创建一个新字典
playerRect.topleft=(窗口宽度/2,窗口高度-50)
moveLeft=moveRight=moveUp=moveDown=laser=False
baddieAddCounter=0
尽管如此:
对于pygame.event.get()中的事件:
如果event.type==退出:
终止()
如果event.type==KEYDOWN:
如果event.key==ord('a'):
moveRight=False
moveLeft=True
如果event.key==ord('d'):
moveLeft=False
moveRight=True
如果event.key==ord('w'):
向下移动=错误
向上移动=真
如果event.key==ord('s'):
moveUp=False
向下移动=真
如果event.key==K_空间:
激光=真
如果event.type==KEYUP:
如果evnet.type==K_转义:
终止()
如果event.key==K_LEFT或event.key==ord('a'):
moveLeft=False
如果event.key==K_RIGHT或event.key==ord('d'):
moveRight=False
如果event.key==K_UP或event.key==ord('w'):
moveUp=False
如果event.key==K_DOWN或event.key==ord('s'):
向下移动=错误
如果event.key==K_空间:
LaserImage.add(LaserRect)
如果event.key==ord('j'):
颜色=2
如果event.key==ord('k'):
如果颜色==2:
颜色=1
playerImage=pygame.image.load('Tank\u RED.png'))
如果颜色==1:
颜色=2
playerImage=pygame.image.load('Tank_BLUE.png'))
如果event.type==MOUSEMOTION:
playerRect.move_ip(事件位置[0]-playerRect.centerx,事件位置[1]-playerRect.centery)
如果baddieAddCounter==AddNewAddRate:
baddieAddCounter=0
#TJ(红色)病毒的Dict
baddieSize=(TJSIZE)
NewTjVirus={'rect':pygame.rect(random.rantint(0,WINDOWWIDTH-TJSIZE),0-TJSIZE,TJSIZE,TJSIZE),
“速度”:(TJMOVEMENT),
“surface”:pygame.transform.scale(TJImage,(TJSIZE,TJSIZE)),
}
TjVirus.append(NewTjVirus)
#哈维(蓝色)病毒口述
baddieSize=(HARVEYSIZE)
NewHarveyVirus={'rect':pygame.rect(random.randint(0,WINDOWWIDTH-HARVEYSIZE),0-HARVEYSIZE,HARVEYSIZE,HARVEYSIZE),
“速度”:(HARVEYMOVEMENT),
“曲面”:pygame.transform.scale(HARVEYSIZE,(HARVEYSIZE,HARVEYSIZE))
}
附加病毒(新病毒)
#球员运动
如果moveLeft和playerRect.left>0:
playerRect.移动ip(-1*PLAYERMOVEMENT,0)
如果moveRight和playerRect.right<窗口宽度:
playerRect.移动ip(PLAYERMOVEMENT,0)
如果moveUp和playerRect.top>0:
playerRect.move_ip(0,-1*PLAYERMOVEMENT)
如果向下移动并播放rect.bottom<窗口高度:
playerRect.移动ip(0,PLAYERMOVEMENT)
pygame,鼠标。设置位置(playerRect.centerx,playerRect.centery)
#需要针对每个病毒进行更改
对于HarVirus和TjVirus中的b:
b['rect'].移动ip(0,b['speed'])
对于HarVirus和TjVirus中的b:
如果b['rect'].top>WINDOWHEIGHT:
坏家伙。移除(b)
windowSurface.fill(pygame.image.load('Background\u Proto copy.png'))
对于HarVirus和TjVirus中的b:
windowSurface.blit(b['surface',b['rect']))
pygame.display.update()
如果playerHasHitBaddies(playerRect,
def startGame():
while True:
for event in pygame.event.get():
if event.type == QUIT:
terminate()
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
terminate()
return
def terminate():
if pygame.event() == QUIT:
pygame.quit()
pygame,mouse.set_pos(playerRect.centerx,playerRect.centery)