pygame

pygame创建一个简单的窗口

。_饼干妹妹 提交于 2020-03-12 09:54:59
之前我们已经基本了解了Pygame的常用模块,和常用模块的一些方法及功能。现在我们可以简单的用pygame来创建一个可以改变颜色的窗口。一起来看看: 首先我们需要导入模块: import pygame, sys 然后需要初始化pygame,我定义了一个pygame_test()方法,所以如果需要执行程序的话,要调用这个方法哟: import pygame, sys def pygame_test(): # 初始化 pygame.init() 初始化之后就可以开始创建窗口了,我创建了一个500x500的窗口,这时执行程序我们就能看到创建被创建好了,但是窗口出现后马上就会退出: import pygame, sys def pygame_test(): # 初始化 pygame.init() # 创建一个窗口 screen = pygame.display.set_mode((500, 500)) # 设置窗口标题 pygame.display.set_caption('这是一个窗口标题') pygame_test() 所以我们可以给它加上一个while循环,将值设为True(也就是一个死循环)。可以通过不断循环来侦听事件,如果想要退出程序可以使用 sys.exit() 。 import pygame, sys def pygame_test(): # 初始化 pygame.init(

pygame----窗口创建与命名

為{幸葍}努か 提交于 2020-03-12 09:48:25
开发工具:pycharm 基本介绍:使出现的窗口显示窗口名和窗口图片 导入pygame模块 编写hello world函数 运行主代码 一,导入pygame模块 import pygame from pygame.locals import * import sys 二,编写hello world函数 def hell_world (): pygame.init() pygame.display.set_mode(( 640 , 480 )) pygame.display.set_caption( 'hello world' ) while True : for event in pygame.event.get(): if event.type==QUIT: pygame.quit() sys.exit() pygame.display.update() 三,主函数 if __name__ == '__main__' : hell_world() 参考代码: import pygame from pygame.locals import * import sys def hell_world (): pygame.init() pygame.display.set_mode(( 640 , 480 )) pygame.display.set_caption( 'hello

Shooting a bullet in pygame in the direction of mouse

谁说胖子不能爱 提交于 2020-03-12 05:29:18
问题 I just cant figure out why my bullet is not working. I made a bullet class and here it is: class Bullet: def __init__(self): self.x = player.x self.y = player.y self.height = 7 self.width = 2 self.bullet = pygame.Surface((self.width, self.height)) self.bullet.fill((255, 255, 255)) Now i added several function in my game class and here it is: class Game: def __init__(self): self.bullets = [] def shoot_bullet(self): if self.bullets: for bullet in self.bullets: rise = mouse.y - player.y run =

pygame installation issue in mac os

五迷三道 提交于 2020-03-07 07:57:10
问题 I have following issue of installing pygame package. In file included from src/_numericsurfarray.c:23: src/pygame.h:106:10: fatal error: 'SDL.h' file not found #include <SDL.h> ^ 1 error generated. error: Setup script exited with error: command 'gcc' failed with exit status 1 System information Mac OS-10.9.2 python version- Python 2.7.5 :: Anaconda 1.6.1 (x86_64) Any suggestion will be greatly appreciate ? Thanks. 回答1: Here (OSX Mavericks) I got able to install this way: brew install sdl sdl

Pygame Collision Detection error

删除回忆录丶 提交于 2020-03-05 05:04:40
问题 import pygame, random, time class Game(object): def main(self, screen): bg = pygame.image.load('data/bg.png') house1 = pygame.image.load('data/house1.png') playerIdle = pygame.image.load('data/playerIdle.png') playerRight = pygame.image.load('data/playerRight.png') playerLeft = pygame.image.load('data/playerLeft.png') playerUp = pygame.image.load('data/playerUp.png') playerX = 0 playerY = 50 clock = pygame.time.Clock() spriteList = [] gameIsRunning = False house1Selected = False while 1:

Using a matrix as a sprite and testing if two sprites overlap

送分小仙女□ 提交于 2020-03-03 13:59:54
问题 Right now, I have a ball that moves around the screen in a random diagonal direction and bounces off the wall when it collides with it. An image of said program My end goal is to have the ball be a matrix and have it perform just the same around a 30x30 grid. I have tried the enumerate() function, but couldn't think of how to make it perform like it does in the picture and code I have referenced at the end of this post. by perform, I mean move randomly in a diagonal direction on the screen

Get color of individual pixels of images in pygame

余生颓废 提交于 2020-03-01 12:47:13
问题 How can I get the colour values of pixels of an image that is blitted onto a pygame surface? Using Surface.get_at() only returns the color of the surface layer, not the image that has been blitted over it. 回答1: The method surface.get_at is fine. Here is an example showing the difference when blitting an image without alpha channel. import sys, pygame pygame.init() size = width, height = 320, 240 screen = pygame.display.set_mode(size) image = pygame.image.load("./img.bmp") image_rect = image

Get color of individual pixels of images in pygame

北城余情 提交于 2020-03-01 12:44:32
问题 How can I get the colour values of pixels of an image that is blitted onto a pygame surface? Using Surface.get_at() only returns the color of the surface layer, not the image that has been blitted over it. 回答1: The method surface.get_at is fine. Here is an example showing the difference when blitting an image without alpha channel. import sys, pygame pygame.init() size = width, height = 320, 240 screen = pygame.display.set_mode(size) image = pygame.image.load("./img.bmp") image_rect = image

Phython 小游戏编写

依然范特西╮ 提交于 2020-02-27 10:34:10
使用PyGame库编写一个对性能要求不那么高的小游戏是非常高效的 特别是Clock机制用起来很舒服,告别界面刷新的麻烦 clock = pygame.time.Clock() pygame.display.flip() clock.tick(tms_sec) # tms_sec=timers in a second 春节放假在家,有时间写了一个打地鼠的小游戏,给小朋友玩玩还是足够了,呵呵 while sw_game: if timer>0 and timer%tms_sec==0 and clicked == False: # next round # redraw the playgd for i in range(1,10,1): pygame.draw.rect( playgd, color_bk,blok_list[i-1],0) # new rats pos seed_n = random.randint(0,8) while seed_n==seed_o: seed_n = random.randint(0,8) seed_x = seed_n//3+1 seed_y = seed_n%3+1 seed_o = seed_n # draw the rats(icon) rat_x = diff_w*seed_x+blok_w*(seed_x-1) rat_y =

pygame.display.update runs after pygame.time.wait(1000)

故事扮演 提交于 2020-02-25 06:44:25
问题 I am working through a tutorial. The tutorial can be found here https://inventwithpython.com/makinggames.pdf The main script i am referring to can be found here https://inventwithpython.com/memorypuzzle.py There is a bug in this program that I have been trying to resolve for days. In the main game loop ` while True: # main game loop mouseClicked = False DISPLAYSURF.fill(BGCOLOR) # drawing the window drawBoard(mainBoard, revealedBoxes) for event in pygame.event.get(): # event handling loop if