python游戏循环设置_Pygame:游戏循环前的初始菜单
我正在制作一个this game的版本,并尝试将起始菜单设置为:
我的计划是先做surface.fill(overlaycolor),然后将这个图像blitting到屏幕上。在一个while循环中。之后,在用户想玩(另一个问题除外)之后,我们将进入另一个while循环。在
我试过这么做,但这只会给我一个黑幕。在
这是我的代码:import pygame, sys, random
from pygame.locals import *
# set up pygame
pygame.init()
mainClock = pygame.time.Clock()
# set up the window
width = 800
height = 600
thesurface = pygame.display.set_mode((width, height), 0, 32)
pygame.display.set_caption('')
bg = pygame.image.load("bg.png")
menu1 = pygame.image.load("menu1.png")
basicFont = pygame.font.SysFont('calibri', 36)
# set up the colors
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
overlay = (121, 126, 128)
playercolor = BLUE
# set up the player and food data structure
foodCounter = 0
NEWFOOD = 20
FOODSIZE = 10
splitting = False
player = pygame.draw.circle(thesurface, playercolor, (60, 250), 40)
foods = []
for i in range(20):
foods.append(pygame.Rect(random.randint(0, width - FOODSIZE), random.randint(0, height - FOODSIZE), FOODSIZE, FOODSIZE))
# set up movement variables
moveLeft = False
moveRight = False
moveUp = False
moveDown = False
MOVESPEED = 10
size = 10
score = size
gameisplaying = False
while True:
thesurface.fill(overlay)
thesurface.blit(menu1, (400,400))
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONUP:
pos = pygame.mouse.get_pos()
gameisplaying = True
break
# run the game loop
while gameisplaying:
thesurface.blit(bg, (0, 0))
# check for events
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
# change the keyboard variables
if event.key == K_LEFT or event.key == ord('a'):
moveRight = False
moveLeft = True
if event.key == K_RIGHT or event.key == ord('d'):
moveLeft = False
moveRight = True
if event.key == K_UP or event.key == ord('w'):
moveDown = False
moveUp = True
if event.key == K_DOWN or event.key == ord('s'):
moveUp = False
moveDown = True
if event.key == K_SPACE and size >= 32: # XXX if size and space set splitting to true
splitting = True
if event.type == KEYUP:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
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 == ord('x'):
player.top = random.randint(0, height - player.height)
player.left = random.randint(0, width - player.width)
if event.type == MOUSEBUTTONUP:
foods.append(pygame.Rect(event.pos[0], event.pos[1], FOODSIZE, FOODSIZE))
foodCounter += 1
if foodCounter >= NEWFOOD:
# add new food
foodCounter = 0
foods.append(pygame.Rect(random.randint(0, width - FOODSIZE), random.randint(0, height - FOODSIZE), FOODSIZE, FOODSIZE))
if 100>score>50:
MOVESPEED = 9
elif 150>score>100:
MOVESPEED = 8
elif 250>score>150:
MOVESPEED = 6
elif 400>score>250:
MOVESPEED = 5
elif 600>score>400:
MOVESPEED = 3
elif 800>score>600:
MOVESPEED = 2
elif score>800:
MOVESPEED = 1
# move the player
if moveDown and player.bottom < height:
player.top += MOVESPEED
if moveUp and player.top > 0:
player.top -= MOVESPEED
if moveLeft and player.left > 0:
player.left -= MOVESPEED
if moveRight and player.right < width:
player.right += MOVESPEED
# splitting
if not splitting:
pygame.draw.circle(thesurface, playercolor, player.center, size)
else:
pygame.draw.circle(thesurface, playercolor,(player.centerx,player.centery),int(size/2))
pygame.draw.circle(thesurface, playercolor,(player.centerx+size,player.centery+size),int(size/2))
# check if the player has intersected with any food squares.
for food in foods[:]:
if player.colliderect(food):
foods.remove(food)
size+=1
# draw the food
for i in range(len(foods)):
pygame.draw.rect(thesurface, GREEN, foods[i])
printscore = basicFont.render("Score: %d" % size, True, (0,0,0))
thesurface.blit(printscore, (10, 550))
pygame.display.update()
# draw the window onto the thesurface
pygame.display.update()
mainClock.tick(80)
感谢帮助!在
python游戏循环设置_Pygame:游戏循环前的初始菜单相关推荐
- Win11游戏性能设置 提高游戏性能方法
很多用户担心升级Win11系统之后会影响游戏体验感,但是要是做好性能优化,将赋予游戏更大的空间去提升效果和玩法.那Win11怎么提高游戏性能呢?下面我们就来一起看看. Win11 ...
- python游戏循环设置_python - 游戏脚本之无脑的循环挑战
三国挂机手游session 模拟点按并循环,在指定位置截图 # coding: utf-8 import os import time import wda import random #import ...
- 用python给游戏加上音效_pygame游戏之旅 添加icon和bgm音效的方法
本文为大家分享了pygame游戏之旅的第14篇,供大家参考,具体内容如下 添加icon需要用的函数是: gameIcon = pygame.image.load("carIcon.png&q ...
- TC Games电脑玩手游绝地求生:刺激战场电脑版游戏按键设置教程
TC Games电脑玩手游助手介绍: TC Games电脑玩手机游戏软件,是安卓手游必备助手,通过WIFI/USB一键连接手机和电脑,帮助玩家用鼠标/键盘/快捷键,在电脑端玩转手游.TCGames无需 ...
- 森林正版服务器,《森林》正式版服务器怎么设置 游戏专用服务器设置教程
导 读 对于想要联机的小伙伴来说如果拥有自己的服务器那是一件再好不过的时间了,所以今天小编为大家带来的就是玩家"ccsingtao"翻译整理分享的森林游戏专用服务器设置教程,不会创 ...
- python循环10次_开发一个循环 5 次计算的小游戏, 设置随机种子为10,每次随机产生两个 1~10的数字以及随机选择...
开发一个循环 5 次计算的小游戏, 设置随机种子为10,每次随机产生两个 1~10的数字以及随机选择 "+.-.*"运算符,构成一个表达式, 让用户计算式子结果并输入结果,如果计算 ...
- python|利用打飞机小游戏学习分支和循环|今天是记录自己学习的第四天呀!加油!
** python|利用打飞机小游戏学习分支和循环|今天是记录自己学习的第四天呀!加油! ** /*** Author:Jiang Zhiying Data:2017.11.11 Emial:1443 ...
- 开发一个循环 5 次计算的小游戏, 设置随机种子为10,每次随机产生两个 1~10的数字以及随机选择
开发一个循环 5 次计算的小游戏, 设置随机种子为10,每次随机产生两个 1~10的数字以及随机选择 "+.-.*"运算符,构成一个表达式, 让用户计算式子结果并输入结果,如果计算 ...
- python一直循环怎么写_自学Python,写一个挨打的游戏代码来初识While循环
自学Python的第11天. 旋转~跳跃~,我~闭着眼! 学完循环, 沐浴着while的光芒,闲来无事和同事一起扯皮, 我说:"编程语言好神奇, 一个小小的循环,竟然在生活中也可以找到原理和 ...
最新文章
- python爬虫教程视频-python爬虫(入门教程、视频教程)
- 在统一软件开发过程中使用UML
- mysql scope runtime_Maven依赖scope属性详解-一个报错引发的问题 - 老郭种树
- oracle从入门到精通_【论文】基于SPSS Modeler和Oracle的学生行为数据分析
- 集合去重 (集合元素为引用类型)--- java 8 新特性 --- 根据元素单属性、多属性实现去重
- 红尘EMLOG技术导航分类网主题模板
- 实例浅析javascript call by value与call by reference
- 罗永浩直播带货被吐槽不是全网最低价 本人亲自回应了
- radar(nyoj287贪心)
- 做游戏,学编程(C语言) 6 数组之空战游戏
- 爬虫是后端吗_北京市互联网后端职位招聘需求分析
- 拓端tecdat|WinBUGS对多元随机波动率模型:贝叶斯估计与模型比较
- Tomcat6.0 配置外部数据源(JNDI)
- Poi导入校验因单元格格式产生的空对象问题
- PlayYou 捉弄人软件(手机控制电脑)
- java生气_Java来抢饭碗,C++可别生气
- 金笛全新技术架构,鲲鹏击浪从兹始
- 国外著名大学网络课堂
- L3-python语言中的几种特征操作
- 构建多平台Docker镜像