pygame

pygame(一)

旧巷老猫 提交于 2020-01-01 19:43:00
个人博客 pygame 安装见 这里 引用 import pygame ,除此外,个人还喜欢用sys与time个人喜好。 起始 from pygame.locals import * , pygame.init() 基本设置 窗口设置 windowSurface = pygame.display.set_mode((500, 400), 0, 32) pygame.display.set_caption('标题名称') 第一行设置窗口大小500*400,第二行设置窗口标题。 颜色与字体 一般用RGB表示,先设置好常用的,后面引用函数即可。 WHITE = (255, 255, 255) basicFont = pygame.font.SysFont(“仿宋”,48) 第二行为字体设置,字体样式与大小。 渲染对象 text =basicFont.render('文本', True,颜色, 颜色) render()为对文本的设置,第二个是抗锯齿(更为平滑)的布尔值,第三个是文本颜色,第四个是文本背景颜色。 位置与基本图形 文本位置 pygame的React属性用于表示一个特定大小和位置的矩形区域,可以作为窗口内对象的位置设置。 函数名 = text.get_rect() 函数名x = windowSurfcae.get_rect().conterx 函数名y =

Add collision detection to a plattformer in pygame

拟墨画扇 提交于 2020-01-01 18:20:51
问题 I'm working on a small platformer game in which you place blocks to make a level, then play it. I got gravity, jumping, and left and right movement.. but I am not sure how to make the player collide with walls when moving left or right. The way I want it to work is like this- if key[K_LEFT]: if not block to the left: move to the left How would I go about doing this (relative to this source): import pygame,random from pygame.locals import * import itertools pygame.init() screen=pygame.display

python and pygame shooting

若如初见. 提交于 2020-01-01 14:44:52
问题 I am working on a game in pygame, so far the player can walk around, 50 blue blocks spawn randomly on the screen and the player can walk around and shoot them, but there is one problem the player can only shoot up, I want the player to shoot towards the mouse but am having some trouble getting it to do this. this is my code import pygame from pygame import * import random black = ( 0, 0, 0) white = ( 255, 255, 255) red = ( 255, 0, 0) blue = ( 0, 0, 255) player_x, player_y = 0, 0 move_player_x

PyGame has huge delay with playing sounds

旧街凉风 提交于 2020-01-01 11:35:30
问题 I am playing around with building a python script that play rhythms like a drum machine. I have used PyGame to handle the audio. However I experience significant/unacceptable delays between calling play and hearing the actual audio. I pasted the following code into the interactive interpreter, and then execute the last line again and again: import pygame pygame.mixer.init(frequency=22050, size=-16, channels=2, buffer=4096) s = pygame.mixer.Sound('snare.wav') s.play(loops=0, maxtime=0, fade_ms

pyGame full core usage in simple loop

江枫思渺然 提交于 2020-01-01 08:02:33
问题 Edit This is an open issue and appears to be related to alsa audio. A workaround is to shutdown the audio mixer, or install pyGame from source. pygame.init() pygame.mixer.quit() I am just beginning development with pyGame and have found that I should use the following to gate CPU time: fps = 30 clock = pygame.time.Clock() while True: # Logic... clock.tick(fps) The issue that I seem to be having, is that the most basic draw a square program running at 1 FPS consumes a full CPU core. import

Transparent Window in Pygame or Python

给你一囗甜甜゛ 提交于 2020-01-01 07:11:09
问题 Im trying to make a transparent window for a splash screen for my game. The image has transparency but I cant make the window transparent(like see screen, desktop, etc.. behind it) All I've found is a no-go with pygame. Is there any external libraries that I could pull from to make this possible? BTW. This is entirely a Linux project. So os specific are ok too. #Splash Screen screen = pygame.display.set_mode((680,300), NOFRAME) splashbg = pygame.image.load("Images/SplashBG.png") font = pygame

Pygame: Draw single pixel

China☆狼群 提交于 2020-01-01 04:22:26
问题 I'm looking for method that allow me to draw single pixel on display screen. For example when I click mouse, I want the position of clicked pixel to change color. I know how to read mouse pos, but I could not find simple pixel draw ( there is screen.fill method but it's not working as I want). 回答1: You can do this with surface.set_at(): surface.set_at((x, y), color) You can also use pygame.gfxdraw.pixel(): from pygame import gfxdraw gfxdraw.pixel(surface, x, y, color) Do note, however, the

Using the python multiprocessing module for IO with pygame on Mac OS 10.7

放肆的年华 提交于 2020-01-01 04:14:12
问题 I use pygame for running experiments in cognitive science, and often I have heavy I/O demands so I like to fork off these tasks to separate processes (when using a multi-core machine) to improve performance of my code. However, I encountered a scenario where some code works on my colleague's linux machine (Ubuntu LTS), but not on my mac. Below is code representing a minimal reproducible example. My mac is a 2011 Macbook Air running 10.7.2 and using the default python 2.7.1. I tried both

Using the python multiprocessing module for IO with pygame on Mac OS 10.7

我怕爱的太早我们不能终老 提交于 2020-01-01 04:14:08
问题 I use pygame for running experiments in cognitive science, and often I have heavy I/O demands so I like to fork off these tasks to separate processes (when using a multi-core machine) to improve performance of my code. However, I encountered a scenario where some code works on my colleague's linux machine (Ubuntu LTS), but not on my mac. Below is code representing a minimal reproducible example. My mac is a 2011 Macbook Air running 10.7.2 and using the default python 2.7.1. I tried both

Simple Pygame Audio at a Frequency

泪湿孤枕 提交于 2020-01-01 03:12:08
问题 How can I create a 440 Hz sound that plays smoothly forever using audio with Pygame? I assume this should be easy enough, but I don't want to use any stupid files to accomplish the task. The final intent of this is to play a note while a key is being held down as I have asked in another question. Any help would be greatly appreciated as I have wasted great quantities of time trying to find an answer to this. 回答1: What kind of 440 Hz sound? There are many types of waves at "440 Hz": sine, saw,