pygame

pygame get key pressed as a string [duplicate]

∥☆過路亽.° 提交于 2021-02-15 06:47:46
问题 This question already has answers here : Pygame: Is there any easy way to find the letter/number of ANY alphanumeric pressed? (2 answers) Closed last year . I want to change the text in the controls screen based on the key which the user presses. how do I convert pygame.event.get() into a string that shows which key has been pressed? preferably without many if staments for event in pygame.event.get(): if event.type == pygame.KEYDOWN: slectedKey = # get key name as a string print(slectedKey)

Conway's Game of life pygame implementation not working with a copy of the grid, not using numpy

只愿长相守 提交于 2021-02-15 06:46:17
问题 I'm trying to implement Conway's game of life in python using pygame. However, it seems there is a problem with the calculation of each cell in the main loop. To ensure each calculation is done at the same time, I created a copy of the grid and got the calculations on that grid. Nevertheless, the program is not working and I can't figure it out. Here is a copy of my code. import pygame import time def create_grid(ROWS, COLS, SCREEN): """Creates a grid, sets all values to 0""" assert ROWS > 0,

Playing midi on Pygame

陌路散爱 提交于 2021-02-15 04:21:13
问题 Ok, this is what I got: import pygame import sys from pygame.locals import * bif="bg.jpg" mif="pkmn.png" sif="bubble.png" song_1="testaudio.mid" pygame.init() FPS = 30 # FPS FPSCLOCK = pygame.time.Clock() # FPS screen = pygame.display.set_mode ((600,375),0,32) intro=pygame.mixer.Sound(song_1) intro.play() background = pygame.image.load(bif).convert() pygame.mouse.set_visible(0) char = pygame.image.load(mif).convert_alpha() x = screen.get_width()/2 - char.get_width()/2 y = screen.get_height()

Playing midi on Pygame

天大地大妈咪最大 提交于 2021-02-15 04:20:41
问题 Ok, this is what I got: import pygame import sys from pygame.locals import * bif="bg.jpg" mif="pkmn.png" sif="bubble.png" song_1="testaudio.mid" pygame.init() FPS = 30 # FPS FPSCLOCK = pygame.time.Clock() # FPS screen = pygame.display.set_mode ((600,375),0,32) intro=pygame.mixer.Sound(song_1) intro.play() background = pygame.image.load(bif).convert() pygame.mouse.set_visible(0) char = pygame.image.load(mif).convert_alpha() x = screen.get_width()/2 - char.get_width()/2 y = screen.get_height()

Playing midi on Pygame

我的未来我决定 提交于 2021-02-15 04:19:33
问题 Ok, this is what I got: import pygame import sys from pygame.locals import * bif="bg.jpg" mif="pkmn.png" sif="bubble.png" song_1="testaudio.mid" pygame.init() FPS = 30 # FPS FPSCLOCK = pygame.time.Clock() # FPS screen = pygame.display.set_mode ((600,375),0,32) intro=pygame.mixer.Sound(song_1) intro.play() background = pygame.image.load(bif).convert() pygame.mouse.set_visible(0) char = pygame.image.load(mif).convert_alpha() x = screen.get_width()/2 - char.get_width()/2 y = screen.get_height()

python常用标准库和第三方库

别等时光非礼了梦想. 提交于 2021-02-14 10:01:51
python 常用的标准库及第三方库 标准库 Python拥有一个强大的标准库。Python语言的核心只包含数字、字符串、列表、字典、文件等常见类型和函数,而由Python标准库提供了系统管理、网络通信、文本处理、数据库接口、图形系统、XML处理等额外的功能。 Python标准库的主要功能有: 1.文本处理,包含文本格式化、正则表达式匹配、文本差异计算与合并、Unicode支持,二进制数据处理等功能 2.文件处理,包含文件操作、创建临时文件、文件压缩与归档、操作配置文件等功能 3.操作系统功能,包含线程与进程支持、IO复用、日期与时间处理、调用系统函数、日志(logging)等功能 4.网络通信,包含网络套接字,SSL加密通信、异步网络通信等功能 5.网络协议,支持HTTP,FTP,SMTP,POP,IMAP,NNTP,XMLRPC等多种网络协议,并提供了编写网络服务器的框架 6.W3C格式支持,包含HTML,SGML,XML的处理。 7.其它功能,包括国际化支持、数学运算、HASH、Tkinter等 Python社区提供了大量的第三方模块,使用方式与标准库类似。它们的功能覆盖科学计算、Web开发、数据库接口、图形系统多个领域。第三方模块可以使用Python或者C语言编写。SWIG,SIP常用于将C语言编写的程序库转化为Python模块。Boost C++

snake eating food

浪尽此生 提交于 2021-02-14 08:50:09
pygame 初始化 # 初始化游戏 pygame.init() # 初始化pygame screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT]) # 初始化窗口 pygame.display.set_caption('This is my first pygame-program') # 设置窗口标题 #!/usr/bin/env python # -*- coding:utf-8 -*- #@Time : 2019/3/24 15:08 #@File : yuanzu.py import pygame import sys import random # 全局定义 SCREEN_X = 600 SCREEN_Y = 600 # 蛇类 # 点以25为单位 class Snake(object): # 初始化各种需要的属性 [开始时默认向右/身体块x5] def __init__(self): self.dirction = pygame.K_RIGHT self.body = [] for x in range(5): self.addnode() # 无论何时 都在前端增加蛇块 def addnode(self): left, top = (0, 0) if self.body: left, top =

PyGame press two buttons at the same time

人走茶凉 提交于 2021-02-13 17:40:57
问题 I wrote this: import pygame finish = False while not finish: for event in pygame.event.get(): if event.type == pygame.QUIT: finish = True if event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: print "A" if event.key == pygame.K_RIGHT: print "B" if event.key == pygame.K_LEFT: print "C" Why can't I press two buttons at the same time? 回答1: The keyboard events (e.g. pygame.KEYDOWN ) occure only once when a button is pressed. Use pygame.key.get_pressed() to continuously evaluate the states

PyGame press two buttons at the same time

允我心安 提交于 2021-02-13 17:38:14
问题 I wrote this: import pygame finish = False while not finish: for event in pygame.event.get(): if event.type == pygame.QUIT: finish = True if event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: print "A" if event.key == pygame.K_RIGHT: print "B" if event.key == pygame.K_LEFT: print "C" Why can't I press two buttons at the same time? 回答1: The keyboard events (e.g. pygame.KEYDOWN ) occure only once when a button is pressed. Use pygame.key.get_pressed() to continuously evaluate the states

How can i make a block follow another block in pygame [duplicate]

梦想的初衷 提交于 2021-02-13 17:37:54
问题 This question already has answers here : How to make smooth movement in pygame (2 answers) Closed 4 months ago . I have two blocks, one is controlled by the user. When i move my block, i want the other block to follow me. I tried doing something like this def follow(): distance = math.hypot(abs(m.x - p.x), abs(m.y - p.y)) angle_radians = math.atan2(abs(m.y - p.y), abs(m.x - p.x)) if distance != 0: p.y += math.sin(angle_radians) p.x += math.cos(angle_radians) However, the block ends up moving